80

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?

Nuria
  • 935
  • 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 05 '13 at 13:29
  • Something like that, yeah, it is redundant – Nuria Oct 05 '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 05 '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 05 '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 05 '13 at 13:44
  • @Nuria I updated my answer with an example – David Carlisle Oct 05 '13 at 13:48

3 Answers3

37

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}
David Carlisle
  • 757,742
29

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

Another application:

\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{}
\newcommand{\prefix@subsection}{\thesubsection\ - }
\newcommand{\prefix@subsubsection}{\thesubsubsection\ - }
\renewcommand{\thesubsection}{\arabic{subsection}}
\makeatother

\begin{document} \section{A section} \section{Another section} \section{A third section} \subsection{A subsection} \subsubsection{A subsubsection} \subsubsection{Another subsubsection}

\end{document}

enter image description here

egreg
  • 1,121,712
28

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

Sean Allred
  • 27,421
  • 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 05 '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 05 '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 05 '13 at 15:36