1

Is it possible to add the column of an section/subsection in an multicol environment to the table of contents?

The table of content looks like this in the moment

1. Headline 1 ......... 1 % Shows only the page
2. Headline 2 ......... 1
3. Headline 3 ......... 1

But I want it to look like this

1. Headline 1 ......... 1.1 % <page>.<column>
2. Headline 2 ......... 1.3
3. Headline 3 ......... 1.5

Here is a minimal example, which has to be extended:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, landscape, margin=3cm]{geometry}
\usepackage{lipsum} 
\usepackage{multicol}

\begin{document}
\begin{multicols*}{5}

\tableofcontents
\section{Headline 1}
\lipsum[1-3]

\section{Headline 2}
\lipsum[4-6]

\section{Headline 3}
\end{multicols*}

\end{document}
  • welcome to tex.se! it is not clear what you like to obtain. your mwe works (as expected), however, section out of multicols* start new page ... what is your real problem? – Zarko Feb 9 '18 at 20:26
  • If you want to put the sections in one column and the subsections in another (overlappping) you coulod do it with paracol, but you will need to customize \l@section and \l@subsection to use \leftcolumn and \rightcolumn. – John Kormylo Feb 9 '18 at 20:57
  • The table of contents shows only the pages, but I want to add the corresponding columns in which the section is defined to find them faster. – brnk Feb 9 '18 at 21:07
  • Are your readers really so helpless that they can't make the "leap" from the table of contents to a sectioning header on some unless both the page number and the column number are printed in the ToC? – Mico Feb 9 '18 at 21:19
  • I have really many subsections on each page since it's a cheatsheet for a lecture. So knowing the corresponding column would really help. – brnk Feb 9 '18 at 21:34
2

The following does what you want. It needs a few runs (3) to display everything correct. The option colaction is necessary for the command \docolaction and for the patch to work.

New answer for arbitrary number of columns

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum} 
\usepackage[colaction]{multicol}
\usepackage[left=1cm,right=1cm,top=1cm,bottom=1cm]{geometry}
\usepackage{etoolbox}

\makeatletter
\newcounter{brnk@col@count}
\renewcommand*\addcontentsline[3]
  {%
    \addtocontents{#1}
      {%
        \protect\contentsline{#2}{#3}
          {\thepage\ifnum\doublecol@number>0\relax.\string\brnk@cur@col\fi}%
      }%
  }
\def\brnk@patch@last
  {%
    \stepcounter{brnk@col@count}%
    \protected@write\@auxout{}%
      {%
        \string\@writefile{toc}%
          {\string\def\string\brnk@cur@col{\arabic{brnk@col@count}}}%
      }%
  }
\def\brnk@patch@else
  {%
    \ifmc@firstcol
      \setcounter{brnk@col@count}{0}%
    \fi
    \brnk@patch@last
  }
\def\brnk@patch@error
  {%
    \GenericError{}
      {Patching of \string\mc@col@status@write\space failed}
      {%
        Make sure `colaction` was set as an option for `multicol`.%
        \MessageBreak
        Else you're screwed, don't use the code provided here.\MessageBreak%
      }
      {No further help available.}%
  }
\pretocmd\mc@lastcol@status@write{\brnk@patch@last}{}{\brnk@patch@error}
\pretocmd\mc@col@status@write{\brnk@patch@else}{}{\brnk@patch@error}
\makeatother

\begin{document}
\begin{multicols*}{5}

\tableofcontents
\section{Headline 1}
\lipsum[1-2]

\section{Headline 2}
\lipsum[3]

\section{Headline 3}
\lipsum[4-7]

\subsection{Subsection 1}
\lipsum[8]
\subsection{Subsection 2}
\lipsum[9]
\end{multicols*}
\section{Headline 4}
\lipsum[5]
\begin{multicols*}{2}
\section{Headline 5}
\lipsum[6]
\end{multicols*}

\end{document}

enter image description here


Original answer for only 3 columns

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum} 
\usepackage[colaction]{multicol}

\makeatletter
\renewcommand*\addcontentsline[3]{%
  \def\brnk@column{}%
  \ifnum\doublecol@number>0
    \docolaction
      [5]
      {\def\brnk@column{.1}}
      {\def\brnk@column{.2}}
      {\def\brnk@column{.3}}%
  \fi
  \addtocontents{#1}{\protect\contentsline{#2}{#3}
    {\thepage\brnk@column}}}
\makeatother

\begin{document}
\begin{multicols*}{3}

\tableofcontents
\section{Headline 1}
\lipsum[1-2]

\section{Headline 2}
\lipsum[3]

\section{Headline 3}
\lipsum[4]
\end{multicols*}
\section{Headline 4}
\lipsum[5]
\begin{multicols*}{2}
\section{Headline 5}
\lipsum[6]
\end{multicols*}

\end{document}

Resulting ToC:

enter image description here

  • I looked up the docolaction command in the documentation and it states that it's only for three columns (left, middle, right). But I have 5 columns. I just used three columns in the minimal example to make the document easier to read. – brnk Feb 10 '18 at 13:13
  • Another problem is that my subsections are defined inside the multicols in a different environment. Is it possible to check whether the multicols environment is a parent instead of checking whether it is the current environment? – brnk Feb 10 '18 at 13:31
  • @brnk the edit uses a better test. For the support of more than 3 columns I can't do anything right now, but will get back at it later this day (hopefully). – Skillmon Feb 10 '18 at 13:55
  • @brnk sorry to disappoint you. A solution for 5 columns seems possible, but I'm neither willing nor have the time to create and test it. You could send a feature request to the maintainers of multicol and hope for the best, though. – Skillmon Feb 10 '18 at 23:35
  • @brnk sorry to disappoint you again, but I did create some code which works for 5 columns (in fact it should work for an arbitrary number of columns). I didn't test it too much, so it might not be perfectly stable. – Skillmon Feb 11 '18 at 17:06

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.