4

Here is my code:

\documentclass{article}

\makeatletter
\def\Printdimen#1#2{\strip@pt \dimexpr #2 *65526 /\number\dimexpr 1#1}
\makeatother

\begin{document}

\Printdimen{mm}{\hsize} mm
\end{document}
David Carlisle
  • 757,742

1 Answers1

5

The macro \strip@pt wants after it a dimension register; it will remove the final pt after the extracted value.

In e-TeX, \dimexpr behaves as an “unnamed” dimension register, so it's good after \strip@pt. According to the syntax rules of \dimexpr, a dimension (implicit or explicit) inside it can be followed by * or / to denote multiplication or (rounded) division. However the multiplier/divisor must be an integer.

So \dimexpr 3pt/2 is valid, but \dimexpr3pt/1.5 is not.

A dimension register appearing in a context where TeX expects an integer will be coerced to it using the value in scaled points; 65536 scaled points are 1pt, which is the reason for the 65536 multiplier.

Thus \number is not really necessary and

\makeatletter
\def\Printdimen#1#2{\strip@pt\dimexpr (#2) * 65536 / \dimexpr 1#1\relax\relax}
\makeatother

is good as well. It's better to add the two \relax that end the \dimexpr expression.

egreg
  • 1,121,712