0

Is there a way to have line breaks inside commands that don't accept line breaks. For example,

\documentclass{article}

\begin{document}

    \mbox{line 1\\line 2}

\end{document}
  • What's the problem with \texttt{line 1\\line2}? \texttt accepts line breaks. – Werner Apr 28 '16 at 5:25
  • I don't fully understand the question... – Werner Apr 28 '16 at 5:56
  • I think he asked a general question about control sequences that do not allow for vertical mode. Along the lines of "How do I allow for multiple lines using this?", but gave an example of a control sequence that does allow for multiple lines/vertical mode. At least that's the best of my understanding of it. – A Feldman Apr 28 '16 at 6:08
  • @AFeldman Yes, that's correct. For some reason, when I used the standalone class the `\texttt{a\\b} didn't work. I fixed this to another example. – jak123 Apr 28 '16 at 6:22
4

Some examples and solutions:

\documentclass{article}
\usepackage{varwidth}
\usepackage{parskip}
\begin{document}

\texttt{line 1\\line 2}

\emph{line 1\\line 2}

But:

\mbox{line 1\\line 2}

\fbox{line 1\\line2}

Solutions:

\fbox{\parbox{5cm}{line 1\\line 2}} % or environment minipage

% varwidth is like minipage, but truncates the lines if possible:
\fbox{\begin{varwidth}{5cm}line 1\\line 2\end{varwidth}}

\fbox{\begin{tabular}{ll}line 1\\line 2\end{tabular}}

\end{document}

Result

  • Nice answer, even though the question was perhaps questionable. – A Feldman Apr 28 '16 at 5:44
3

By putting it into a box that allows line breaks. For instance like a \vbox, you could also use a \vtop box to allow for this. The problem is that you need to get out of horizontal mode where a normal \hbox only allows single lines up to \hsize in width.

\documentclass{article}

\begin{document}

        \mbox{\vbox{\noindent line1\\line2}}

 \end{document}

![enter image description here

but, as Werner pointed out, using \texttt there is no need to use anything because it does allow paragraphs.

But, using a \fbox, a command which does not allow for multiple paragraphs (thanks to Heiko Oberdiek for his below examples):

\documentclass{article}

\begin{document}

    \fbox{\vbox{\noindent line 1\\line 2}}

\end{document}

enter image description here

The same result if you use a \vtop box:

    \fbox{\vtop{\noindent line 1\\line 2}}

enter image description here

  • 2
    \texttt accepts an argument. In your case you're only supplying the first token l (which is evident from your output). Also, \texttt accepts line breaks, so there's no need for \vbox. – Werner Apr 28 '16 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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