6

I have the following code to create table of contents:

     \documentclass{article}

     \begin{document}

     \tableofcontents

     \section{section A}

     \addcontentsline{toc}{section}{section B}
     \section*{section B}

     \end{document}

Section B does appear in the TOC, but it appears unaligned with the text from section A, because there is no number preceding it. Is there a way to make section B go forward a bit, so that it is aligned?

EDIT: I would rather have someone that is "truly" aligned, instead of pushing it forward with blank spaces...

kloop
  • 9,684
  • https://github.com/johannesbottcher/unnumberedtotoc with option indentunnumbered should do the trick just fine. – Johannes_B Nov 16 '14 at 09:05

3 Answers3

6

You can use the little package unnumberedtotoc which can be found on github at the moment. It just provides one option which will suit your needs; indentunnumbered. It also takes care or your marks. All this functionality is provided by the KOMA-classes by default. A switch might be worth it ;-)

\documentclass{article}
\usepackage[indentunnumbered]{unnumberedtotoc}%<-----
\begin{document}

\tableofcontents

\section{section A}

\addsec{section B}

\end{document}
Johannes_B
  • 24,235
  • 10
  • 93
  • 248
  • this seems to work, but unfortunately breaks hyperref in the TOC. Any ideas (clicking on an numbered section goes back to the beginning of the document, instead of the right page)? – kloop Nov 16 '14 at 15:54
  • No problem here, did you load my package before loading hyperref? EDIT: It ieven works when loaded after, just checked with the above example (extended by some blindtext). – Johannes_B Nov 16 '14 at 16:01
  • @kloop Are there any news concerning hyperlinks? – Johannes_B Dec 17 '14 at 17:35
  • This is exactly what I was looking for ... but it seems not to work with amsbook documentclass. I encounter two issues: 1. the documentclass itself adds unnumbered sections to the toc, so entries are doubled (one with indent, one without); 2. Really I want to have unnumbered subsubsections with the correct indent, and it seems there is no \addsubsubsec command similar to \addsec. Any possible fix or workaround you can think of? Thanks. – Chris Nov 16 '21 at 11:39
  • 1
    Hi @chris. The little package is unsuited for ams classes. It is worth asking a new question about it. – Johannes_B Nov 16 '21 at 20:38
  • @Johannes_B Thanks, I have asked this as a new question here: https://tex.stackexchange.com/questions/622874/indent-unnumbered-sections-in-toc-for-ams-classes – Chris Nov 17 '21 at 15:32
5

You can add horizontal spacing equivalent to what would normally be added:

enter image description here

Notes:

  • The \addcontentsline should be immediately after the \section*{}, not before the \section{}.

Code:

 \documentclass{article}

\begin{document}

\tableofcontents

\section{section A}

\section*{section B} \addcontentsline{toc}{section}{\protect\hphantom{\numberline{\thesection}}section B}

\section{section C}

\end{document}

Peter Grill
  • 223,288
  • Just \protect\numberline{} is sufficient. – egreg Nov 16 '14 at 10:39
  • the issue with that is that it doesn't add a pointer to the page number... – kloop Nov 16 '14 at 15:56
  • Tip for the hyperref package users with bookmarksnumbered option: \protect\numberline{} produces an unwanted leading whitespace in the .out file, hence in bookmark as well. \texorpdfstring{\protect\numberline{}foo}{foo} will help (hyperref isn't related to the original question but just in case someone in the future refers to this answer). – yudai-nkt May 12 '17 at 07:44
1

You have to push that entry by \@pnumwidth which is the width reserved for typsetting the section numbers in toc.

 \documentclass{article}
 \newlength{\mylen}
 \makeatletter
 \setlength{\mylen}{\@pnumwidth}
 \makeatother
 \begin{document}

 \tableofcontents

 \section{section A}


 \section*{section B}
 \addcontentsline{toc}{section}{\hspace*{\mylen}section B}

 \end{document}

enter image description here

  • this didn't work for me on the actual document, though it could be because it is entangled with something else.... also, it doesn't add a pointer to the page number. – kloop Nov 16 '14 at 15:55