1

I added a custom sectioning block to my document following the instructions I found on this question: Defining custom sectioning commands. Below is a mvce:

\documentclass[12pt]{article}
\usepackage{titlesec}
    % Define a new \question sectioning element
    \titleclass{\question}{straight}[\section]
        \newcounter{question}
    \titleformat{\question}[runin]{\normalfont\bfseries}{QUESTION \thesection .\thequestion:}{0.5em}{}
    \titlespacing*{\question}{0pt}{1ex plus 1ex minus .2ex}{2ex plus .2ex}
    \newcommand{\questionautorefname}{question}

\begin{document}

\section{The First Section}
\question{} Who am I?
\question{} Who are you?

\section{The Second Section}
\question{} Who are we?
\question{} Who are they?

\end{document}

Which produces:

Counter sectioning mvce

Now I am wondering if there's a way to automatically reset the counter after I declare the section so to display Question 2.1 as opposed to Question 2.3, without having to call \resetcounter{} manually each time?

JoErNanO
  • 145
  • 9

1 Answers1

2

There are some ways to achieve this:

\newcounter{question}[section] will solve all troubles most likely, however. (Too easy?)

If this is not possible, you can use the package chngcntr for this and hook the question counter into the section counter reset list with

\counterwithin*{question}{section}

since you are using the output already as \thesection .\thequestion, so you need \counterwithin*{...}{...} instead of \counterwithin{...}{...}

Alternatively use the LaTeX core command \@addtoreset

\makeatletter
\@addtoreset{question}{section}
\makeatother

in the preamble. You don't need the chngcntr package then.

\documentclass[12pt]{article}

\usepackage{chngcntr}


\usepackage{titlesec}
    % Define a new \question sectioning element
    \titleclass{\question}{straight}[\section]
        \newcounter{question}
    \titleformat{\question}[runin]{\normalfont\bfseries}{QUESTION \thesection .\thequestion:}{0.5em}{}
    \titlespacing*{\question}{0pt}{1ex plus 1ex minus .2ex}{2ex plus .2ex}
    \newcommand{\questionautorefname}{question}

\counterwithin*{question}{section}

\begin{document}

\section{The First Section}
\question{} Who am I?
\question{} Who are you?

\section{The Second Section}
\question{} Who are we?
\question{} Who are they?

\end{document}

enter image description here