7

For various purposes I need to know the width of a given text. The easiest way to do this is to use the \widthof{} command from the calc package. Issue is that I can't understand what unit of measurement is this command using. I expected it to be pt, but the number gets so big that it can't possibly be that.

The following MWE, for instance, will print the value 2081098:

\documentclass[11pt, a4paper]{article}

\usepackage{calc}

\newcounter{a}

\begin{document}

\setcounter{a}{\widthof{asdasd}}
\thea

\end{document}

The package documentation doesn't indicate the unit of measurement.

  • 4
    Scaled points; 65536sp=1pt. But why using a counter? If \mylen is a length, \settowidth{\mylen}{asdasd} would store the width more easily. – egreg Mar 21 '17 at 13:14
  • 2
    this is nothing to do with \widthof, it is how any length is cast to a counter, try \setcounter{a}{\textwidth} for example. – David Carlisle Mar 21 '17 at 13:19
  • I was totally noob on the lengths matter. Now I see how to use them. Thanks everybody – il mietitore Mar 21 '17 at 14:17
9

When you do

\documentclass[11pt]{article}

\newlength{\mylen}
\newcounter{a}

\begin{document}

\settowidth{\mylen}{asdasd}

\the\mylen

\setlength{\mylen}{\widthof{asdasd}}

\the\mylen

\end{document}

you get

31.75504pt
31.75504pt

If you add

\setcounter{a}{\mylen}
\thea

you get

2081098

because

31.75504 * 65536 = 2081098.30144

and TeX truncates this to an integer. The dimension in points is coerced to scaled points (65536sp = 1pt).

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.