Constant Length of \underline

Here, \funderline will have a fixed minimum width of \qquad, but will grow if the length of the word demands it. I accomplish it by underlining a superimposed stack of the argument #1 and a \qquad, with center alignment.

\documentclass{article}
\usepackage{stackengine}
\newcommand\funderline[1]{\underline{\stackengine{0pt}{\qquad}{#1}{O}{c}{F}{F}{L}}}
\begin{document}
This is \funderline{a} \funderline{test} of an \funderline{extremely} long underline.
\end{document}

enter image description here

If I misunderstood the question, and the intent is to have the underline always be 2em length, even if the word is longer, then this:

\documentclass{article}
\usepackage{stackengine}
\newcommand\funderline[1]{%
  \stackengine{0pt}{\underline{\qquad\vphantom{#1}}}{#1}{O}{c}{F}{F}{L}}
\begin{document}
This is \funderline{a} \funderline{test} of an \funderline{extremely} long underline.
\end{document}

enter image description here


Here you are. I added a variant, based on eqparbox which allows all \ulmakebox sharing the same tag (default ULB) to have the same underlining width, with a minimum of 2em:

\documentclass{article}

\usepackage{eqparbox, ulem}
\newcommand\ulmakebox[2][ULB]{\eqsetminwidth{#1}{2em}\underline{\eqmakebox[#1]{#2}}}

\begin{document}

The underline \underline{\qquad} (\verb+\underline{\qquad}+) is the length of the underline that I would like to have no matter the contents of the \verb+\underline+ argument. The length of the contents is always shorter than 2em (a \verb+\qquad+).

Example: I would like, say, `$1$' and `$11$' to be placed at the middle of an \underline{\qquad} (\verb+\underline{\qquad}+).\\[\baselineskip]
How do I achieve that?
\underline{\makebox[2em]{$1$}}\quad\underline{\makebox[2em]{$11$}}\bigskip

\ulmakebox{I would like} \ulmakebox{$ 1 $} and \ulmakebox{$ 11 $} to be placed at the middle of an \verb|\underline|

\end{document} 

enter image description here


When using \makebox, \width refers to the natural width of the text:

\documentclass{article}

\newcommand{\wideunderline}[2][2em]{%
  \underline{\makebox[\ifdim\width>#1\width\else#1\fi]{#2}}%
}

\begin{document}

This is short \wideunderline{11} and
\wideunderline{this is long}.

You can specify a longer one:
\wideunderline[6em]{11}

\end{document}

enter image description here

The first optional argument to \makebox should be a dimension; TeX expands token when looking for it and it will already have set \width to the natural width of the text in the mandatory argument. Since the token list starts with \ifdim, TeX does the comparison and follows either the true branch or the false branch. In the “true” case, that is when #1 (which stands for the optional argument to \wideunderline or the default value 2em if missing) is larger than \width, \width is returned, otherwise #1 is returned.

Tags:

Underline