[Tex/LaTex] Macro to convert ‘multi-line’ text to ‘single-line’ (remove line breaks?)

line-breaking

Consider the following example:

\documentclass{minimal}

\def\myMultiLineText{%
Line One Line One \\ %
Line Two Line Two \\ %
Line Three Line Three \\ %
Line Four Line Four  %
}

\begin{document}

Here is multi-line: \myMultiLineText ...

\end{document}

In the document, I'd like to insert something like:

Here is single-line: \doSingleLine{\myMultiLineText} ...

… where the \doSingleLine{} macro would basically remove the line-breaks (\\) from the text stored in \myMultiLineText (so its contents would be rendered on a single line).

Could someone provide an example of such a macro?

Best Answer

\newcommand\doSingleLine[1]
    {\let\normalnewline=\\
     \let\\=\relax
     #1%
     \let\\=\normalnewline}

EDIT: As pointed by egreg, this does not take care of the optional argument of \\. Here is an alternative implementation that handles \\* and \\[10pt]:

\makeatletter
\newcommand\gobblestar
    {\futurelet\@let@token\@dogobblestar}

\def\@dogobblestar
    {\let\next=\relax
     \ifx\@let@token*%
        \def\next{\@gobble}%
     \else\ifx\@let@token[%
         \def\next{\@gobbleoptional}%
     \fi\fi
     \next}

\def\@gobbleoptional[#1]{}
\makeatother

\newcommand\doSingleLine[1]
    {\let\normalnewline=\\
     \let\\=\gobblestar
     #1%
     \let\\=\normalnewline}

Note that one cannot directly use \@ifstar as it will gobble the space at \\.