我有一段文字需要重复多次:
\newcommand{\repeatme}{text to repeat that is somewhat long and verbose}
我想给这段文字加下划线,并且不会导致水平框溢出错误。因此我尝试(使用 soul 包,就像我使用 ulem 或给它加下划线一样):
Blah blah blah, \ul{\repeatme}
这会出现这个错误:
I came across hyphenatable material enclosed in group braces,
which I can't handle. Either drop the braces or make the material
unbreakable using an \mbox (\hbox). Note that a space
also counts as possible hyphenation point. See page 4 of the manual.
我目前的解决方法是这样的:
\newcommand{\repeatmeul}{\ul{text to repeat that is somewhat long and verbose}}
和
Blah blah blah, \repeatmeul
但那失败了……错了。肯定有更优雅的方法!
答案1
希望 MWE 能够解释一切。
\documentclass{article}
\usepackage[normalem]{ulem}
\usepackage{soul}
%----------------------------------------------------
\newcommand*{\repeatme}{text to repeat that
is somewhat long and verbose} %<----without underline
%----------------------------------------------------
\newcommand{\repeatmeul}{\uline{text to repeat that
is somewhat long and verbose}} % ,---using ulem
%----------------------------------------------------
\newcommand{\repeatmeulsoul}{\ul{text to repeat that
is somewhat long and verbose}} %<--- using soul
%----------------------------------------------------
\begin{document}
This is going to be a small task for \verb|ulem| -- \repeatmeul.
And another example of using \verb|soul| --
\repeatmeulsoul. And this is with underline -- \ul\repeatme. Here we used
\verb|soul| package. And a similar one with \verb|ulem| won't work since the material inside \verb|{}|
is considered as a box -- that won't break.
\end{document}
此代码与以下输出一起工作。
结论:使用\ul\repeatme
或\repeatmeulsoul
usingsoul
包。或者使用\repeatmeul
usingulem
包。不要使用\uline\repeatme
from ulem
,否则不起作用。
附言:如果这个 MWE 对您不起作用,您应该考虑更新您的软件包。
答案2
以下肯定有效(与soul
)
\expandafter\ul\expandafter{\repeatme}
在命令内部或类似命令中展开内容总是很麻烦\ul
。另一方面,印刷界不赞成使用下划线。
使用\expandafter\ul\expandafter{\repeatme}
,宏\ul
呈现的是 的扩展\repeatme
而不是宏本身,因此这相当于写
\ul{text to repeat that is somewhat long and verbose}
但更短。