Underline text from a macro including a line break

line-breakingmacrostext;underline

I'm trying to underline text that spans over more than one line. In principle, the ulem oder the soul package can do the trick when inputting the text directly to the respective command.

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

However, I need to input the text via a command/macro. This is because it's the title of the document, so I think it's handy to store the text/title within a command/macro. Sadly, I seem to be unable to get it to work as I want to. Here is my 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}

enter image description here

Does anyone have a solution to this?

PS: I know underlining does not look nice, but it's nothing that I want but that I have to do.

EDIT:
David's answer did actually solve my initial problem. However, I came across a follow-up problem using not only a title, but a title followed by a title separator and a subtitle. The code below is what I would ask now. But since I do indeed use LuaLatex, Marcel's answer does solve all of my problems so far. Simply using the lua-ul package and its \underLine command does the trick (solution is not shown here). I have highlighted the changes for the subtitle in the image below.

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

enter image description here

Best Answer

If you can use LuaLaTeX, then you can use my lua-ul package which works correctly with macros by default:

% !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}

enter image description here

Related Question