\documentclass{article}

\begin{document}

\tableofcontents

\section{section A}

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

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}

\end{document}

• 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)? 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). Nov 16 '14 at 16:01
• @kloop Are there any news concerning hyperlinks? Dec 17 '14 at 17:35

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

## 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}

\section{section C}

\end{document}

• Just \protect\numberline{} is sufficient. Nov 16 '14 at 10:39
• the issue with that is that it doesn't add a pointer to the page number... 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). May 12 '17 at 7:44

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}