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...

1

3 Answers 3

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}
6
  • 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
    Commented Nov 16, 2014 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
    Commented Nov 16, 2014 at 16:01
  • @kloop Are there any news concerning hyperlinks?
    – Johannes_B
    Commented Dec 17, 2014 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
    Commented Nov 16, 2021 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
    Commented Nov 16, 2021 at 20:38
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}
3
  • Just \protect\numberline{} is sufficient.
    – egreg
    Commented Nov 16, 2014 at 10:39
  • the issue with that is that it doesn't add a pointer to the page number...
    – kloop
    Commented Nov 16, 2014 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
    Commented May 12, 2017 at 7: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

1
  • 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
    Commented Nov 16, 2014 at 15:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .