8

TeX stores dimensions internally in scaled points but operation \the expands to the corresponding value in points. Can we avoid the conversion and get the more precise, "original" value instead?

9
$ tex \\relax
This is TeX, Version 3.14159265 (TeX Live 2014) (preloaded format=tex)

*
(Please type a command or say `\end')
*\dimen0=3.5in

*\showthe\dimen0
> 252.94499pt.
<*> \showthe\dimen0

? 

*\count0=\dimen0

*\showthe\count0
> 16577003.
<*> \showthe\count0

dimen values are just integer values interpreted as sp so you can do the above (although actually there is no loss of precision I think in the fractional pt value tex shows, it always shows enough decimal places to determine the exact value in sp)

6
  • 2
    If e-TeX is available, \showtokens\expandafter{\number\dimen0} avoids assigning the value to a count register. In general \number\dimen0 will expand the dimension in scaled points (with possible number termination problems, of course). – egreg Oct 23 '14 at 8:58
  • This was an easy question :-) But how about dimension values in the output file? Can we have them in scaled points, too? – SJU Oct 23 '14 at 11:45
  • @egreg What are number termination problems? – SJU Oct 23 '14 at 11:47
  • @AngelTsankov Don't worry: leave a space after \dimen0 and you'll be OK. – egreg Oct 23 '14 at 11:53
  • @egreg so modern, as always, I forget the 1980's have finished. – David Carlisle Oct 23 '14 at 11:57
6

For printing a length in sp units, define

\newcommand\lengthinsp[1]{\number\dimexpr#1\relax sp}

This will accept \lengthinsp{\parindent} (with a register) or even \lengthinsp{3.5in}.

Example:

\documentclass{article}

\newcommand\lengthinsp[1]{\number\dimexpr#1\relax sp}

\begin{document}

\lengthinsp{\parindent}

\lengthinsp{3.5in}

\end{document}

enter image description here

This is fully expandable. If you don't want to depend on e-TeX, a non-expandable version is needed:

\makeatletter
\newcommand{\lengthinsp}[1]{%
  \begingroup
  \dimen@=#1\relax
  \number\dimen@ sp%
  \endgroup
}
\makeatother

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.