1

There's any number of variations on the question "How do I get my unnumbered sections into the table of contents?" ...

and none of the answers are any good to me, because they all involve sticking \addcontentsline, plus maybe other stuff (\mark, \phantomsection, ...?), at just the right place in the document. In my case, the \section* commands are being issued by packages, and they don't give me hooks to insert stuff at just the right place; and also it is unclear exactly what set of "other stuff" I need. It would be simpler and more robust if I could just redefine \section* so that it behaves exactly as its non-starred version does, except that it does not print a section number.

How do I do that?

Note: KOMA-Script classes are not an option. I am currently using plain article for drafts and I will eventually need to drop in a journal's class.

Note 2 (from comments on an attempted answer): adjusting \secnumdepth globally will not work for me, because (a) I still want normal section numbering from unstarred \section etc, and (b) the packages that are issuing \section* commands can't be persuaded to use plain \section instead.

  • is titlesec an option? – touhami Feb 11 '16 at 18:40
  • titlesec/titletoc have a numberless key, which allows for different formatting for numbered and unnumbered sections, chapters, &c. – Bernard Feb 11 '16 at 18:54
  • @touhami Unless I'm missing something, titlesec has no option which makes \section* do what I need it to do. It has commands which are intended to replace \section*, but they do not help me, because I don't control the code that's issuing \section* commands. – zwol Feb 13 '16 at 23:22
  • @Bernard see reply to touhami – zwol Feb 13 '16 at 23:22
  • @zwol you are right. – touhami Feb 14 '16 at 6:01
1

You can redefine \section to capture and condition on when the starred-version is used. Upon finding \section*, issue it just like you would \section, but remove the number-printing mechanism through an appropriate setting of the counter secnumdepth.

enter image description here

\documentclass{article}

\usepackage{xparse}

\let\oldsection\section
\makeatletter
\newcounter{@secnumdepth}
\RenewDocumentCommand{\section}{s o m}{%
  \IfBooleanTF{#1}
    {\setcounter{@secnumdepth}{\value{secnumdepth}}% Store secnumdepth
     \setcounter{secnumdepth}{0}% Print only up to \chapter numbers
     \oldsection{#3}% \section*
     \setcounter{secnumdepth}{\value{@secnumdepth}}}% Restore secnumdepth
    {\IfValueTF{#2}% \section
       {\oldsection[#2]{#3}}% \section[.]{..}
       {\oldsection{#3}}}% \section{..}
}
\makeatother

\begin{document}

\tableofcontents

\section{TestA}

\section*{TestB}

\end{document}

xparse provides an easy interface for (re)defining commands that may have a starred version, as well as an optional argument.

  • I'll have to experiment with this but it looks promising, thank you. – zwol Feb 11 '16 at 19:11

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.