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
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}
question is: What is the best way to handle Verbatim
in multicols environment?
Best Answer
Your assumptions are wrong. In no case does
Verbatim
is "wrapped" to the column width. Example: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
toarticle
causes the text width to be increased to 469pt rather than the 390pt with theonecolumn
(default) option. If you try yourmulticol
example and extend the text width, also usingmulticols*
in order not to do the column balancing,you get identical output, as shown below.
If you want a verbatim mode that also break lines, use
listings
: