[Tex/LaTex] one-column multicol environment

multicol

I'm writing some xsl that takes xml source and outputs a .tex file. The source has an element that is meant to frequently become a multicols environment in the .tex output, and it has a cols attribute to specify how many columns there are.

I'd like the default to be one column if no cols attribute has been specified. But it appears that \begin{multicols}{1} still produces two-column output. I could write the xsl in a casewise way and skip the multicols environment altogether when cols is not specified (or is specified as 1), but for the sake of consistency I'd prefer not to.

Is there any LaTeX way to get the multicols environment to produce one-column output?

Best Answer

This solution is inspired by Werner's, but it supports the optional argument to \begin{multicols} and, more importantly, doesn't collect the whole contents in a single swoop, which is best to avoid, if possible. It also doesn't require changing the environment's name.

\documentclass{article}
\usepackage{multicol,lipsum,xparse}

\let\multicolmulticols\multicols
\let\endmulticolmulticols\endmulticols

\RenewDocumentEnvironment{multicols}{mO{}}
 {%
  \ifnum#1=1
    #2%
  \else % More than 1 column
    \multicolmulticols{#1}[#2]
  \fi
 }
 {%
  \ifnum#1=1
  \else % More than 1 column
    \endmulticolmulticols
  \fi
 }

\begin{document}

\begin{multicols}{2}[\section{Title in single column}]
\lipsum[1-2]
\end{multicols}

\begin{multicols}{1}
\lipsum[1-2]
\end{multicols}

\end{document}

enter image description here

Related Question