如何为宏中的文本(包括换行符)添加下划线?

如何为宏中的文本(包括换行符)添加下划线?

我正在尝试为多行文本添加下划线。原则上,当将文本直接输入到相应命令时,ulem 或 soul 包可以完成此操作。

\ul{some text with linebreak works well} % soul
\uline{some text with linebreak works well} % ulem

但是,我需要通过命令/宏输入文本。这是因为它是文档的标题,所以我认为将文本/标题存储在命令/宏中很方便。遗憾的是,我似乎无法让它按我想要的方式工作。这是我的 MWE。

\documentclass{scrreport}
\usepackage[showframe]{geometry}
\usepackage{soul, ulem}

%%% In class definition:
% Default value
\def\printtitle{default title}
% Provide command for user to set own title
\newcommand{\settitle}[1]{\def\printtitle{#1}}%

%%% In preamble to the document --> visible to the user
%\settitle{my own title}
\settitle{long title for testing long title for testing long title for testing long title for testing long title for testing }

\begin{document}
    I want the title to be underlined, even with a line break. The soul package (\verb|\ul|) or the ulem package (\verb|\uline|) can do this: \newline
    \ul{long title for testing long title for testing long title for testing long title for testing long title for testing} \newline
    \uline{long title for testing long title for testing long title for testing long title for testing long title for testing} 
    
    \vspace{1cm}
    However, I need the title at multiple places --> storing the title as a macro seems like a good idea. However, using the macro inside \ul{} from the soul package doesn't work: \newline
    \ul{\printtitle} \newline
    \uline{\printtitle}
\end{document}

在此处输入图片描述

有人有解决这个问题的方法吗?

附言:我知道下划线看起来不太好看,但我并不介意但我去做。

编辑: David 的回答确实解决了我最初的问题。但是,我遇到了一个后续问题,它不仅使用标题,还使用标题后跟标题分隔符和副标题。下面的代码是我现在要问的。但由于我确实使用了 LuaLatex,Marcel 的回答确实解决了我迄今为止的所有问题。只需使用 lua-ul 包及其\underLine命令即可解决问题(此处未显示解决方案)。我在下图中突出显示了副标题的更改。

\documentclass{scrreport}
\usepackage[showframe]{geometry}
\usepackage{soul, ulem}

%%% In class definition:
% Default value
\def\printtitle{default title}
\def\printtitlesep{:\enspace}
\def\printsubtitle{}
% Provide command for user to set own title
\newcommand{\settitle}[1]{\def\printtitle{#1}}
\newcommand{\settitlesep}[1]{\def\printtitlesep{#1}}
\newcommand{\setsubtitle}[1]{\def\printsubtitle{#1}}
% If no subtitle was given by the user, set the title separator empty. In any case, provide a command for the full title
\AtBeginDocument{%
    \ifx \printsubtitle\empty \settitlesep{} \else \fi
    \def\printfulltitle{\printtitle\printtitlesep\printsubtitle}
}

%%% In preamble to the document --> visible to the user
\settitle{long title for testing long title for testing long title for testing long title for testing long title for testing}
\settitlesep{\enspace\textendash\enspace}
\setsubtitle{some subtitle}

\begin{document}
    I want the title to be underlined, even with a line break. The soul package (\verb|\ul|) or the ulem package (\verb|\uline|) can do this: \newline
    \ul{long title for testing long title for testing long title for testing long title for testing long title for testing -- some subtitle} \newline
    \uline{long title for testing long title for testing long title for testing long title for testing long title for testing -- some subtitle} 
    
    \vspace{1cm}
    However, I need the title at multiple places --> storing the title as a macro seems like a good idea. However, using the macro inside \verb|\ul{}| from the soul package doesn't work: \newline
    \ul{\printfulltitle} \newline
    \uline{\printfulltitle}
\end{document}

在此处输入图片描述

答案1

如果您可以使用 LuaLaTeX,那么您可以使用我的lua-ul包,该包默认可以正确与宏一起使用:

% !TEX program = lualatex
\documentclass{scrreport}
\usepackage[showframe]{geometry}
\usepackage{lua-ul, soul, ulem}

%%% In class definition:
% Default value
\def\printtitle{default title}
% Provide command for user to set own title
\newcommand{\settitle}[1]{\def\printtitle{#1}}%

%%% In preamble to the document --> visible to the user
%\settitle{my own title}
\settitle{long title for testing long title for testing long title for testing long title for testing long title for testing }

\begin{document}
I want the title to be underlined, even with a line break. The soul package (\verb|\ul|) or the ulem package (\verb|\uline|) and for Lua\LaTeX\ also the lua-ul package (\verb|\underLine|) can do this: \newline
    \ul{long title for testing long title for testing long title for testing long title for testing long title for testing} \newline
    \uline{long title for testing long title for testing long title for testing long title for testing long title for testing} \newline
    \underLine{long title for testing long title for testing long title for testing long title for testing long title for testing} 
    
    \vspace{1cm}
    However, I need the title at multiple places --> storing the title as a macro seems like a good idea. However, this only works with lua-ul's \verb|\underLine|: \newline
    \underLine{\printtitle}
\end{document}

在此处输入图片描述

答案2

在此处输入图片描述

您可以在将结果传递给下划线函数之前扩展该命令。

\documentclass{scrreport}
\usepackage[showframe]{geometry}
\usepackage{soul, ulem}

%%% In class definition:
% Default value
\def\printtitle{default title}
% Provide command for user to set own title
\newcommand{\settitle}[1]{\def\printtitle{#1}}%

%%% In preamble to the document --> visible to the user
%\settitle{my own title}
\settitle{long title for testing long title for testing long title for testing long title for testing long title for testing }

\begin{document}
    I want the title to be underlined, even with a line break. The soul package (\verb|\ul|) or the ulem package (\verb|\uline|) can do this: \newline
    \ul{long title for testing long title for testing long title for testing long title for testing long title for testing} \newline
    \uline{long title for testing long title for testing long title for testing long title for testing long title for testing} 
    
    \vspace{1cm}
    However, I need the title at multiple places --> storing the title as a macro seems like a good idea. However, using the macro inside \ul{} from the soul package doesn't work: \newline
    \expandafter\ul\expandafter{\printtitle} \newline
    \expandafter\uline\expandafter{\printtitle}
\end{document}

相关内容