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

Detailed ToC

The hyperlinks are not shown here...
memoiremulatestocloft, which in turn uses\newlistofinstead the traditional\tableofcontentsdefinition. The\contentsnameaddition to the ToC is hardcoded -- once it is there, it is written to the.tocfile. You rather need two different tocs! – Dec 29 '16 at 19:21hyperrefhere? – Dec 30 '16 at 22:32hyperref. – murray Jan 01 '17 at 16:27