5

How can I create a counter that when used under a section to count as follows

1.1

1.2

1.3

where the 1 is the section number and when used under a subsection to produce

1.1.1

1.1.2

1.1.3

where 1 is the section number and 1 is the subsection number?

The following is not a MWE. It doesn't compile, it is used to illustrate the use I am looking for.

\documentclass{article}

\begin{document}

\section{Section 1}

\Counter

% Should produce 1.1

\subsection{Subsection 1}

\Counter

% Should produce 1.1.1

\subsection{Subsection 2}

\Counter

% Should produce 1.2.1

\section{Section 2}

\Counter

% Should produce 2.1

\subsection{Subsection 1}

\Counter

% Should produce 2.1.1

\end{document}
4

Define the counter, say, acntr to be in the reset list of subsection and make a query within \theacntr whether the current subsection counter value is 0 or otherwise.

If 0 use \thesection.\arabic{acntr}, otherwise use \thesubsection.\arabic{acntr}.

Since \stepcounter{section} or \refstepcounter{section} will also reset the subsection counter, the correct value is given at least with LaTeX kernels later than April 2015, otherwise the (outdated) fixltx2e package must be loaded.

\documentclass{article}

\newcounter{acntr}[subsection]

\makeatletter

\renewcommand{\theacntr}{\ifnum0=\c@subsection\thesection.\arabic{acntr}\else\thesubsection.\arabic{acntr}\fi}
\makeatother

\newcommand{\Counter}{%
  \refstepcounter{acntr}%
  \theacntr%
}  

\begin{document}

\section{Section 1}

1.1 $\to$ \Counter

% Should produce 1.1

\subsection{Subsection 1}

1.1.1 $\to$ \Counter

% Should produce 1.1.1

\subsection{Subsection 2}

1.2.1 $\to$ \Counter

% Should produce 1.2.1

\section{Section 2}


2.1 $\to$ \Counter
% Should produce 2.1

\subsection{Subsection 1}


2.1.1 $\to$ \Counter
% Should produce 2.1.1

\end{document}

enter image description here

  • I use ShareLaTeX and without the package fixltx2e it doesn't reset the automatically. I don't think that they use something outdated though... – Adam Jun 12 '17 at 18:10
  • I am sorry I deleted my first comment as it worked with the package fixltx2e. – Adam Jun 12 '17 at 18:10
  • @Adam: ShareLaTeX is ... well, I don't recommend it ;-) – user31729 Jun 12 '17 at 18:10
  • Ok thank you very much though. :) – Adam Jun 12 '17 at 18:11
  • @Adam: ShareLaTeX is using an outdated version of TeX Live (2014). Overleaf is a little more up-to-date. – Werner Jun 12 '17 at 22:48
3

The following is a little more verbose than Christian's answer, defining a macro that provides a way of conditioning whether you've called a \section or a \subsection, using the appropriate counter representation where needed:

enter image description here

\documentclass{article}

\let\oldsection\section
\let\oldsubsection\subsection
\renewcommand{\section}{\def\cursectioning{section}\oldsection}
\renewcommand{\subsection}{\def\cursectioning{subsection}\oldsubsection}

\newcounter{seccounter}[subsection]
\renewcommand{\theseccounter}{\csname the\cursectioning\endcsname.\arabic{seccounter}}

\newcommand{\Counter}{\stepcounter{seccounter}\theseccounter}

\begin{document}

\section{Section 1}

\Counter

% Should produce 1.1

\subsection{Subsection 1}

\Counter

% Should produce 1.1.1

\subsection{Subsection 2}

\Counter

% Should produce 1.2.1

\section{Section 2}

\Counter

% Should produce 2.1

\subsection{Subsection 1}

\Counter

% Should produce 2.1.1

\end{document}
  • +1 It is a nice answer. Also it is very informative. :) – Adam Jun 13 '17 at 4:10

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.