3

I have to calculate the ratio of two dimensions, and display it (possibly as the rounded int value).

I don't know how to produce the best MWE but here is an example:

\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\test}{}{%

\dim_ratio:nn{\columnwidth}{\textheight}

}
\ExplSyntaxOff
\begin{document}
\test

\end{document}

Outputs:

22609920/36044800

And now I'm stuck since I don't know how to make it calculate the ratio, nor I am able to set it to a floating point variable e.g. via

\fp_new:N\l_my_fp
\fp_set:Non\l_my_fp{\dim_ratio:nn{\columnwidth}{\textheight}}

Since I get an error:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! ./Senza-titolo.tex:13: LaTeX error: "kernel/misused-fp" ! ! A floating point with value '0.6272727272727273' was misused. ! ! See the LaTeX3 documentation for further information. ! ! For immediate help type H . !...............................................

2
  • 2
    You don't need to load expl3 when xparse is used -- xparse is an expl3 package – user31729 Apr 22 '17 at 11:48
  • @ChristianHupfer Thanks, I did not know that – Moriambar Apr 22 '17 at 11:49
4

There is more than one way, I think.

If you want to have an expandable way, \fp_eval:n {\dim_ratio:nn{}{}} (shortened) is enough, you don't need \NewDocumentCommand for this then.

\documentclass{article}
%\usepackage{expl3}
\usepackage{xparse}

\ExplSyntaxOn

 \fp_new:N \l_moriambar_ratio_fp 

 % Expandable: 

\cs_new:Npn \calctheratio #1#2 {%
  \fp_eval:n {\dim_ratio:nn{\columnwidth}{\textheight}}
}  


\NewDocumentCommand{\test}{}{%


\fp_set:Nn \l_moriambar_ratio_fp  {\dim_ratio:nn{\columnwidth}{\textheight}}

\fp_use:N \l_moriambar_ratio_fp


}
\ExplSyntaxOff
\begin{document}
\test

\calctheratio{\columnwidth}{\textwidth}

\end{document}

enter image description here

2
  • that was easy... – Moriambar Apr 22 '17 at 11:42
  • @Moriambar: Sometimes expl3 is quite easy... – user31729 Apr 22 '17 at 11:56
1

There is not need for expl3 here at all.

\documentclass{article}

\makeatletter
\def\test{%
  \strip@pt\dimexpr\columnwidth/\strip@pt\textheight\relax}
\makeatother

\begin{document}

\test

\end{document}

Even better: Use LuaTeX!

\documentclass{article}
\begin{document}

\directlua{
  tex.sprint(tex.getdimen("columnwidth")/tex.getdimen("textheight"))
}

\end{document}
1
  • Background info from chat: The OP is writting a package completely in expl3, that's why he asked about about expl3 way – user31729 Apr 23 '17 at 7:51

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.