# Automatically suppress multicol layout if available space is too narrow

I am using pdfLaTeX (in MikTex) to write technical articles for a company newsletter.

Inside any one article I switch between two column layout for descriptive text and one column layout for code segments, figures, tables etc.

%newsletterA4.text
\documentclass[a4paper,12pt]{article}
\usepackage{multicol}
\usepackage{lipsum}
\begin{document}
\input{editorial.tex}
\input{article1.tex}
\input{article2.tex}
\end{document}


A typical article file:

% article1.tex
Headings go over the full text width

\begin{multicols}{2}
\lipsum
\end{multicols}

\begin{verbatim}
code listing (or figure or image go over the full text width)
\end{verbatim}

\begin{multicols}{2}
\lipsum
\end{multicols}


The articles I write, are occasionally published also in different media

1. with smaller \textwidth and/or
2. inside a (narrow) minipage

In future all articles may also be published in

1. html on the company's intranet

In those scenarios I prefer a single column layout. Currently, I run a script creating an alternative version of the article file, commenting out the \begin{multicols} and \end{multicols}. Not exactly elegant

Is there a way that LaTeX takes care of this automatically?
Something like:
If \textwidth is smaller than some value then ignore the multicol environment.

Does a minipage have its own \textwidth parameter so the test would be possible as well?

I admit, I have not looked into the latex2html topic yet (no web presence yet) but I thought I mention it in case it influences the answers. I would appreciate a hint how I could "ignore" multicol for publishing in html.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. May 9 '15 at 11:24

You can define a new environment mymulticols in this way

\newenvironment{mymulticols}[1]
{\ifdim\textwidth<\mylength\else\begin{multicols}{#1}\fi}
{\ifdim\textwidth<\mylength\else\end{multicols}\fi}


so that if \textwidth < \mylength, multicols is not used, otherwise it is.

Set the length of \mylength to the needed value. In this example I've set

\newlength{\mylength}
\setlength{\mylength}{20cm}


and since \textwidth < 20cm in this case, multicols is discarded.

MWE:

\documentclass[a4paper,12pt]{article}
\usepackage{multicol}
\usepackage{lipsum}

\newlength{\mylength}
\setlength{\mylength}{20cm}

\newenvironment{mymulticols}[1]
{\ifdim\textwidth<\mylength\else\begin{multicols}{#1}\fi}
{\ifdim\textwidth<\mylength\else\end{multicols}\fi}

\begin{document}
\begin{mymulticols}{2}
\lipsum
\end{mymulticols}

\begin{verbatim}
code listing (or figure or image go over the full text width)
\end{verbatim}

\begin{mymulticols}{2}
\lipsum
\end{mymulticols}
\end{document}


Output:

If you change the value of \mylength to 10cm you will get:

• By the way: It works on minipages as well! May 10 '15 at 10:31
• @Phil You're welcome. Inside minipages the value of \textwidth changes accordingly to the width of the minipage, so it works there too :-) May 10 '15 at 19:53