4

I have a problem dividing two lenghts and getting a result as an integer. I was trying to use this package:

\usepackage[nomessages]{fp}

It didn't really work for me. I tried it multiple ways, but it still didnt work.

Can you help me with an alternative way?

8

You can use \numexpr and \dimexpr, but this rounds rather than truncate:

\documentclass{article}

\newcommand{\getlengthratio}[2]{%
  \number\numexpr
    \dimexpr#1\relax
     /
    \dimexpr#2\relax
  \relax}

\newcounter{test}

\begin{document}

There are \getlengthratio{\textwidth}{\parindent} parindents in a line

\setcounter{test}{\getlengthratio{\textwidth}{\parindent}}

The same number: \thetest

\end{document}

A different approach, where you can choose between floor, ceil and round, is with expl3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\getlengthratio}{mm}
 {
  \fp_eval:n { floor ( \dim_to_fp:n { #1 } / \dim_to_fp:n { #2 } , 0 ) }
 }
\ExplSyntaxOff

\newcounter{test}

\begin{document}

There are \getlengthratio{\textwidth}{\parindent} parindents in a line

\setcounter{test}{\getlengthratio{\textwidth}{\parindent}}

The same number: \thetest

\end{document}

enter image description here

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.