# Macro to convert 'multi-line' text to 'single-line' (remove line breaks?)

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?

-

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

-
Many thanks for your answer, @Aditya - it seems to work fine! Cheers! – sdaau Sep 28 '11 at 7:51
Beware that this will fail if the optional argument to \\ or its *-form is used. – egreg Sep 28 '11 at 7:59
@egreg: See the modified answer. – Aditya Sep 28 '11 at 8:34

put it into a \mbox

\mbox{\myMultiLineText}

-
Many thanks for your answer, @Herbert - however, since the \makebox command creates a box to contain the text specified, if the text lines are a little longer (just edited the OP, so the example shows that), then \mbox will create just a single unbreakable line... Cheers! – sdaau Sep 28 '11 at 7:54