1

With the memoir documentclass, I have both a Short Contents (chapters and sections only) and a Detailed Contents (also subsections, which happen to be unnumbered).

Is it possible to include the entry "Detailed Contents" in the Short Contents table without including the entry "Contents" in the Detailed Contents, and vice versa?

Source:

\documentclass[12pt]{memoir}

\begin{document}

\frontmatter

{\renewcommand*{\contentsname}{Short Contents}
  \setcounter{tocdepth}{1}
  \tableofcontents
}
\renewcommand*\contentsname{Detailed Contents}
\setcounter{tocdepth}{3}
\tableofcontents 

\mainmatter

\chapter{First Chapter} 

\section{Section 1}

\subsection*{First subsection}
\addcontentsline{toc}{subsection}{First subsection}

\subsection*{Second subsection}
\addcontentsline{toc}{subsection}{Second subsection}

\section{Section 2}

\end{document}

First page of output:

Short and detailed TOCs

Of course:

  • using the starred form \tableofcontents* for only the Detailed Contents produces the entry "Short Contents" in both TOCS (but "Detailed Contents" in neither);

  • using the starred form \tableofcontents* for only the Short Contents produces the entry "Detailed Contents" in both TOCs (but "Short Contents" in neither); and

  • using the starred form for both produces no entry for either TOC in either of the TOCs.

  • memoir emulates tocloft, which in turn uses \newlistof instead the traditional \tableofcontents definition. The \contentsname addition to the ToC is hardcoded -- once it is there, it is written to the .toc file. You rather need two different tocs! – user31729 Dec 29 '16 at 19:21
  • Do you need hyperref here? – user31729 Dec 30 '16 at 22:32
  • Yes, I am using hyperref. – murray Jan 1 '17 at 16:27
2

This is a possible solution, by using a \if@shorttoc switch and writting dummy \contentsline to the ToC, which are evaluated at the time of loading the .toc file in \@input{\jobname.toc} (hidden in \@starttoc usually)

If \@shorttoctrue is set, \longcontentsstuff will show the contentsline meant for the detailed contents, correspondingly it is \relaxed for \@shorttocfalse, accordingly the opposite is done for\shortcontentsstuff`.

Simplification is done with \shorttableofcontents and \longtableofcontents, both having an optional argument that is used as value for the tocdepth counter. (I know, there are memoir equivalents of this tocdepth as well)

In order to provide hyperref support some 'heavy' tricks has to be used.

  • Perhaps I can look after a memoir based solution later on.
  • Of course, my redefinition of \tableofcontents is not compatible with memoir's version, especially with the \tableofcontents* way.

\documentclass[12pt]{memoir}

\usepackage{hyperref}



\makeatletter

% Just in case we're not loading hyperref
\@ifpackageloaded{hyperref}{%
}{
  \providecommand{\@currentHref}{}
  \providecommand{\hyperlink}[2]{#2}
  \providecommand{\getrefnumber}[1]{}
  \providecommand{\phantomsection}{}
}


\AtBeginDocument{%
\newcommand{\shortcontentsstuff}{%
  \contentsline{chapter}{\hyperlink{\getrefnumber{toc::shorttoc::anchor}}{\shortcontentsname}}{\pageref{toc::shorttoc}}{\getrefnumber{toc::shorttoc::anchor}}
}

\newcommand{\longcontentsstuff}{%
  \contentsline{chapter}{\hyperlink{\getrefnumber{toc::longtoc::anchor}}{\longcontentsname}}{\pageref{toc::longtoc}}{\getrefnumber{toc::longtoc::anchor}}
}
}

\newif\if@shorttoc

\renewcommand{\tableofcontents}{%
  \if@twocolumn
  \@restonecoltrue\onecolumn
  \else
  \@restonecolfalse
  \fi
  \chapter*{\contentsname
   \@mkboth{%
     \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}
 % Addition
 \if@shorttoc
 \protected@edef\@currentlabel{shorttoc}% Rather unimportant
 \edef\@currentlabelname{\shortcontentsname}% For \nameref
 \label{toc::shorttoc}% 
 % Now let us fix the hyperanchor for the short toc
 \edef\@currentlabelname{}%
 \protected@edef\@currentlabel{\@currentHref}\label{toc::shorttoc::anchor}
 \else
 \protected@edef\@currentlabel{longtoc}% Rather unimportant
 \edef\@currentlabelname{\longcontentsname}
 \label{toc::longtoc}%
 % Now let us fix the hyperanchor for the long toc
 \edef\@currentlabelname{}%
 \protected@edef\@currentlabel{\@currentHref}\label{toc::longtoc::anchor} 
 \fi
 % End of additions
 \@starttoc{toc}%
 \if@restonecol\twocolumn\fi
}

\newcommand{\longcontentsname}{Detailed Contents}
\newcommand{\shortcontentsname}{Short Contents}

\newcommand{\shorttableofcontents}[1][1]{%
  \begingroup
  \@shorttoctrue
  \setcounter{tocdepth}{#1}
  \let\shortcontentsstuff\relax
  \phantomsection
  \write\@auxout{\string\@writefile{toc}{\protect\longcontentsstuff}}
  \let\contentsname\shortcontentsname% Disabling the \shortcontentsstuff in toc
  \tableofcontents
  \endgroup
  \@shorttocfalse% Disable the short toc
}

\newcommand{\longtableofcontents}[1][3]{%
  \begingroup
  \setcounter{tocdepth}{#1}
  \phantomsection
  \let\longcontentsstuff\relax% Disabling the \longcontentsstuff in toc
  \write\@auxout{\string\@writefile{toc}{\protect\shortcontentsstuff}}
  \let\contentsname\longcontentsname
  \tableofcontents
  \endgroup
}
\makeatother




\begin{document}


\frontmatter


\shorttableofcontents

\longtableofcontents

\mainmatter

\chapter{First Chapter} 

\section{Section 1}

\subsection*{First subsection}
\addcontentsline{toc}{subsection}{First subsection}

\subsection*{Second subsection}
\addcontentsline{toc}{subsection}{Second subsection}


\section{Section 2}

\end{document}

Short ToC enter image description here

Detailed ToC enter image description here

The hyperlinks are not shown here...

  • Interesting solution! Alas, at this point I'm committed to the memoir document class. – murray Dec 31 '16 at 16:26
  • @murray: It is a memoir solution -- don't blame me for hardcoded stuff in memoir – user31729 Dec 31 '16 at 17:05
  • Thanks for the solution! I realize this required modifying low-level memoir package code. Always nice if a solution is available that does not require such mods, since then it's likely to be robust when package changes. – murray Jan 6 '17 at 21:26
  • @murray: Packages & classes change once in a while but most times they gain other functionality by using new macros and features rather than break the old stuff. I am pretty confident that my solution is not really low-level ;-) – user31729 Jan 6 '17 at 21:29
  • Certainly hope old stuff doesn't break with new versions! "Low-level" is evidently in eye of beholder. When I say it, I mean code that changes something in a package (notably when @ appears!) rather than by using options or additional packages. But no disparagement of the fix intended. – murray Jan 7 '17 at 23:05

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.