63

I want to add a section without a number, but \section* also excludes it from the numbering, so if there is this

\section*{Section 1}
\subsection{Subsection}
\subsection{Subsection 2}
\section*{Section 2}
\subsection{Subsection 3}

I get what I want untill Section 2, but I want Subsection 3 to be numbered as "2.1", and it keeps numbering "1.3".

Is there any command that can fix this?

  • Welcome to TeX.SX! Do you want that the titles of the sections are exactly “Section 1”, “Section 2” and so on, so the number at the left is redundant? – egreg Oct 5 '13 at 13:29
  • Something like that, yeah, it is redundant – Nuria Oct 5 '13 at 13:37
  • @Nuria If that is the intention my answer is not the way, you should never number by hand, just change the format so the automatic number comes in the right place. – David Carlisle Oct 5 '13 at 13:41
  • @DavidCarlisle It's not that I'm numbering by hand, I want something like this: Section 1: Hello! So Latex makes it: 1 Section 1: Hello! Thanks for your answer – Nuria Oct 5 '13 at 13:42
  • 1
    @Nuria That is exactly what I mean, the markup should be \section{hello!} and just modify the style to put Section 1: at the left instead of just 1. – David Carlisle Oct 5 '13 at 13:44
25

Here's a customizable version; by defining \prefix@subsection, for instance, you can add some prefix also to the subsection number. Note that you want to use \section and not \section*.

\documentclass{article}
\makeatletter
% we use \prefix@<level> only if it is defined
\renewcommand{\@seccntformat}[1]{%
  \ifcsname prefix@#1\endcsname
    \csname prefix@#1\endcsname
  \else
    \csname the#1\endcsname\quad
  \fi}
% define \prefix@section
\newcommand\prefix@section{Section \thesection: }
\makeatother

\begin{document}
\section{Hello}
\subsection{Subsection}
\subsection{Subsection 2}
\section{Go on}
\subsection{Subsection 3}
\end{document}

enter image description here

  • It's this, it's this! But my document keeps looking like this: Section 1: Hello, 1.1 Subsection, 1.2 Subsection 2, Section 1: Go on, 1.1 Subsection 3. Is there any way to fix this? – Nuria Oct 5 '13 at 14:03
  • 1
    @Nuria You must have something else; have you redefined \thesection, maybe? You probably had done \renewcommand{\thesection}{1} in order to get the output you showed. Check for it and remove. – egreg Oct 5 '13 at 14:04
  • Okay, it works now... I deleted everything i had accumulated in the preamble regarding sections and such and found the problem, thanks! – Nuria Oct 5 '13 at 14:10
33

You don't want unnumbered sections, you just want to modify the style not to show the number:

enter image description here

\documentclass{article}

\makeatletter
\def\@seccntformat#1{%
  \expandafter\ifx\csname c@#1\endcsname\c@section\else
  \csname the#1\endcsname\quad
  \fi}
\makeatother

\begin{document}
\section{Section aa}
\subsection{Subsection}
\subsection{Subsection bbb}
\section{Section zzz}
\subsection{Subsection jjj}

\end{document}

Or as clarified in comments you do want the number, but prefixed with Section:

enter image description here

\documentclass{article}

\makeatletter
\def\@seccntformat#1{%
  \expandafter\ifx\csname c@#1\endcsname\c@section
  Section \thesection:
  \else
  \csname the#1\endcsname\quad
  \fi}
\makeatother

\begin{document}
\section{aa}
\subsection{Subsection}
\subsection{Subsection bbb}
\section{zzz}
\subsection{Subsection jjj}

\end{document}
  • It's just what I want, but using the automatic numbering the second section is Section 1 and I don't know why. – Nuria Oct 5 '13 at 13:54
  • @Nuria you probably have a * left stopping the numbering – David Carlisle Oct 5 '13 at 14:05
  • Why not use titlesec for this? – Sean Allred Oct 5 '13 at 14:19
  • 4
    @SeanAllred because I know the latex insides and can suggest a \@seccntformat redefinition in my sleep but I would have to read the pacge doc for the package (if I remembered it existed at all:-) freel free to post a package based answer as well, break up my little head-to-head with egreg:-) – David Carlisle Oct 5 '13 at 14:34
  • @DavidCarlisle A very good reason indeed :-) – Sean Allred Oct 5 '13 at 14:37
21

Consider using the titlesec package, which does the hard work for you. Note that this solution requires you to decide how your sections will look. As the style is hard-coded in the definition of \section, I don't believe there is a 'nice' way to access it.

\documentclass{article}
\usepackage{titlesec}

\titleformat{\section}
  {\normalfont\Large\bfseries}   % The style of the section title
  {}                             % a prefix
  {0pt}                          % How much space exists between the prefix and the title
  {Section \thesection:\quad}    % How the section is represented

% Starred variant
\titleformat{name=\section,numberless}
  {\normalfont\Large\bfseries}
  {}
  {0pt}
  {}

\begin{document}
\section{Animals}
\subsection{Vertebrates}
\subsection{Invertebrates}
\section{Plants}
\subsection{Fruit-Bearing}
\subsection{Carnivorous}
\section*{Unnumbered Section}
\end{document}

output

(Thanks to @egreg for pointing out the need for a numberless variant.)

  • explicit is not needed; just remove the #1; you should also define the numberless variant, in any case. David's and my answers have the advantage one doesn't need to guess at the fonts to be used. – egreg Oct 5 '13 at 15:14
  • @egreg Thanks for the tip, and very true. Although, I can't think of a (normal) situation where such fonts would be redefined without titlesec, or at most without some sort of \@sectionstyle macro. (I'm actually surprised that \section hard-codes it in its definition.) – Sean Allred Oct 5 '13 at 15:31
  • 1
    @SeanAllred The definition of the sectional units is left to the class; the standard classes don't have a provision for easily changing the fonts, because this is better an expert's job. Everybody can see where the freedom of font choosing in word processors has led. – egreg Oct 5 '13 at 15:36

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.