81

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?

6
  • 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
    Commented Oct 5, 2013 at 13:29
  • Something like that, yeah, it is redundant
    – Nuria
    Commented Oct 5, 2013 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. Commented Oct 5, 2013 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
    Commented Oct 5, 2013 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. Commented Oct 5, 2013 at 13:44

3 Answers 3

30

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

0
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}
6
  • It's just what I want, but using the automatic numbering the second section is Section 1 and I don't know why.
    – Nuria
    Commented Oct 5, 2013 at 13:54
  • @Nuria you probably have a * left stopping the numbering Commented Oct 5, 2013 at 14:05
  • Why not use titlesec for this? Commented Oct 5, 2013 at 14:19
  • 5
    @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:-) Commented Oct 5, 2013 at 14:34
  • @DavidCarlisle A very good reason indeed :-) Commented Oct 5, 2013 at 14:37
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.)

3
  • 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
    Commented Oct 5, 2013 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.) Commented Oct 5, 2013 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
    Commented Oct 5, 2013 at 15:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .