3

I have a whole document written up, and I would now like to format it in two columns.

However, I rely heavily on headers like \section, and would like these headers to not be in the columns. In other words, the two-column text should end in the middle of the page, the section title should appear, then the two-column text should start back up again.

I can get the desired effect by specifically ending the multicol environment, then starting it again:

...
\end{multicol}

\begin{multicol}{2}[\section{Section Name Here}]
...

But it'll be tedious to apply this to every single heading in my document, and then to go through and modify every instance if I change the number of columns.

Is there a way to make this happen automatically, perhaps by modifying the definitions of \section, \subsection, and so on, or using titlesec?

MWE:

\documentclass{article}

\usepackage{lipsum}
\usepackage{multicol}

\begin{document}

\begin{multicols}{2}
\section{First section}
\lipsum[1-3]
\section{Second section}
\lipsum[4-6]
\section{Third section}
\lipsum[7-9]
\end{multicols}

\end{document}
1
  • Oops, didn't notice the brackets. May 31 '19 at 23:25
3

It wasn't quite as simple as I first thought, although the test of \@currenvir was probably unneeded.

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

\let\oldsection=\section
\makeatletter
\def\section{\@ifnextchar*\oldsection\newsection}% handle \section*

\newcommand{\newsection}[2][\empty]{% #1=short title (optonsl), #2=title
  \def\test{multicols}%
  \ifx\@currenvir\test
    \end{multicols}\begin{multicols}{2}[{%
      \ifx\empty#1\relax
        \oldsection{#2}%
      \else
        \oldsection[#1]{#2}%
      \fi}]%
  \else
    \ifx\empty#1\relax
      \oldsection{#2}%
    \else
      \oldsection[#1]{#2}%
    \fi
  \fi}
\makeatother

\begin{document}
\tableofcontents

\begin{multicols}{2}
\section{First section}
\lipsum[1-3]
\section[Sec 2]{Second section}
\lipsum[4-6]
\section{Third section}
\lipsum[7-9]
\end{multicols}

\end{document}
3
  • Perfect, this is exactly what I needed! Thank you!
    – Draconis
    Jun 1 '19 at 2:13
  • Unnumbered sections (\section*) don't span the two columns with this implementation. From the question, I had understood they should, but it was rather unclear in this respect.
    – frougon
    Jun 1 '19 at 8:19
  • @frougon - I only bothered with \section* since I added the \tableofcontents to test the short title. Jun 1 '19 at 13:07
3

You can use this:

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

\ExplSyntaxOn

\cs_new_protected:Npn \draconis_start_multicols_env:nn #1#2
  {
    \begin{multicols}{#1}[{#2}]
  }

\cs_generate_variant:Nn \draconis_start_multicols_env:nn { nV }

\tl_new:N \l__draconis_sectioning_command_tl

\cs_new_protected:Npn \draconis_startsection:Nnnn #1#2#3#4
  {
    \tl_if_eq:nnF {#4} { first } { \end{multicols} }
    \tl_set:Nn \l__draconis_sectioning_command_tl { \section }

    \tl_put_right:Nx \l__draconis_sectioning_command_tl
      {
        \IfBooleanT {#1} { * }                 % star form
        \IfValueT {#2} { [ \exp_not:n {#2} ] } % title for toc and headers
        { \exp_not:n {#3} }                    % title
      }

    % This 2 is the number of columns (passed to 'multicols')
    \draconis_start_multicols_env:nV {2} \l__draconis_sectioning_command_tl
  }

\NewDocumentCommand \startsection { s o m O{} }
  {
    \draconis_startsection:Nnnn #1 {#2} {#3} {#4}
  }

\NewDocumentCommand \stoplastsection { }
  {
    \end{multicols}
  }

\ExplSyntaxOff

\begin{document}

\startsection{First section}[first]
\lipsum[1-3]

\startsection[Short title]{Second section}
\lipsum[4-6]

\startsection*{Unnumbered section}
\lipsum[7-9]

\startsection{Third section}
\lipsum[10-12]

\stoplastsection

\end{document}

My \startsection works like \section (including the star form), except it has the multicols wrapping you want and accepts an optional, final argument: if the final argument is first, then \startsection doesn't insert \end{multicols} (otherwise, that is the first thing it puts in the input stream).


Page 1

Page 1


Page 2

Page 2


Page 3

Page 3

If you don't mind:

  • starting an empty multicols environment before the first section (hidden by the implementation, but done);

  • repeating the number of columns in several parts of the document (I had understood from your question that you wanted to avoid that, but apparently not), and;

  • explicitly opening and closing the multicols environment after \begin{document},

it is easy to adapt the above solution to look precisely the way you like:

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

\ExplSyntaxOn

\cs_set_eq:NN \draconis_orig_section \section

\cs_new_protected:Npn \draconis_start_multicols_env:nn #1#2
  {
    \begin{multicols}{#1}[{#2}]
  }

\cs_generate_variant:Nn \draconis_start_multicols_env:nn { nV }

\tl_new:N \l__draconis_sectioning_command_tl

\cs_new_protected:Npn \draconis_startsection:Nnnn #1#2#3#4
  {
    \tl_if_eq:nnF {#4} { first } { \end{multicols} }
    \tl_set:Nn \l__draconis_sectioning_command_tl { \draconis_orig_section }

    \tl_put_right:Nx \l__draconis_sectioning_command_tl
      {
        \IfBooleanT {#1} { * }                 % star form
        \IfValueT {#2} { [ \exp_not:n {#2} ] } % title for toc and headers
        { \exp_not:n {#3} }                    % title
      }

    % This 2 is the number of columns (passed to 'multicols')
    \draconis_start_multicols_env:nV {2} \l__draconis_sectioning_command_tl
  }

\NewDocumentCommand \startsection { s o m O{} }
  {
    \draconis_startsection:Nnnn #1 {#2} {#3} {#4}
  }

% Replace \section with \startsection (ugh...)
\cs_set_eq:NN \section \startsection

\ExplSyntaxOff

\begin{document}

\begin{multicols}{2}
\section{First section}
\lipsum[1-3]

\section[Short title]{Second section}
\lipsum[4-6]

\section*{This is the title of an unnumbered section}
\lipsum[7-9]

\section{Third section}
\lipsum[10-12]
\end{multicols}

\end{document}

Note that this implementation does work with \section*, as shown below:

Screenshot of unnumbered section

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.