6

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}
4
  • What's the problem with \texttt{line 1\\line2}? \texttt accepts line breaks.
    – Werner
    Apr 28, 2016 at 5:25
  • I don't fully understand the question...
    – Werner
    Apr 28, 2016 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, 2016 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, 2016 at 6:22

2 Answers 2

8

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

1
  • Nice answer, even though the question was perhaps questionable.
    – A Feldman
    Apr 28, 2016 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
  • 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, 2016 at 5:32
  • \texttt changes the fonts using in the document.
    – Manjoy Das
    Oct 29, 2022 at 20:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .