[Tex/LaTex] How to redefine the section symbol (§)

symbols

I'm trying to redefine the section symbol (§) so that it automatically places a space after it. When inserting the symbol in text, I typically write \S{} Section Number which gets me the spacing between the § and the Section Number that I'm after. Since I do this every single time I figured I'd just redefine the command.

Here is my attempt:

\documentclass[12pt]{article}

\let\OldS\S
\renewcommand{\S}{\OldS{}}

\begin{document}

\S 13

\OldS 13

\S{} 13

\OldS{} 13

\end{document}

Unfortunately, it doesn't seem that I've changed the behavior of \S at all— both \S and \OldS seem to behave identically.

Why doesn't my attempt produce the desired space between \S and the following text and how can I fix it?

Best Answer

The command \S is defined with \DeclareRobustCommand, so redefining it with \renewcommand is not the best strategy, see When to use \LetLtxMacro?

If you really want a space after \S (which is not usual, I should say), it's better to act at a lower level.

The kernel definition of \S is

% latex.ltx, line 1798:
\DeclareRobustCommand{\S}{\ifmmode\mathsection\else\textsection\fi}

so a better strategy would be

\usepackage{xspace}

\let\S\relax % to avoid spurious warnings
\DeclareRobustCommand{\S}{%
  \ifmmode
    \mathsection
  \else
    \textsection~%
  \fi
}

You surely don't want a line break after §, do you? So ~ is necessary. Using \xspace doesn't guarantee that a line break is not taken at the space.


Note. With

\let\OldS\S
\renewcommand{\S}{\OldS\ }

an \S command that ends up in a caption or other moving argument will result in two spaces. Why?

When LaTeX writes in the .aux file a command \S 1 appearing in a figure caption, it will write

\protect \S \ 1

because the expansion of \OldS is still \protect\S. The same would be written out in the .lof file. When the .lof file is read in, \protect will be ignored, and \S will be interpreted normally as \OldS\; this means two spaces, because the following \ would still be there.


For having “double §”, you can do

\documentclass{article}

\makeatletter
\DeclareRobustCommand{\NS}{%
  \textsection\@ifnextchar\NS{}{~}%
}
\makeatother

\begin{document}

\NS 1 and \NS\NS 2--3

\end{document}

reserving \S for the cases where you don't want a space (I can't see when, though).

However, I believe that the simplest way is to type

\S~1 and \S\S~2--3
Related Question