Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 8 down vote accepted
\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 \\.

share|improve this answer
    
Many thanks for your answer, @Aditya - it seems to work fine! Cheers! –  sdaau Sep 28 '11 at 7:51
2  
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}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.