7

My question is incorporated in the code/output.

\documentclass{article}

\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?

\end{document}

question

3 Answers3

4

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

4

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.

egreg
  • 1,121,712
  • Hi Enrico. Can you elaborate a bit on what \ifdim\width>#1\width\else#1\fi does? – Svend Tveskæg Oct 19 '16 at 20:37
  • 1
    @SvendTveskæg Added – egreg Oct 19 '16 at 21:04
  • golly, i do find the different vertical positioning of underlined strings with or without descenders discombobulating! there's got to be a reasonable way to make them more uniform. it might be to ignore the descenders and overprint. have you seen any published recommendations about this? – barbara beeton Oct 20 '16 at 21:51
  • @barbarabeeton There are several threads on the site about it. I'd not go too much into the details; I guess the OP can do it by himself. Hint: \smash[b]{...} the argument (requires amsmath). – egreg Oct 20 '16 at 21:57
3

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

Bernard
  • 271,350