[Tex/LaTex] An extra dot after section counter while numbering equations

equationsnumberingpunctuationsectioning

My sections are numbered with a dot after counter. I get it like this

\renewcommand{\thesection}{\arabic{section}.}

I also need the equations to be numbered within sections and I try to do it like this

\numberwithin{equation}{section}

The result is that I get numbers of equations with an extra dot, e.g. (1..1) instead of (1.1).

How can I fix this?

Best Answer

If you add a closing dot the the definition of \thesection, you should also remove the "middle" dot from definitions using \thesection, especially \theequation and \thesubsection. (Note that your redefinition of \thesection will also produce a closing dot in the table of contents, the header/footer, and cross references.)

\documentclass{article}

\usepackage{amsmath}

\renewcommand{\thesection}{\arabic{section}.}
\numberwithin{equation}{section}

\renewcommand{\theequation}{\thesection\arabic{equation}}
\renewcommand{\thesubsection}{\thesection\arabic{subsection}}

\begin{document}

\section{foo}

\subsection{bar}

\begin{equation}
a^2 + b^2 = c^2
\end{equation}

\end{document}

EDIT: In response to Frank Mittelbach, here's an alternative that changes the \@seccntformat macro so that it adds a dot if the new \@seccntdot conditional is true. The etoolbox package is used to set this conditional to true before \section and to false after every equal/lower-level sectioning command.

\documentclass{article}

\usepackage{amsmath}

\numberwithin{equation}{section}

\usepackage{etoolbox}

\makeatletter

\newif\if@seccntdot

\pretocmd{\section}{\@seccntdottrue}{}{}
\apptocmd{\@xsect}{\@seccntdotfalse}{}{}

\def\@seccntformat#1{%
  \csname the#1\endcsname
  \if@seccntdot .\fi
  \quad
}

\makeatother

\begin{document}

\section{foo}

\subsection{bar}

\begin{equation}
a^2 + b^2 = c^2
\end{equation}

\end{document}

Output for both examples:

enter image description here