1

Im struggling with this aspect of the table of contents. I have a one column document but need a two column table of contents. By default, the \twocoltocetc produces results undesirable for a one column document, placing the Contents section heading also inside the two column environment instead of placing it normally. I believe this is a limitation of the of the definition on line 7732 of memoir.cls where \twocolumn is called. It seems that there is a limitation where the \twocolumn and \onecolumn switches always must start on a new page, as indicated by Peter Wilsons remarks found here.

Peter advices to use the multicols environment for such a task, and I have also found an example on page 163 of memman where the the multicols environment is added to the .toc with \addtocontents{toc}{\protect\begin{multicols}{2}}

With modifications, I can create the following environment, and when I wrap the entire document in it I can produce the table of contents as desired.

\newenvironment{twocoltoc}[1]{%
   \addtocontents{toc}{\protect\begin{multicols}{2}}}%
   {\addtocontents{toc}{\protect\end{multicols}}}

This feels rather clunky though. I can also just directly place the \addtocontents... instead of use the environment. I would like to have a single call function which acomplishes this goal, similar to \twocoltocetc. I need a way to add a line to the end of the .toc after the rest of the lines are generated, but I am not quite sure how to achieve this. It has to be in the guts of starttoc{toc} but I am unsure of where to find that.

Here is my test file showing what I have so far, it produces a desirable output but is not elegant.

\documentclass[letterpaper, 10pt]{memoir}
\usepackage{multicol}
\usepackage{xparse}

\maxtocdepth{subsection}

\newif\ifwanttwocoltoc
\wanttwocoltocfalse

\NewDocumentCommand{\twocoltoc}{}{
    \addtocontents{toc}{\protect\begin{multicols}{2}}%
    \wanttwocoltoctrue
}

\NewDocumentCommand{\maketoc}{}{
    \tableofcontents
    \ifwanttwocoltoc
        % \addtocontents{toc}{\protect\end{multicols}}
}

\begin{document}

\twocoltoc
\maketoc
 
\chapter{Chapter}
\section{Section}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\subsection{Subsection}
\section{Section}
\subsection{Subsection}
\section{Section}
\chapter{Chapter}

\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\section{Section}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\section{Section}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\section{Section}

\chapter{Chapter}
\section{Section}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}


{\addtocontents{toc}{\protect\end{multicols}}}
\end{document}

p.s. there is a typo in the mentioned example, \end{multocols}.

1 Answer 1

1

I think that this answer goes towards what the OP wants, but there is a slight snag.

% twocoltocprob.tex  SE 637513

\documentclass[letterpaper, 10pt]{memoir}

\usepackage{comment}

\begin{comment}    %%% OPs code
\usepackage{multicol}
\usepackage{xparse}

\maxtocdepth{subsection}

\newif\ifwanttwocoltoc
\wanttwocoltocfalse

\NewDocumentCommand{\twocoltoc}{}{
    \addtocontents{toc}{\protect\begin{multicols}{2}}%
    \wanttwocoltoctrue
}

\NewDocumentCommand{\maketoc}{}{
    \tableofcontents
    \ifwanttwocoltoc
        % \addtocontents{toc}{\protect\end{multicols}}
}

\setlength{\textheight}{0.33\textheight}

\begin{document}

\twocoltoc
\maketoc

\end{comment}  %% end of OPs code

%\begin{comment}  %% my code

\setlength{\textheight}{0.4\textheight}

\usepackage{lipsum}
\usepackage{multicol}

\newcommand{\twocoltoc}{\tableofcontents%
  \addtocontents{toc}{\protect\begin{multicols}{2}}}
  
\renewcommand{\twocoltoc}{\addtocontents{toc}{\protect\begin{multicols}{2}}
  \tableofcontents}
  

\begin{document}

%%%% regular (my) code for the ToC
\twocoltoc

\end{document}

%%% OPs division codes

\chapter{Chapter}
\lipsum[1]
\section{Section}
\lipsum[2]
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\subsection{Subsection}
\section{Section}
\subsection{Subsection}
\section{Section}
\chapter{Chapter}

%%%%\end{document}

\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\section{Section}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\section{Section}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}
\subsection{Subsection}
\subsubsection{Subsubsection}
\subsection{Subsection}
\section{Section}

\chapter{Chapter}
\section{Section}
\section{Section}
\subsection{Subsection}
\subsection{Subsection}


%% {\addtocontents{toc}{\protect\end{multicols}}}
\end{document}

I think that the output is what the OP wants but when processing the above I get an error message about file ended scanning use of \next. However, ignoring this, the output is what I think the OP is after.

Can anyone help with the \next problem?

enter image description here

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.