2

I put multiple short sections together to a book. I want a two column layout, but a new section should always span both columns. The sections are generated from markdown files with pandoc.

I tried @xports approach with \multicol, but I do not want to edit each section title and would prefer to define the behavior in the preamble.

\documentclass[twocolumn=true]{scrbook}
\usepackage{lipsum}

\begin{document}
\chapter{Chapter 1}
\section{Section 1}
\lipsum[1-2]

\section{Section 2}
\lipsum[3-4]

\section{Section 3}
\lipsum[1-4]

\chapter{Chapter 2}
\section{Section 1}
\lipsum[1-2]

\section{Section 2}
\lipsum[3-4]

\section{Section 3}
\lipsum[1-5]

\end{document}

3 Answers 3

3

Welcome to TeX.SX! You could make use of the cuted package like in the following example. It may be possible, I think, that LaTeX doesn't always prevent page breaks between the section title and the following paragraph. Maybe some adjustment is needed.

\documentclass[twocolumn=true]{scrbook}
\usepackage{lipsum, cuted}

\renewcommand{\sectionlinesformat}[4]{%
\begin{strip}\thesection\autodot\enskip #4\end{strip}%
}

\begin{document}
\chapter{Chapter 1}

\section{Section 1}
\lipsum[1-3]

\section{Section 2}
\lipsum[3-6]

\section{Section 3}
\lipsum[1-4]

\chapter{Chapter 2}
\section{Section 1}
\lipsum[1-2]

\section{Section 2}
\lipsum[3-4]

\section{Section 3}
\lipsum[1-5]

\end{document}

First page of the output:

enter image description here


Edit: If you don't want to use the same styling for subsection headings, you can use the following to apply the above settings only to sections. I also changed the code so that the starred version of section title won't get a section number:

\documentclass[twocolumn=true]{scrbook}
\usepackage{lipsum, cuted}

\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
\Ifstr{#1}{section}{%
\begin{strip}\@hangfrom{\hskip #2#3}{#4}\end{strip}%
}{%
\@hangfrom{\hskip #2#3}{#4}%
}%
}
\makeatother

\begin{document}
\chapter{Chapter 2}
\section{Section 1}
\lipsum[1-2]

\subsection{Subsection 1}
\lipsum[2]

\section{Section 2}
\lipsum[3-4]

\section{Section 3}
\lipsum[1-5]

\end{document}

The first page of the output renders like this:

enter image description here

7
  • Your redefinition of \sectionlinesformat gives unexpected results for e.g. subsection headings or unnumbered sections headings, because the section number would be used for them, too. Additionally environment strip would be used for e.g. subsections, too.
    – esdd
    Apr 7, 2021 at 20:10
  • @esdd You are right. See my edit. You then need to restrict the re-formatting so that it only applies to section headers. I am not sure why the spacing sometimes seems to be a bit off, though. Apr 7, 2021 at 21:24
  • Why you do not use the original definition for the section level in environment strip, i.e. \begin{strip}\@hangfrom{\hskip #2#3}{#4}\end{strip}?
    – esdd
    Apr 7, 2021 at 22:00
  • 3
    You can load package needspace. And you could use the hooks for the headings provided by the class scrbook: \AddtoDoHook{heading/begingroup/section}{\startonecolumn} and \AddtoDoHook{heading/endgroup/section}{\stoponecolumn} with \newcommand*{\startonecolumn}[1]{\begin{strip}\needspace{4\baselineskip}} and \newcommand*{\stoponecolumn}[1]{\end{strip}}.
    – esdd
    Apr 7, 2021 at 22:08
  • 1
    I think @esdd's solution is indeed great and they should post it as an answer! But @orangerkater is right: somehow, neither \nopagebreak nor \needspace can really prevent the headings from being seperated in certain situations. Apr 8, 2021 at 7:01
2

Here is a method that will work for your MWE.

% sectionprob.tex  SE 591865
%\documentclass[twocolumn=true]{scrbook}
\documentclass{scrbook}
\usepackage{multicol}
\usepackage{lipsum}

% \section not in multicolumn
\let\savesection\section
\renewcommand{\section}[1]{%
  \end{multicols}
  \savesection{#1}
  \begin{multicols}{2}}

% \chapter not in multicolumn
\let\savechapter\chapter
\renewcommand{\chapter}[1]{%
  \end{multicols}
  \savechapter{#1}
  \begin{multicols}{2}}

\begin{document}
\begin{multicols}{2}  % start 2 columns
\chapter{Chapter 1}

\section{Section 1}
\lipsum[1-2]

\section{Section 2}
\lipsum[3-4]

\section{Section 3}
\lipsum[1-4]

\chapter{Chapter 2}
\section{Section 1}
\lipsum[1-2]

\section{Section 2}
\lipsum[3-4]

\section{Section 3}
\lipsum[1-5]

\end{multicols} % end 2 columns
\end{document}

enter image description here

Strangely to me regular \chapter in a 2 column multicolumn gets set above the second column.

2
  • Your redefinition of \chapter and \section breaks, if their starred version or the optional argument would be used. \tableofcontents can not be used.
    – esdd
    Apr 7, 2021 at 20:14
  • @esdd My solution works for the given MWE. I admit that It will not work for starred versions or those with optional parameters but the OP gave no indication that they might be used. I felt that I had spent enough time on this without going into all possible (but possibly unused) versions. --- GOM Apr 9, 2021 at 18:00
2

As proposed in a comment by @esdd I used a hook for the headings with cuted and needspace:

\documentclass[twocolumn=true]{scrbook}
\usepackage{lipsum}

\usepackage{cuted, needspace}
\newcommand*{\startonecolumn}[1]{\begin{strip}\needspace{4\baselineskip}}
\newcommand*{\stoponecolumn}[1]{\end{strip}}
\AddtoDoHook{heading/begingroup/section}{\startonecolumn}
\AddtoDoHook{heading/endgroup/section}{\stoponecolumn}
% \AddtoDoHook{heading/begingroup/subsection}{\startonecolumn}
% \AddtoDoHook{heading/endgroup/subsection}{\stoponecolumn}

\begin{document}
\chapter{Chapter 1}
\section{Section 1}
\lipsum[1]
\subsection{Subsection 1}
\lipsum[2]

\section{Section 2}
\lipsum[3-4]

\section{Section 3}
\lipsum[1-4]

\chapter{Chapter 2}
\section{Section 1}
\lipsum[1-2]

\section{Section 2}
\lipsum[3-4]

\section{Section 3}
\lipsum[1-5]

\end{document}

I also added the hooks for spanning subsections (commented out above).

mwe output page 1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .