9

I have defined the following shortcut for some text

\newcommand{\sometext}{text\\text\\text}

Now I would like to use this in some other place but with the linebreaks removed. So is there an easy way to do it (e.g. something like "\remove_linebreaks{\sometext}")?

Moked
  • 1,530

1 Answers1

11

This will change \\ into a space:

\newcommand{\removelinebreaks}[1]{%
  \begingroup\def\\{ }#1\endgroup}

\removelinebreaks{\sometext}
egreg
  • 1,121,712
  • \\ has an optional star and an optional argument. Something like \def\\{\@ifstar {\@gobbleopt}{\@gobbleopt}}\providecommand*\@gobbleopt[1][]{} would handle these too. – Schweinebacke Nov 23 '11 at 13:56
  • @Schweinebacke How is this chunk supposed to work? Compilation fails like this:

    ! LaTeX Error: Missing \begin{document}. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.65 ...bbleopt}{\@gobbleopt}}\providecommand*\@go bbleopt[1][]{} ?

    – Frotz Oct 01 '18 at 20:29
  • @Frotz: Use it instead of \def\\{ } in egreg's code, but → https://tex.stackexchange.com/questions/8351/what-do-makeatletter-and-makeatother-do or ask a new question. – Schweinebacke Oct 02 '18 at 06:23