\renewcommand or \renewenvironment with multicols

I'm trying to create a horizontal white space in my multicolumn environment every time I start a new subsection.

It stops people from having to trawl their eyes up and down the page between different content, splitting the page in a more readable fashion.

However, I keep getting the following error:

        ! Missing \endcsname inserted.
\let
l.83 \renewenvironment{\subsection}
{\end{multicols}}{\start{multicols}{2}}
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.


Minimal example:

\documentclass[11pt]{article}
\usepackage{multicol}

%\let\origsubsection\subsection
\renewcommand{\subsection}{\end{multicols}\subsection\start{multicols}{2}}

\begin{document}

\section{Something here}
Filler text1

\begin{multicols}{2}

\subsection{This is a test1}  %\endMulti startsub  \beginMulti
Filler text2
\subsection{This is a test2}  %\endMulti startsub  \beginMulti
Filler text3

\end{multicols}
\end{document}

• Please construct a compilable minimal working example (MWE) starting from \documentclass... until \end{document}. – masu Dec 10 '13 at 17:40
• btw: you're creating an environment but using a macro. Is this intentional?! – masu Dec 10 '13 at 17:41
• @masu Okay, done sorry. And also yes, though I take it this is bad... I did try using \renewenvironment but the errors were the same – tetris11 Dec 10 '13 at 17:48
• What are you trying to achieve? Your question is an XY problem for me right now. – masu Dec 10 '13 at 17:57
• ah never mind, I'll just do \end{multicols}\subsection{blah}\start{multicols}{2} manually for now. It's only really needed a few times anyhow. – tetris11 Dec 10 '13 at 18:22

You had defined \subsection in terms of itself which would have been an infinite loop, but you also used a command \start that is undefined. You intended something more like this:

\documentclass[11pt]{article}
\usepackage{multicol}

\let\origsubsection\subsection
\renewcommand{\subsection}[1]{\end{multicols}\origsubsection{#1}\begin{multicols}{2}}

\begin{document}

\section{Something here}
Filler text1

\begin{multicols}{2}

\subsection{This is a test1}  %\endMulti startsub  \beginMulti
Filler text2
\subsection{This is a test2}  %\endMulti startsub  \beginMulti
Filler text3

\end{multicols}
\end{document}

• Perfect! I think I did try doing something like this but I must have missed something. Thank you! – tetris11 Dec 10 '13 at 19:38

Idea is the same as in the other answer, however, the support for the starred varian and the optional argument was added:

\makeatletter
\let\subsection@old\subsection
\def\subsection@pre{\end{multicols}}
\def\subsection@post{\begin{multicols}{2}}
\newcommand\subsection{\@ifnextchar*\subsection@star\subsection@nostar}
\def\subsection@star*#1{\subsection@pre\old@subsection*{#1}\subsection@post}
\def\subsection@nostar{\@ifnextchar[\subsection@br\subsection@nobr}
\def\subsection@br[#1]#2{\subsection@pre\old@subsection[#1]{#2}\subsection@post}
\def\subsection@nobr#1{\subsection@pre\old@subsection{#1}\subsection@post}
\makeatother


Sorry for the (a bit) cryptic code, I don't like using \@dblarg and \@ifstar

• (+1) essential to mention the starred variant (and the optional argument) – masu Dec 10 '13 at 20:04

Is this what you wanted?! I hope so. If you also want to change the spacing before section and subsection check this answer.

I assumed that you have an article (and don't have any chapters). I also assumed that you want the entire stuff to be twocolumn, and stop it at every section and subsection command.

Instead of the \begin{multicols} and \end{multicols} notation I used the macro calls \multicols and \endmulticols.

\documentclass[11pt]{article}

\usepackage{lipsum} % demo only

\usepackage{multicol}

\AtBeginDocument{\begin{multicols}{2}}
\AtEndDocument{\end{multicols}}

\makeatletter
\let\section@orig\section
\let\subsection@orig\subsection
\renewcommand\section[1]{\endmulticols\section@orig{#1}\multicols{2}}
\renewcommand\subsection[1]{\endmulticols\subsection@orig{#1}\multicols{2}}
\makeatother

\begin{document}

\show\section
\show\subsection

\section{Section}

\lipsum[1]

\subsection{Subsection}

\lipsum[2]

\subsection{Subsection}

\lipsum[3]

\section{Section}

\lipsum[4]

\subsection{Subsection}

\lipsum[5]

\subsection{Subsection}

\lipsum[6]

\end{document}


Note: lipsum is only present for the demo text.

I've also tried to use xpatch, but instead of a solution I've created a problem... here's a partial code. I don't know how to fix the issue... \protect doesn't seem to solve anything other than removing the errors... Related topic: What is the difference between Fragile and Robust commands?

\documentclass[11pt]{article}

\usepackage{lipsum} % demo only

\usepackage{multicol}
\usepackage{xpatch}

\AtBeginDocument{\begin{multicols}{2}}
\AtEndDocument{\end{multicols}}

\xpretocmd{\section}{\endmulticols}{}{}
\xapptocmd{\section}{\protect\multicols{2}}{}{}
\xpretocmd{\subsection}{\endmulticols}{}{}
\xapptocmd{\subsection}{\protect\multicols{2}}{}{}

\begin{document}

\section{Section}

\lipsum[1]

\subsection{Subsection}

\lipsum[2]

\subsection{Subsection}

\lipsum[3]

\section{Section}

\lipsum[4]

\subsection{Subsection}

\lipsum[5]

\subsection{Subsection}

\lipsum[6]

\end{document}

• you've loaded xpatch but not used it, but redefined in the classic \let\subsection@orig\subsection manner instead (using xpatch might have been an interesting alternative to the other annwers) \protect isn't likely to be needed there (you are not likely to use \subsection in a moving argument.) – David Carlisle Dec 10 '13 at 19:27
• @DavidCarlisle I've tried that, that's why xpatch remained in the code... but \xapptocmd didn't seem to work easily (without messing with the internals of the \section definition). It messed the titles, and need more tweeking around than this. So this seemed to be more appropriate. – masu Dec 10 '13 at 19:39
• This is very nice though, exactly what I wanted. @masu - Could you expand a bit on how protect works? – tetris11 Dec 10 '13 at 19:43
• @tetris11 I've added a reference instead – masu Dec 10 '13 at 19:50
• as David mentioned, you don't really need the protect there... – masu Dec 10 '13 at 20:08