6

I would like to add a dot between the number of the section and section name. I used

\makeatletter
\renewcommand*{\@seccntformat}[1]{%
\csname the#1\endcsname .}
\makeatother

The problem is that, obviously, it put a dot also among subsection number and subsection name which I do not want. Is it possible to add the dot only between section number and section using low level instructions i.e. without any additional package?

7

Using a \ifx\.... comparison

\documentclass{article}

\makeatletter


\DeclareRobustCommand{\@seccntformat}[1]{%
  \def\temp@@a{#1}%
  \def\temp@@b{section}%
  \ifx\temp@@a\temp@@b
  \csname the#1\endcsname .\quad%
  \else
  \csname the#1\endcsname\quad%
  \fi
} 


\makeatother


\begin{document}

\section{Foo}

\subsection{Foo}

\end{document}

enter image description here

11

How about

\makeatletter
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\quad}%    default
   {\csname #1@cntformat\endcsname}}% enable indiv. control
\newcommand\section@cntformat{\thesection.\quad}
\makeatother

Feel free to use a different spacing amount than \quad.


A full MWE:

\documentclass{article}
\makeatletter
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\quad}%    default
   {\csname #1@cntformat\endcsname}}% enable indiv. control
\newcommand\section@cntformat{\thesection.\quad}
\makeatother

\begin{document}
\section{Hello World}
\subsection{Good Morning}
\subsubsection{Yawn}
\section{Goodbye World}
\end{document} 

Addendum: The OP has asked me to explain a bit how this code works. It's helpful to begin with the default definition of the \@seccntformat macro that's provided by the LaTeX kernel (see the file latex.ltx):

\def\@seccntformat#1{\csname the#1\endcsname\quad}

The macro's argument, #1, is a string of the form section, subsection, subsubsection, paragraph, or subparagraph. Upon expansion, the macro will thus return \thesection\quad, \thesubsection\quad, etc. Observe that there is no . ("dot", "full stop") before \quad.

The proposed modified form of the macro is:

\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\quad}%    default
   {\csname #1@cntformat\endcsname}}% enable indiv. control

The first thing the redefined macro does is to check if the macro \#1@cntformat is "undefined". (As above, #1 is a string of the form section, subsection, etc.) If \@ifundefined{#1@cntformat} is true, the "true" branch is followed. Suppose, for instance, that #1 is set to subsubsection. Assuming \subsubsection@cntformat hasn't been defined before \@seccntformat is executed, the "true" branch is chosen, and thus \@seccntformat expands to \thesubsubsection\quad.

If, in contrast, #1 is set to section, \section@cntformat will in fact have been defined by the time \@seccntformat is executed. Specifically, the solution given above provides:

\newcommand\section@cntformat{\thesection.\quad}

Hence, the "false" branch of \@ifundefined{#1@cntformat} is followed, \@seccntformat#1 evaluates to \section@cntformat which, in turn, evaluates to \thesection.\quad.

Comparing this approach with the one in Christian Hupfer's answer is useful. Christian's version of \@seccntformat begins by setting up two "scratch" macros; the first is set to section and the other to (the contents of) #1. Then the \ifx conditional is applied to compare the two macros. If the condition is 'true', which will only be the case if #1 is equal to the string section, \thesection.\quad is executed; otherwise, \csname the#1\endcsname\quad is executed. If you're only ever going to redefine the look of the formatted section counter, both approaches are equally good. If, for some reason not revealed so far, you also needed to change the looks of the formatted subsection, subsubsection, paragraph, and subparagraph counters, the approach above may be a bit more straightforward to implement. Let me state that this statement shouldn't be taken to imply some kind of criticism of Christian's solution!

  • Your solution of course work, but I didn't understand how. In particular where is that I decide that the non-default command is for section only? What if I would like to format section in a way, subsection in another, subsubsection again differently? – MaPo May 9 '16 at 18:34
  • I had only section in mind, of course, I am aware that my solution is not extendible to other section levels ;-) – user31729 May 9 '16 at 22:11
  • @ChristianHupfer - I'm definitely not criticizing your approach at all. For sure, the OP indicated an interest only in modifying the formatted section counter, and nothing else. – Mico May 9 '16 at 22:17
4

Under regular documents, using \pdfstrcmp (e-TeX) should work:

enter image description here

\documentclass{article}

\makeatletter
\renewcommand{\@seccntformat}[1]{%
  \csname the#1\endcsname% Print sectional counter
  \ifnum\pdfstrcmp{#1}{section}=0 .\fi% If \section, print .
  \quad% Space between number and title
}
\makeatother

\begin{document}

\section{A section}
\subsection{A subsection}
\subsubsection{A subsubsection}

\end{document}

Since \@seccntformat receives a single argument for the sectional unit counter, we string-compare the argument against section. \pdfstrcmp{<strA>}{<strB>} returns 0 if <strA> matches <strB>.

3

With a KOMA-Script class there is no need to redefine an internal command. You only have to redefine \sectionformat:

\renewcommand\sectionformat{\thesection.\enskip}

But maybe there should be also a dot between between section number and section title in the page header:

\renewcommand\sectionmarkformat{\thesection.\enskip}

and in TOC

\RedeclareSectionCommand[
  tocentrynumberformat=\tocentrynumberwithdot
]{section}
\newcommand\tocentrynumberwithdot[1]{\renewcommand\autodot{.}#1}

enter image description here

Code:

\documentclass[
  numbers=noenddot% <- no dot at the end of numbers
]{scrartcl}
% replace \autodot by . for section titles in document
\renewcommand\sectionformat{\thesection.\enskip}

% replace \autodot by . for section titles in page header
\renewcommand\sectionmarkformat{\thesection.\enskip}

% % replace \autodot by . for section titles in TOC
\RedeclareSectionCommand[
  tocentrynumberformat=\tocentrynumberwithdot
]{section}
\newcommand\tocentrynumberwithdot[1]{\renewcommand\autodot{.}#1}

\pagestyle{headings}
\usepackage{blindtext}% only for dummy text
\begin{document}
\tableofcontents
\blinddocument
\end{document}
1

No \ifs required.

\documentclass{article}
\makeatletter
\def\sectionsuffix      {.}
\def\subsectionsuffix   {}
\def\subsubsectionsuffix{}
\def\paragraphsuffix    {}

\renewcommand\@seccntformat[1]{\csname the#1\endcsname%
  \csname#1suffix\endcsname\quad}
\makeatother
\begin{document}
\section{My section\label{s:B}}
\subsection{My subsection\label{s:BB}}
\subsubsection{My subsubsection}
In sections \ref{s:B} and \ref{s:BB}, a refed section does NOT end in a dot, which is essential.
\end{document}

enter image description here

0

Just another variation of the same basic idea. It has the slight advantage that the \ifx test is purely expandable, should this ever be a concern.

\documentclass[a4paper]{article}

\makeatletter

\renewcommand*{\@seccntformat}[1]{%
  \csname the#1\endcsname
  \expandafter\ifx\csname c@#1\endcsname \c@section
    .%
  \fi
  \quad
} 


\makeatother



\begin{document}

\section{Foo}
Contents of section Foo

\subsection{Bar}
Contents of subsection Bar

\end{document}

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.