43

How do I hide section number in section heading ? I can't just use \section*{xxxx}, because I want subsections numbered correctly. I also tried :

\section*{xxxx}  
\stepcounter{xxxx}

Which hides section number and keeps subsection numbers. Unfortunately this solution also hides section in table of contents, which is undesirable.

Next attempt was :

\chapter{}
\section*{xxxx}
\addtocounter{section}{1}

However this made numbering of subsections continue on and on instead of reseting at new section. So what can I do to make it behave as normal, just not showing section numbers ? Thanks!

lockstep
  • 250,273
slezadav
  • 531

4 Answers4

49

Redefining \thesection is not sufficient. This will do better: the section title will be aligned to the left margin and not indented; in the table of contents, the section title will appear horizontally aligned to the chapter titles.

\documentclass{book}

\renewcommand{\thesection}{}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}
\makeatletter
\def\@seccntformat#1{\csname #1ignore\expandafter\endcsname\csname the#1\endcsname\quad}
\let\sectionignore\@gobbletwo
\let\latex@numberline\numberline
\def\numberline#1{\if\relax#1\relax\else\latex@numberline{#1}\fi}
\makeatother

\begin{document}
\frontmatter
\tableofcontents
\mainmatter
\chapter{Title}
\section{Xyz}
\subsection{Here we are}
\end{document}

Contents


Chapter

egreg
  • 1,121,712
  • Unfortunately this affects not only the section title, but also erases its counting when using \ref? (ok admittedly referring to a section number when it is not printed is a bit weird, but still?) Thanks! – Matifou Jan 05 '23 at 13:24
  • @Matifou And what should \ref produce if there's nothing to show? Even if we keep the numbering, how's a user supposed to find it? – egreg Jan 05 '23 at 13:54
35

I think your best bet is to redefine \thesection, which is the macro that normally prints the section number.

\renewcommand\thesection{}

If you want subsection numbers to include the section numbers that aren't getting printed, you will also need to redefine \thesubsection, which normally calls \thesection:

\makeatletter
\renewcommand\thesection{}
\renewcommand\thesubsection{\@arabic\c@section.\@arabic\c@subsection}
\makeatother
zwol
  • 2,919
  • Ok works well in hiding section number.Problem with this command is that it also hides section number in subsection numbering, which I want to keep so subsections are numbered like .1 .2 .3 and so on. –  Oct 30 '12 at 14:30
  • See edit - I thought of that right after I hit save :) – zwol Oct 30 '12 at 14:30
  • 2
    Don't do this; there's most likely a package for that. – Martin Schröder Nov 01 '12 at 13:37
  • 1
    This still slightly indents the section (at least when used with scrartcl), so I prefer answer https://tex.stackexchange.com/a/300055/77029 – Max Feb 22 '18 at 09:37
  • 1
    Unfortunately this also has the disadvantage of not only affecting the section title, but also every occurence as a reference (\ref). – Philipp Moers Feb 28 '22 at 22:12
  • yes, do you have a solution that does not erase also the \ref @zwol ? Thanks! – Matifou Jan 05 '23 at 13:20
  • @Matifou I think you had better ask a new question about that, explaining exactly what you want to see in the section heading, the table of contents, and \ref output. – zwol Jan 06 '23 at 05:00
10

If you're using a KOMA-script document class (e.g., scrartcl), then for normal behaviour, just not showing section numbers, include in your preamble

\renewcommand*{\sectionformat}{}

This also works for \chapterformat (if you're using scrbook or scrreport), \partformat, all the way down to \subparagraphformat.

The counter counts the section despite the number label not being shown, so TOC, PDF bookmarks, and subsection numbering all operate as they should (tested with hyperref and bookmark packages loaded.)

  • @egreg's response works great, but it's a mouthful to use repeatedly. So I tried to define a macro that I could invoke whenever I needed this functionality. However, when I do the obvious thing. \def\foo{ @egreg's script } an error is thrown when I call \foo, because seccntformat requires an argument. Could somebody please tell me how to make the script into a macro? – Leo Simon Jun 10 '18 at 02:26
0

With KOMA you can use the nonumber=true option in your sections.

You just need to enable the optional arguments when choosing the (KOMA) class:

\documentclass[headings=optiontoheadandtoc]{scrbook}

Then you can use:

\section[nonumber=true]{Numberless section}

This will hide the section number from the section header and the table of contents entry alike. Subsection numbering etc. are left unmodified.

There's also \addsec{Numberless section} which does the same thing but isn't quite as generic.

Iizuki
  • 161