Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When using * to remove headlines from the toc, there might be times when you would like to see all the headlines in the toc -- including the removed ones. But you would like to see the headlines just temporarily to check what is in your document and what is not there without changing all the *-ed headlines to non-*-headlines (and back afterwards).

My question: Is there a command or something to ignore the *-ing globally in the whole document, that can be switched on and off at any time?

share|improve this question
    
some classes do include the starred headers in the toc, for example amsbook, amsart and amsproc. this, of course, results in the problem of how to omit (some or all of) them if they're not wanted there. but that's a different question. –  barbara beeton Aug 10 '12 at 14:47
add comment

1 Answer

up vote 7 down vote accepted

You can include the following patch in your document preamble and active it using the \ifshowallintoc conditional:

enter image description here

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox

\newif\ifshowallintoc
%\showallintoctrue% Comment/uncomment to include all/not in ToC

\makeatletter
\ifshowallintoc
  \patchcmd{\@startsection}% <cmd>
    {\@ifstar}% <search>
    {\xdef\@@section@level{#1}\xdef\@@section@level@num{#2}\@ifstar}% <replace>
    {}{}% <success><failure>
  \patchcmd{\@ssect}% <cmd>
    {\@xsect}% <search>
    {\addcontentsline{toc}{\@@section@level}{%
       \ifnum \@@section@level@num>\c@secnumdepth \else
         \protect\numberline{}%
       \fi%
     #5}\@xsect}% <replace>
    {}{}% <success><failure>
\fi
\makeatother
\begin{document}
\tableofcontents
\section{A section}
\subsection{A subsection}
\subsection*{Another subsection}
\subsection{A subsection}
\section*{Another section}
\section{A section}
\subsection*{Another subsection}
\end{document}

Uncommenting \showallintoctrue (since the default is \showallintocfalse) reveals:

enter image description here

The motivation behind the solution is to store information previously lost when using the starred version of a sectional unit. This information is then written to the ToC file upon the condition being true or false. The main macros modified with the patch (provided by etoolbox's \patchcmd) are \@startsection and \@ssect.

Note that this solution is heavily dependent on the document class you're using. It may require a completely different patch if you're not using one of the standard document classes, or even using one that supports \chapter- and \part-level commands, like book and report.

share|improve this answer
    
Thank you very much! Basically, it works. But there are two questions remaining: 1. Is it possible to define a single solution like this that can be used in every class? 2. As you see in your shared outcome (more precise, in the second) there is the headline "Contents" twice. Could one be removed? –  Philip Aug 11 '12 at 8:58
    
@Philip: 1) It would be possible to adapt the current solution to work for other document classes, but something that is general would be difficult. Why? Different document classes handle their sectional units differently; either in structure, or the collection of information. What document class do you use? 2) The "second" Contents is the 'Table of Contents' heading included in the ToC. This is because of the fact that the ToC (in article) is set as a \section*. It is possible to remove this "duplication". Then again, you're just toggling this removal anyway, so it's temporary, right? –  Werner Aug 11 '12 at 13:37
    
1) Usually, I use the book or the article class. 2) I see. And yes, I would use it just temporary. –  Philip Aug 14 '12 at 21:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.