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:

enter image description here


  1. Why does \KernA not work?
  2. When can I use \widthof directly?








%x~\KernA~y  \verb|\kernA: \widthof| ??

x~\KernB~y  \verb|\KernB: \setlength and \widthof|

x~\KernC~y  \verb|\KernC: \settowidth|
  • 1
    You could just use \hphantom{$text$} instead. May 1, 2012 at 20:47
  • @MartinScharrer: Yes that would be a simpler solution for this situation in the MWE, but this was meant to be a more general question. May 1, 2012 at 20:55

2 Answers 2


\kern is a TeX primitive which awaits a dimension expression, while \widthof is a calc 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.


The problem here is that \widthof is actually only a "macro" that works inside one of calc's redefined length macros. Using \show\widthof you'll note that it is actually defined as \ignorespaces... a placeholder and action to remove any preceding spaces in the argument. As such, your two-step procedure (using an interim length) is required to obtain the desired result.

The alternative \settowidth macro does box assignment and length extraction using (from latex.ltx):

\def\settodepth {\@settodim\dp}
\def\settowidth {\@settodim\wd}
  • 3
    The calc package redefines \setcounter, \addtocounter, \setlength and \addtolength to benefit from the extended syntax. Only in their arguments \widthof and the similar commands receive the meaning for doing computations; outside of them they are equivalent, as you say, to \ignorespaces.
    – egreg
    May 1, 2012 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.