Divide two lengths
You can't divide one dimension by another and get a dimension: the units would cancel. You need the ratio of the two multiplied by the numerator. For example
\documentclass{article}
\usepackage{calc}
\newlength\A
\newlength\B
\newlength\C
\setlength\A{10pt}
\setlength\B{5pt}
\setlength\C{\A*\ratio{\A}{\B}}
\begin{document}
\the\C
\end{document}
takes the ratio and multiplies it by \A
, whereas
\documentclass{article}
\usepackage{calc}
\newlength\A
\newlength\B
\newlength\C
\setlength\A{10pt}
\setlength\B{5pt}
\setlength\C{1pt * \ratio{\A}{\B} }
\begin{document}
\the\C
\end{document}
gives the 'ratio as a length' (not quite sure that makes sense, but it's what the question asks!).
You don't need calc
package:
\documentclass{article}
\newlength\A
\newlength\B
\newlength\C
\setlength\A{10pt}
\setlength\B{5pt}
\setlength\C{\dimexpr \numexpr \A * \A / \B \relax sp\relax}
\begin{document}
\the\C
\end{document}
produces 20.0pt
.
I include the \relax
for clarity, they terminate the expressions and get swallowed. Depending how \setlength
is coded (not checked) the second one may be superfluous. The first one is superfluous because the letter s
terminates the \numexpr
with no error.
Explanations:
inside
\numexpr
a dimension is converted into an integer representing its value in "scaled points" (1sp = 1/65536pt). This is in fact the TeX internal representation of a length (or rather the\dimen
part of it, discarding glue components; the LaTeX lengths are actually skips).inside
\numexpr
you are allowed to do in temporary doubled precision a so-called "scaling" operation i.e.(u times v) divided by w
. No overflow will be raised by the first operation, even though the intermediate product exceeds the2^31-1
TeX bound for numbers.finally we convert back to dimen by using explicitely unit
sp
.
Related notes:
the graphics package internally has some macro to divide dimensions,
in a another answer of mine which I will will try to find I provided a TeX macro (non-expandable) not using e-TeX doing this division with higher precision than the graphics version.
Related:
see https://tex.stackexchange.com/a/328894/4686, particularly the \divdef
macro from Part D for non-eTeX macro reliably dividing lengths and producing the result as a fixed point number with 4 or 5 decimal digits (after decimal mark).
This is not using any extra package.