8

I have an article divided into sections and subsections. I need to insert a subsection "5.2a" between "5.2" and "5.3". What is the easiest way to do this?

6

If it is just for just one time, the easies way would be to redefine the representation for the subsection counter, and then revert to the original definition:

\documentclass{article}

\begin{document}

\section{Test Section}
\subsection{Test Subsection One}
\addtocounter{subsection}{-1}
\renewcommand\thesubsection{\thesection.\arabic{subsection}a}
\subsection{Test Subsection One A}\label{sec:test}
\renewcommand\thesubsection{\thesection.\arabic{subsection}}
\subsection{Test Subsection Two}

\end{document}

enter image description here

  • instead of re-resetting after the changed subsection, one could precede the initial \addtocounter by \begingroup and replace the second \renewcommand\thesubsection by \endgroup. the changed heading will go into the table of contents. (and yes, i know these are "plain" tex commands, not latex, but they're more visible than braces, which could also be used.) – barbara beeton Mar 22 '13 at 13:52
  • Thanks, as this is a one-off I think I'll go for the quick and dirty solution! – ajd Mar 23 '13 at 5:51
5

A more general solution is to do the redefinition without any assumption about what \thesubsection expands to.

\documentclass{article}

%\usepackage{hyperref} % uncomment for testing with it

\makeatletter
\newcommand{\specialsubsection}[1]{%
  \def\@specialsubsectionsuffix{#1}%
  \@dblarg\@specialsubsection
}
\def\@specialsubsection[#1]#2{%
 \begingroup
 \expandafter\def\expandafter\thesubsection\expandafter{%
   \thesubsection\@specialsubsectionsuffix}%
 \@ifundefined{theHsubsection}%
   {}%
   {\expandafter\def\expandafter\theHsubsection\expandafter{%
      \theHsubsection\@specialsubsectionsuffix}}%
 \addtocounter{subsection}{-1}%
 \subsection[#1]{#2}%
 \endgroup
}
\makeatother

\begin{document}

\section{Test Section}
\subsection{Test Subsection One}

\specialsubsection{a}{Test Subsection One A}\label{sec:test}

\specialsubsection{b}[For toc]{Test Subsection One B}

\subsection{Test Subsection Two}

\end{document}

This allows also for a different TOC entry, with the usual optional argument (after the suffix).

The \theHsubsection bit is to make the code work with hyperref.

enter image description here

  • Looks excellent, but it doesn't appear to work nicely with \ref. – ajd Mar 23 '13 at 5:51

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.