I am trying to convert \textwidth to other units I want and do calculations with the number (without the unit).

How does one do this?

  • 1
    What do you want to do exactly? A number can't be a command… – Bernard Jun 18 '16 at 17:49
  • The pgfmath package does math without units. \pgfmathparse{\textwidth/1mm} will put the value you want into \pgfmathresult. – John Kormylo Jun 18 '16 at 18:49

\strip@pt removes the unit pt and eTeX's \dimexpr can be used for the calculation:


% #1: macro, which gets the result of the conversion without unit
% #2: length expression
    \strip@pt\dimexpr(#2)*2540/7227\relax % 72.27 pt = 1 in = 25.4 mm

The text width is \SI{\mmTextWidth}{\milli\meter}.


| improve this answer | |

You can use my calculator package. The \LENGTHDIVIDE command divides two lengths and stores result as a number in a new command, as you need.

Try this code:


In a standard a4 article \size returns 121.25427

| improve this answer | |

Length conversion to decimal using LaTeX3:

enter image description here



%\cs_new_eq:NN \calc \fp_eval:n
\cs_new_eq:NN \convertlen \dim_to_decimal_in_unit:nn


\verb|\textwidth| in \verb|pt|s: \the\textwidth

\verb|1mm| in \verb|pt|s: \setlength{\@tempdima}{1mm}\the\@tempdima

$\frac{\texttt{\string\textwidth}}{\texttt{1mm}} = \convertlen{\textwidth}{1mm}$


The above defines the user interface \convertlen{<fromlen>}{<tounit>} using LaTeX3's \dim_to_decimal_in_unit.

| improve this answer | |
  • Interestingly there is a significant drop in precision. Seems that the calculation does basically the quotient with 1mm first approximated by 186467sp which is what \number\dimexpr 1mm\relax returns (I obtain same result as you if I do \xinttheiexpr [5]\textwidth/\dimexpr 1mm\relax\relax.) The computation as in Heiko's answer is more accurate: that would be \xinttheiexpr [5] \textwidth*2540/(7227*65536)\relax, where the ratio is also in smallest terms635/118407168. – user4686 Sep 11 '16 at 19:45
  • for 345pt, the exact value in mm is 345*2540/7227=292100/2409=121.253632212536322125363221253632... – user4686 Sep 11 '16 at 19:50
  • the exact value of 1mm in sp is 186467.9811..., but TeX replaces it by 186467 as it truncates when one assigns dimensions to register. That means basically a relative error of order 0.5*10^-5. And indeed the \dim_to_decimal_in_unit provides here a result which is wrong at the sixth decimal. – user4686 Sep 11 '16 at 19:59

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.