2

In my table of contents, I need to have some entries numbered, and some un-numbered, but still have the number increment on un-numbered parts.

In other words, I need:

1 - First Section Title

2 - Second Section Title

Third Section Title (un-numbered)

4 - Fourth Section Title

My code looks like this:

\documentclass{article}

\begin{document}

\tableofcontents

\section{First Section Title}
\section{Second Section Title}  
\section*{Third Section Title}
\addcontentsline{toc}{section}{Third Section Title} 
\section{Fourth Section Title}

\end{document}

a

But this results in "Fourth Section Title" getting a number of 3 in the Table of Contents. How do I get it to have a 4?

  • Welcome to TeX.SX! Should this occur for any unnumbered section or just for the shown one? – user31729 Nov 24 '15 at 11:36
  • Any un-numbered section, there will be lots in the real document. – dain Nov 24 '15 at 11:37
  • See my answer please, but it's a little bit inconsistent to continue the numbering – user31729 Nov 24 '15 at 11:41
  • I can think to no reason for this. If the numbering increases, why should the number be omitted for some of the sections? – egreg Nov 24 '15 at 11:45
2

A quick and dirty way is to patch the \@startsection command, which is responsible for the starting of a section and look for the \@ifstar macro inside. Add a \stepcounter{section} there only if the section level is section (`\refstepcounter is of no use, in my point of view).

This must be done after \tableofcontents since all ToC-like commands use \section*{} for the ToC heading.

Be careful with \printindex and \bibliography etc.

\documentclass{article}

\usepackage{xpatch}



\begin{document}

\tableofcontents

\makeatletter
\xpatchcmd{\@startsection}{%
  \@ifstar
  {\@ssect{#3}{#4}{#5}{#6}}%
}{%
  \@ifstar
  {%
    \ifstrequal{#1}{section}{\stepcounter{section}}{}%
    \@ssect{#3}{#4}{#5}{#6}}%
}{\typeout{Yes}}{\typeout{No!}}
\makeatother
\section{First Section Title}
\section{Second Section Title}  
\section*{Third Section Title}
\addcontentsline{toc}{section}{Third Section Title} 
\section{Fourth Section Title}
\subsection*{Test subsection}
\section{Another section}

\end{document}
  • Thanks a lot! That worked fine. Although, what do you mean by "be careful with \printindex and \bibliography"? What bad things will happen? – dain Nov 24 '15 at 11:41
  • \printindex etc. usually use \section*{...} too, so this will lead to additional \stepcounter commands then. If there are no numbered sections after index etc. this is no problem, unless your code relies on a certain total number of sections (which is not the case, most likely) – user31729 Nov 24 '15 at 11:43
  • Now also a \subsection* command would step section – egreg Nov 24 '15 at 11:46
  • @egreg: True, I forgot that \@ssect is used in any of those structuring commands. I'll change – user31729 Nov 24 '15 at 11:47
2

You could switch to the KOMA-classes and patch \addsec. This would also remove the need for the \addcontentsline. But imho the numbering is curious. It looks as if the typesetter made an error and forget a number.

\documentclass{scrartcl}
\usepackage{etoolbox}
\pretocmd\addsec{\refstepcounter{section}}{}{}

\begin{document}

\tableofcontents

\section{First Section Title}
\section{Second Section Title}

\addsec{Third Section Title}

\section{Fourth Section Title}

\end{document}

enter image description here

0

Just define a special command, instead of abusing \section*. When you'll change your mind and decide not to do such a funny thing, you'll just have to replace \specialsection with \section.

\documentclass{article}

\begin{document}

\tableofcontents

\section{First Section Title}
\section{Second Section Title}  
\section*{Third Section Title}
\addcontentsline{toc}{section}{Third Section Title} 
\section{Fourth Section Title}

\end{document}

enter image description here

An advanced version where \specialsection admits the same syntax as \section (with an optional argument for the toc entry):

\documentclass{article}

\makeatletter
\newcommand{\specialsection}{\@dblarg\special@section}
\def\special@section[#1]#2{%
  \stepcounter{section}%
  \section*{#2}%
  \addcontentsline{toc}{section}{#1}%
}
\makeatother

\begin{document}

\tableofcontents

\section{First Section Title}
\section{Second Section Title}
\specialsection{Third Section Title}
\section{Fourth Section Title}

\end{document}

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.