\newlength{\mylength}
\settowidth{\mylength}{invisible word}
\makebox[\mylength]{hello}
Is there a way to do this using a single command? For example,
\phantom[hello]{invisible word}
or
\phantom[hello][l]{invisible word}
\phantom[hello][r]{invisible word}
\phantom[hello][s]{invisible word}
for various alignments. Ideally, I'd like the command to work in math
mode too. So if possible, I'd like it if I didn't have to include $
signs in the following example:
\newlength{\mylength}
\settowidth{\mylength}{$a+b=c$}
\makebox[\mylength]{$f_1$}
So...I'd like to implement the above as:
$\phantom[f_1]{a+b=c}$
Any ideas?
I'd define a new command that looks pretty much like the code you
listed above, noting that \makebox takes an optional alignment parameter:
\documentclass{minimal}
\newlength{\phantomlength}
\newcommand{\phantomword}[3][c]{%
\settowidth{\phantomlength}{#3}
\makebox[\phantomlength][#1]{#2}%
}
\begin{document}
\setlength{\parindent}{0pt}
invisible word\par
\phantomword[l]{hello}{invisible word}\par
\phantomword[r]{hello}{invisible word}\par
\phantomword[c]{hello}{invisible word}\par
\end{document}
> Ideally, I'd like the command to work in math
> mode too. So if possible, I'd like it if I didn't have to include $
> signs in the following example:
>
> \newlength{\mylength}
> \settowidth{\mylength}{$a+b=c$}
> \makebox[\mylength]{$f_1$}
>
> So...I'd like to implement the above as:
>
> $\phantom[f_1]{a+b=c}$
>
> Any ideas?
That's nothing an \ifmmode can't handle:
\newlength{\phantomlength}
\newcommand{\phantomword}[3][c]{%
\ifmmode
\settowidth{\phantomlength}{\ensuremath{#3}}
\makebox[\phantomlength][#1]{\ensuremath{#2}}%
\else
\settowidth{\phantomlength}{#3}
\makebox[\phantomlength][#1]{#2}%
\fi
}
-- Scott
You can save a dimen register (and 2 lines of code!) with the help of calc.
...
\usepackage{calc}
...
\newcommand*\phantomas[3][c]{%
\ifmmode
\makebox[\widthof{$#2$}][#1]{$#3$}%
\else
\makebox[\widthof{#2}][#1]{#3}%
\fi
}
...
A long entry a long entry a long entry\par
\phantomas{A long entry}{hello} \phantomas[l]{a long entry}{world}
\phantomas[r]{a long entry}{!}
$\sin^2 x + \cos^2 x = 1$\par
$\phantomas{\sin^2 x + \cos^2 x}{f(x)} = 1$\par
$\phantomas[l]{\sin^2 x + \cos^2 x}{f(x)} = 1$\par
$\phantomas[r]{\sin^2 x + \cos^2 x}{f(x)} = 1$
...
\end{document}
It could be even better testing displaymode or not when \ifmmode is true
(\ifinner ... \else ...{$\displaystyle #2$}[#1]{$\displaystyle #3$}\fi).
In fact, \mathchoice should be the best solution here.
Jean-Côme Charpentier