5

I'm trying to set a distance which is relative to the font size. The problem is that the distance is reused several times in the document, potentially in environments with a different font size. In each case the distance should automatically rescale to retain it's proportion to the font size. Unfortunately, simply specifying the distance in em or ex doesn't work because the distance is stored at the size of em or ex in the current font. When the font changes, the distance is not updated accordingly.

Is there a way around this? Ideally the solution should work under both LaTeX and PlainTeX.

Below is a M(n)WE which shows the current, undesired, behavior.

\documentclass[12pt]{article}

\newskip\test
\test = 1em

\begin{document}

In each row, the lines should be the same length.

\rule{0.4pt}{1em} \rule{0.4pt}{\test}

{\tiny\rule{0.4pt}{1em} \rule{0.4pt}{\test}}

{\Huge\rule{0.4pt}{1em} \rule{0.4pt}{\test}}

\end{document}

2 Answers2

4

Define the distance as a macro, rather than a length register:

\documentclass[12pt]{article}

\newcommand{\test}{1em}

\begin{document}

In each row, the lines should be the same length.

\rule{0.4pt}{1em} \rule{0.4pt}{\test}

{\tiny\rule{0.4pt}{1em} \rule{0.4pt}{\test}}

{\Huge\rule{0.4pt}{1em} \rule{0.4pt}{\test}}

\end{document}

enter image description here

egreg
  • 1,121,712
3

You can define a new command for the rule that resets the rule length each time you call it. Because a Plain-compatible solution was requested I used \newskip, but I include a LaTeX version commented out below it.

\documentclass[12pt]{article}

\newskip\ruleheight

\def\myrule{%
    \ruleheight = 1em
    \rule{0.4pt}{\ruleheight}%
}

% LaTeX version
%\newlength{\ruleheight}
%\newcommand{\myrule}{%
%    \setlength{\ruleheight}{1em}
%    \rule{0.4pt}{\ruleheight}%
%    

\begin{document}

In each row, the lines should be the same length.

\rule{0.4pt}{1em} \myrule

\tiny \rule{0.4pt}{1em} \myrule

\Huge \rule{0.4pt}{1em} \myrule

\end{document}

The bottom three lines show the output of the new command in different sizes.

enter image description here

musarithmia
  • 12,463
  • While probably adaptable to my use case, the use of \rule in my MWE was simply to demonstrate the problem. The distance actually gets used in a variety of contexts (sizing boxes, spacing elements, etc.). As a result, I think egreg's solution below will be the more practical one. – rpspringuel Feb 20 '15 at 15:51
  • Yes, defining it as a macro means that every time the length is called it is expanded only at that moment, so the current value of the length register would be applied. I was redoing by hand something TeX can already do automatically through macro expansion. – musarithmia Feb 20 '15 at 15:58