2

I've been working on an assignment, and two of the questions are very similar and it's easier to write about them both in the same section. The problem with this is it screws the numbering up:

1 Section Title
(Question 1)
2 Section Title
(Question 2 and 3) 
3 Section Title
(Question 4)

My current approach is:

1 Section Title
(Question 1)
2 Section Title
(Question 2 and 3) 
\setcounter{section}{3}
4 Section Title
(Question 4)

Ideally, what I'd like to do is:

1 Section Title
(Question 1)
2-3 Section Title
(Question 2 and 3) 
4 Section Title
(Question 4)

Is this possible? I understand that the section counters are based on integers, but is there a way to override their visual representation?

4

The visual representation of the section counter is specified by macro \thesection, which can be redefined:

\section{Section Title}
(Question 1)

\let\savedthesection\thesection
\renewcommand*{\thesection}{%
  \arabic{section}--\the\numexpr\value{section}+1\relax
}
\section{Section Title}
\stepcounter{section}
\let\thesection\savedthesection
(Question 2 and 3)

\section{Section Title}
(Question 4)

Full example including patch for the table of contents to increase the spacing for the sections.

\documentclass{article}

% Patch section entries in table of contents to get
% more space for the section numbers
\usepackage{etoolbox}
\makeatletter
\patchcmd\l@section{1.5em}{2.5em}{}{%
  \errmessage{\noexpand\l@section could not be patched}%
}
\makeatother

\begin{document}

\tableofcontents

\section{First question}

\let\savedthesection\thesection
\renewcommand*{\thesection}{%
  \arabic{section}--\the\numexpr\value{section}+1\relax
}
\section{Second and third question}
\stepcounter{section}
\let\thesection\savedthesection

\section{Fourth question}

\end{document}

Result

Macro \rangesection

The following example defines \rangesection with the following arguments:

\rangesection{<number of additional sections>}[<toc entry>]{<section title>}

Also different number schemes are supported.

\documentclass{article}

% Patch section entries in table of contents to get
% more space for the section numbers
\usepackage{etoolbox}
\makeatletter
\patchcmd\l@section{1.5em}{2.5em}{}{%
  \errmessage{\noexpand\l@section could not be patched}%
}

\newcommand*{\rangesection}[1]{%
  \@dblarg{\@rangesection{#1}}%
}
\def\@rangesection#1[#2]#3{%
  \let\saved@thesection\thesection
  \stepcounter{section}%
  \protected@edef\from@thesection{\thesection}%
  \addtocounter{section}{#1}%
  \protected@edef\to@thesection{\thesection}%
  \addtocounter{section}{-1}%
  \renewcommand*{\thesection}{%
    \from@thesection--\to@thesection
  }%
  \section[{#2}]{#3}%
  \let\thesection\saved@thesection
}
\makeatother

\begin{document}

\tableofcontents

\noindent\dotfill

\section{First question}

\rangesection{1}{Second and third question}

\section{Fourth question}

\renewcommand*{\thesection}{\Alph{section}}
\setcounter{section}{0}

\section{First appendix question}

\rangesection{4}{Second to sixth appendix question}

\section{Seventh appendix question}

\end{document}

Result

| improve this answer | |
  • Thanks @Heiko. You've mis-spelled \savedthesection in the second instance of the first example. I can't do single character edits to change it for you. Otherwise this worked perfectly! – Darkstarone May 20 '16 at 23:16
  • 1
    @Darkstarone Thanks. I had fixed it in my test file, but forgotten to update the answer. – Heiko Oberdiek May 20 '16 at 23:17
  • Also the \renewcommand line seems to be cut of in the full example. – Ruben May 20 '16 at 23:32
  • yw! Maybe it would be nice to add an additional version where the whole process juggling with \thesection is wrapped into a command named \rangedsection or similar. +1 anyways ;) – Ruben May 20 '16 at 23:42
  • @Ruben Done. Also I have added support for different section number formats. – Heiko Oberdiek May 21 '16 at 0:44

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.