\underline 的固定长度

\underline 的固定长度

我的问题包含在代码/输出中。

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

问题

答案1

这里,\funderline将具有固定的最小宽度\qquad,但如果单词的长度需要,它将增大。我通过在参数#1和的叠加堆栈下划线来实现这一点\qquad,并居中对齐。

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

在此处输入图片描述

如果我误解了这个问题,并且意图是让下划线总是长度为 2em,即使单词更长,那么这样:

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

在此处输入图片描述

答案2

使用时\makebox\width指的是文本的自然宽度:

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

在此处输入图片描述

的第一个可选参数应该\makebox是维度;TeX 在查找标记时会扩展它,并且它已经将其设置\width为强制参数中文本的自然宽度。由于标记列表以 开头\ifdim,因此 TeX 会进行比较并遵循真分支或假分支。在“真”的情况下,即当#1(代表 的可选参数\wideunderline或缺失时的默认值2em) 大于时\width\width返回 ,否则#1返回 。

答案3

给你。我添加了一个变体,基于eqparbox此变体允许所有\ulmakebox共享相同标签(默认 ULB)具有相同的下划线宽度,最小为 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} 

在此处输入图片描述

相关内容