# Replacing length registers in expressions

I have some understanding on how to use length registers for calculations. However, I always end up requiring way too many length registers for tasks which could probably be solved better by expressions. How can avoid the use of so many registers?

To make things more precise: I have a MWE, which does what I want:

\documentclass{article}
\usepackage{calc}

\def\cone{CONTENT ONE}
\def\ctwo{CONTENT TWO}

\begin{document}

\newdimen\coneheight
\newdimen\conewidth
\newdimen\ctwoheight
\settoheight{\coneheight}{\cone}
\settowidth{\conewidth}{\cone}
\settoheight{\ctwoheight}{\ctwo}
\newdimen\scaled
\setlength{\scaled}{\dimexpr \numexpr \ctwoheight * \conewidth / \coneheight  \relax sp\relax}
Result: \the\scaled

\end{document}


What I am think of is something like this:

\usepackage{calc}

\def\cone{CONTENT ONE}
\def\ctwo{CONTENT TWO}

\begin{document}

\newdimen\better
\setlength{\better}{\dimexpr \numexpr \heightof{\ctwo} * \widthof{\cone} / \heightof{\cone} \relax sp\relax}
Better: \the\better

\end{document}


but unfortunately this code barks at me in all possible kinds...which I do not understand, since I am actually only replacing length registers by length expressions. Or not?

What am I doing wrong and how can I end up in a more compact expression style which avoids placing all lengths into registers?

\documentclass{article}
\usepackage{calc}

\newcommand\cone{CONTENT ONE}
\newcommand\ctwo{CONTENT TWO}
\newlength\scaled

\begin{document}

\setlength{\scaled}{\widthof{\cone}*\ratio{\heightof{\ctwo}}{\heightof{\cone}}}

\the\scaled

\end{document}


The output is

76.66681pt

• Precisely that. Is. It. Thanx a lot. BTW: Where would I have found that in the docs? I mean a clear spec of type and semantics of \widthof. I did not manage with the calc docu. Nov 18, 2020 at 13:40

Use temporary box registers to capture the dimensions needed.

In the MWE, I show for comparison both the original approach "Result" of the OP, as well as the revised "Better" approach using box registers.

\documentclass{article}
\usepackage{calc}

\def\cone{CONTENT ONE}
\def\ctwo{CONTENT TWO}

\begin{document}

\newdimen\coneheight
\newdimen\conewidth
\newdimen\ctwoheight
\settoheight{\coneheight}{\cone}
\settowidth{\conewidth}{\cone}
\settoheight{\ctwoheight}{\ctwo}
\newdimen\scaled
\setlength{\scaled}{\dimexpr \numexpr \ctwoheight * \conewidth / \coneheight  \relax sp\relax}
Result: \the\scaled

\sbox0\cone
\sbox2\ctwo
\newdimen\better
\setlength{\better}{\dimexpr \numexpr \ht2 * \wd0 / \ht0 \relax sp\relax}
Better: \the\better

\end{document}


If one wishes to avoid scrogging the temporary box registers, one can create new saveboxes:

\documentclass{article}
\usepackage{calc}

\def\cone{CONTENT ONE}
\def\ctwo{CONTENT TWO}

\begin{document}

\newsavebox\boxone
\newsavebox\boxtwo
\sbox\boxone\cone
\sbox\boxtwo\ctwo
\newdimen\better
\setlength{\better}{\dimexpr \numexpr \ht\boxtwo * \wd\boxone / \ht\boxone \relax sp\relax}
Better: \the\better

\end{document}

• Helpful, but larger than the accepted solution and has side effects on registers. Nov 18, 2020 at 13:41
• @Nobody-Knows-I-am-a-Dog I repeated the first part, "Result" for the benefit of the reader. Only short second part "Better" is needed. Nov 18, 2020 at 13:49
• @Nobody-Knows-I-am-a-Dog As to affecting the registers, once can replace temporary registers with \newsaveboxes, to avoid scrogging the registers, as shown in edited answer. Nov 18, 2020 at 13:54