4

All I needed to do is divide 300pt by 5mm to get result either in pt or in mm units. But I got something else.

\documentclass[varwidth]{standalone}
\usepackage{expl3}
\begin{document}
  \ExplSyntaxOn
    % following is 60.0ptmm (which is 300/5,
    %  not to mention silly units; ridiculous)
    \dim_eval:n{300pt/5mm}\\

    % following is 14.22636pt (I guess/hope it's correct
    %  conversion of 5mm to pt units)
    \dim_eval:n{5mm}\\

    % following is 21.42857pt (which is 300/14,
    %  not 300/14.22636)
    %   followed by .22636pt (decimal of 14.22636pt;
    %    which is also ridiculous)
    \dim_eval:n{300pt/14.22636pt}
  \ExplSyntaxOff
\end{document}

How do I get correct result in pt units (answer that also shows result in mm units will be greatly appreciated) when dividing points by millimeters inside \dim_eval:n?

  • 1
    incidentally why do you want the ratio of two lengths to be expressed in pt rather than just as a number? – David Carlisle May 23 at 7:01
  • is your intention to divide 300mm by 5? (rather than divide by 5pt) ? – David Carlisle May 23 at 7:08
  • 2
    /instrumentation professor hat on/ dividing a length by a length, whatever the units, gives a pure number. Converting a pure number to whatever length has no significance at all... you should multiply it by a length before. – Rmano May 23 at 7:46
8

\dim_eval:n uses ε-TeX's \dimexpr to perform the calculation, and \dimexpr doesn't do floating point arithmetics; it can only divide dimensions by integers. What you get when you do

\dim_eval:n{300pt/5mm}

is actually

\dim_eval:n{300pt/5}mm

(thus the “silly units”), and with

\dim_eval:n{300pt/14.22636pt}

you are actually doing

\dim_eval:n{300pt/14}.22636pt

which are exactly the output you see.

Use \fp_to_dim:n (or \fp_to_decimal:n if you don't want the units). With versions of expl3 older than May 23, 2019 you need to put the denominator around parentheses to stick the unit to the number. In newer versions multiplication by juxtaposition (e.g., 5mm or 2pi) has higher precedence than explicit multiplication and division so that the unit is converted before the division:

\documentclass[varwidth]{standalone}
\usepackage{expl3}
\begin{document}
  \ExplSyntaxOn
    \fp_to_dim:n{300pt/5mm}\\
    % \fp_to_dim:n{300pt/(5mm)}\\
    \fp_to_dim:n{5mm}\\
    % \fp_to_dim:n{300pt/(14.22636pt)}
    \fp_to_dim:n{300pt/14.22636pt}
  \ExplSyntaxOff
\end{document}

enter image description here

5

It is more natural to express a ratio of two lengths as a number than as a length:

enter image description here

\documentclass[varwidth]{standalone}
\usepackage{expl3}
\begin{document}
  \ExplSyntaxOn

    \fp_eval:n{(300pt)/(5mm)}

   \ExplSyntaxOff
\end{document}

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.