I sometimes encounter a problem when I attempt to use \widthof
from the calc
package, and don't know why. In the past I have just worked around it using a two step process of defining a \newdimen{}
and using \settowidth
(or \setlength
and \widthof
) to determine the value before attempting to use this length.
The MWE below shows how this fails when I attempt to use the \widthof
as the length of a \kern
(i.e., the \KernA
macro), but the two step process works just fine:
Questions:
- Why does
\KernA
not work? - When can I use
\widthof
directly?
References:
Code:
\documentclass{article}
\usepackage{calc}
\newcommand*{\KernA}{\kern\widthof{$text$}}%
\newdimen{\KernAmount}%
\newcommand*{\KernB}{%
\setlength{\KernAmount}{\widthof{$text$}}%
\kern\KernAmount%
}%
\newcommand*{\KernC}{%
\settowidth{\KernAmount}{$text$}%
\kern\KernAmount%
}%
\begin{document}
x~$text$~y
%x~\KernA~y \verb|\kernA: \widthof| ??
x~\KernB~y \verb|\KernB: \setlength and \widthof|
x~\KernC~y \verb|\KernC: \settowidth|
\end{document}
Best Answer
\kern
is a TeX primitive which awaits a dimension expression, while\widthof
is acalc
package function which only works in\setlength
and friends. It requires internal box assignments, which are not allowed in a normal dimension expression. You can't therefore use\widthof
and other things like this at positions where TeX dimension expressions are awaited. You need to use\setlength
first to assign the value to a length register and then use this register, as you did.