4

I prefer to use \begin{multicols}{2} over \documentclass[12pt,twocolumn]{article} so I can control when to have 2 columns and when not.

The problem comes when using \begin{Verbatim}....\end{Verbatim}.

With \documentclass[12pt,twocolumn]{article} verbatim automatically wraps inside each column and will not overflow to the other column. But when using \begin{multicols}{2} it does not do this, and it will overflow to the column next to it.

MWE:

\documentclass[12pt]{article}
\usepackage{multicol}
\setlength{\columnsep}{20pt}
\setlength{\columnseprule}{0.01pt}
\usepackage{fancyvrb}

\begin{document}
\begin{multicols}{2}
\begin{Verbatim}
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
\end{Verbatim}
\end{multicols}

\end{document}

MWE for twocolumn

\documentclass[12pt,twocolumn]{article}

\setlength{\columnsep}{20pt}
\setlength{\columnseprule}{0.01pt}
\usepackage{fancyvrb}

\begin{document}
\begin{Verbatim}
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
\end{Verbatim}
\end{document}

side-by-side

enter image description here

The solution I came up with is to put Verbatim in minipage, using columnwidth as size for width of minipage, and that worked to some extent, but for some reason, I can't make the width of the minipage smaller than column width, as verbatim still goes a little over.

When I write say 0.6\columnwidth it has no effect on the result. It looks the same for any value I tried.

\documentclass[12pt]{article}
\usepackage{multicol}
\setlength{\columnsep}{20pt}
\setlength{\columnseprule}{0.01pt}
\usepackage{fancyvrb}

\begin{document}
\begin{multicols}{2}
\begin{minipage}{0.6\columnwidth}
\begin{Verbatim}
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
\end{Verbatim}
\end{minipage}
\end{multicols}

\end{document}

enter image description here

question is: What is the best way to handle Verbatim in multicols environment?

2
  • Your comparison is not fair; add \setlength{\textwidth}{469pt} to the multicol version and you'll see that the output is the same (except for the balancing of columns).
    – egreg
    Commented Jun 21, 2013 at 10:31
  • 1
    I don't understand your question, As your images show Verbatim (like the standard verbatim`) does not wrap lines, the line breaks are as in the original source, so it is up to you to use a suitable font size. The behaviour seems no different in twocolumn or multicol Commented Jun 21, 2013 at 10:34

1 Answer 1

8

Your assumptions are wrong. In no case does Verbatim is "wrapped" to the column width. Example:

\documentclass[12pt,twocolumn]{article}

\setlength{\columnsep}{20pt}
\setlength{\columnseprule}{0.01pt}
\usepackage{fancyvrb}

\begin{document}
\begin{Verbatim}
blablabla blablabla blablabla blablabla blablablx
blablabla blablabla blablabla blablabla blablablx
blablabla blablabla blablabla blablabla blablablx
blablabla blablabla blablabla blablabla blablablx
blablabla blablabla blablabla blablabla blablablx
\end{Verbatim}
\end{document}

enter image description here

As you can see, the lines go far beyond the separating rule.

The lines in Verbatim are never split across lines, so it's up to you to provide that they don't overfill the column width.

Your comparison is also wrong because you don't keep into account that the option twocolumn to article causes the text width to be increased to 469pt rather than the 390pt with the onecolumn (default) option. If you try your multicol example and extend the text width, also using multicols* in order not to do the column balancing,

\documentclass[12pt]{article}
\usepackage{multicol}
\setlength{\columnsep}{20pt}
\setlength{\columnseprule}{0.01pt}
\usepackage{fancyvrb}

\setlength{\textwidth}{469pt}

\begin{document}
\begin{multicols*}{2}
\begin{Verbatim}
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
\end{Verbatim}
\end{multicols*}

\end{document}

you get identical output, as shown below.

enter image description here

If you want a verbatim mode that also break lines, use listings:

\documentclass[12pt]{article}
\usepackage{multicol}
\setlength{\columnsep}{20pt}
\setlength{\columnseprule}{0.01pt}
\usepackage{listings}
\lstset{basicstyle=\ttfamily,
  breaklines=true}

\setlength{\textwidth}{469pt}

\begin{document}
\begin{multicols*}{2}
\begin{lstlisting}
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
blablabla blablabla blablabla blablabla
\end{lstlisting}
\end{multicols*}

\end{document}

enter image description here

You must log in to answer this question.

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