1

Is it possible to define a macro that changes font based on width that a box "would be"?

I'd like to define a LaTeX macro that does something like:

\newcommand{\mylabel}[1] {
    \if\latexMagicWidthOf{\labelfont #1}>2em
        {\labelfont #1}
    \else
        {\condensedlabelfont #1}
    \fi
}

In other words, if #1 would be wider than 2em in labelfont, use condensedlabelfont instead.

I think I could implement \latexMagicWidthOf with a \newbox and \wd\labelbox, but I'm stuck on comparing dimensions for anything but equality.

1 Answers1

3

You could use

\newsavebox\mybox
\newcommand{\mylabel}[1] {%%%%%
    \sbox\mybox{\labelfont #1}%
    \ifdim\wd\mybox <2em % do you want to set the font here? 2em is in whatever the current font is
        \usebox{\mybox}%%%%
    \else
        {\condensedlabelfont #1}%%%%
    \fi
}

But perhaps simpler would be

\newcommand{\mylabel}[1] {{\labelfont\resizebox{2em}{!}{#1}}}

which will set the text 2em wide in the labelfont.

David Carlisle
  • 757,742