4

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

4

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}
| improve this answer | |
  • 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
2

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}
| improve this answer | |
  • 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 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

| improve this answer | |
  • 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

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.