Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

So I'm typesetting a document, and I go through entries 0 to 99 without trouble, then discover this person has sections 100a and 100. Well, I can get two section 100s easily enough by adding \setcounter{section}{99} after the first section 100, but how do I add section 100a without screwing up the other 150 section in the document?

So to be clear, the structure is:

0
1
2
...
98
99
100a
100
101
...
151

Edit with additional details: I'm using the article class, as I've never figured out what the others are for, and am indeed using hyperref.

share|improve this question
    
Are you using hyperref as well? What \documentclass are you using? –  Werner Aug 20 '13 at 2:04
    
@Werner Damn, should have put in an example: article (all I ever use) and yes. –  Canageek Aug 20 '13 at 6:21
    
And of course, I go to the source document and discover the version I was using has a mistake and it should be 100a and 100b after all. Ah well, thanks anyway. –  Canageek Nov 19 '13 at 4:40

4 Answers 4

up vote 16 down vote accepted

Stretching a bit the syntax, we can define \section+{title} to add a letter to the previous section number. I exploit the fact that \alph{secondary} expands to nothing when the secondary counter has value zero.

In the example I use hyperref, which is not needed. Just add the code after loading hyperref, if wanted.

\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref,bookmark}

\let\latexsection\section
\newif\ifsecondary
\RenewDocumentCommand{\section}{s}
 {%
  \IfBooleanTF{#1}{\latexsection*}{\newsection}%
 }

\newcounter{secondary}
\AtBeginDocument{%
  \expandafter\def\expandafter\thesection\expandafter
    {\thesection\alph{secondary}}%
  \ifdefined\theHsection % to keep hyperref happy
    \let\theHsection\thesection
  \fi
}

\NewDocumentCommand{\newsection}{t+ o m}
 {%
  \IfBooleanTF{#1}
    {\global\secondarytrue}
    {\global\secondaryfalse
     \ifnum\value{secondary}=0 \else
       \setcounter{secondary}{0}%
     \fi}
  \ifsecondary
    \addtocounter{section}{-1}%
    \stepcounter{secondary}%
  \fi
  \IfNoValueTF{#2}{\latexsection{#3}}{\latexsection[#2]{#3}}%
 }

\begin{document}

\section{Should be one}\label{one}

\section*{No number}

\section{Should be two}\label{two}
\section+{Should be two-a}\label{two-a}
\section+{Should be two-b}\label{two-b}

\section{Should be three}\label{three}

References: \ref{one}, \ref{two}, \ref{two-a}, \ref{two-b}, \ref{three}

\end{document}

enter image description here


If you need to have the secondary section before the normal one (although this doesn't make any sense), here's a way:

\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref,bookmark}

\let\latexsection\section
\newif\ifsecondary
\RenewDocumentCommand{\section}{s}
 {%
  \IfBooleanTF{#1}{\latexsection*}{\newsection}%
 }

\newcounter{secondary}
\AtBeginDocument{%
  \expandafter\def\expandafter\thesection\expandafter
    {\thesection\alph{secondary}}%
  \ifdefined\theHsection
    \let\theHsection\thesection
  \fi
}

\NewDocumentCommand{\newsection}{t+ o m}
 {%
  \IfBooleanTF{#1}
    {\global\secondarytrue}
    {\global\secondaryfalse
     \ifnum\value{secondary}=0 \else
       \setcounter{secondary}{0}%
     \fi}
  \ifsecondary
    \stepcounter{secondary}%
  \fi
  \IfNoValueTF{#2}{\latexsection{#3}}{\latexsection[#2]{#3}}%
  \ifsecondary
    \addtocounter{section}{-1}%
  \fi
 }

\begin{document}

\section{Should be one}\label{one}

\section*{No number}

\section{Should be two}\label{two}
\section+{Should be three-a}\label{three-a}
\section+{Should be three-b}\label{three-b}

\section{Should be three}\label{three}

References: \ref{one}, \ref{two}, \ref{three-a}, \ref{three-b}, \ref{three}

\end{document}

enter image description here

share|improve this answer
    
And it turns out the first example is in fact what I need, with a little modification. Thank you. –  Canageek Nov 19 '13 at 4:43
    
And by a little modification, I meant one line, in the 2nd version of the program. This took me 44 minutes to figure out. –  Canageek Nov 19 '13 at 5:28

Instead of setting the counter to 100, I would start by adding -1 after the special section so that it works after moving around (it may end up as 75a after all).

You can simply re-define \thesection which seems to be setup as \arabic{section}.

If you re-define it with

\renewcommand*{\thesection}{\arabic{section}a}

you get an a after the number. To be independent of the \arabic part (it could be also Roman numbering), you can do

\expandafter\def\expandafter\thesection\expandafter{\thesection a}

or with etoolbox

\appto\thesection{a}

Doing this in a group saves you from restoring the initial value (which may also be done with a few \lets).


A command that works with all counters can be defined as

\newcommand*{\addToTheCounter}[2]{%
  \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\csname the#1%
  \expandafter\expandafter\expandafter\endcsname
  \expandafter\expandafter\expandafter{\csname the#1\endcsname#2}}

or with etoolbox

\newcommand*{\addToTheCounter}[2]{\csappto{the#1}{#2}}

Code

\documentclass{article}
\usepackage{lipsum,hyperref,etoolbox}
\newcommand*{\addToTheCounter}[2]{%
  \csappto{the#1}{#2}%
  \csappto{theH#1}{#2}} % for hyperref
\begin{document}
\tableofcontents
\setcounter{section}{8}
\section{9}
\lipsum[2]
{
  \addToTheCounter{section}{a}
  \section{10a}\label{weird}
} \addtocounter{section}{-1}
\lipsum[2]
\section{10}\label{weird2}
\lipsum[2]

see \ref{weird} and \ref{weird2}
\section{11}
\lipsum[2]
\end{document}

Output

enter image description here

share|improve this answer

\thesection can be redefined appropriately:

\documentclass{article}

\renewcommand*{\thesection}{%
  \ifnum\value{section}<102 %
    \the\numexpr\value{section}-1\relax
  \else
    \ifnum\value{section}=102 %
      100a%
    \else
      \the\numexpr\value{section}-2\relax
    \fi
  \fi
}

% Increase the space for the section number in the table of contents
\usepackage{etoolbox}
\newlength\tocnumwidth
\settowidth{\tocnumwidth}{\textbf{100a} }
\expandafter\patchcmd\csname l@section\endcsname{1.5em}{\tocnumwidth}{}{}

\usepackage[colorlinks,bookmarksnumbered]{hyperref}% for testing

\begin{document}
\tableofcontents
\bigskip
\hrule

\section{First section}\label{first}
\section{Next section}\label{next}

\section*{\dots}
\addcontentsline{toc}{section}{\dots}
\setcounter{section}{99}

\section{Luftballons}\label{99}
\section{Lorem}\label{lorem}
\section{Ipsum}\label{ipsum}
\section{Dolor}\label{dolor}
\hrule
\bigskip

Reference list: \ref{first}, \ref{next}, \dots,
  \ref{99}, \ref{lorem}, \ref{ipsum}, \ref{dolor}.
\end{document}

Result

share|improve this answer
1  
+1 for throwing in a cute pop culture reference :-) –  Mico Aug 20 '13 at 5:49

Just for comparison here are a few ways of achieving this with ConTeXt. I have set the special section to be 3 rather than 100. All of these ensure that the numbering will work for references and table of contents.

Define custom conversion

If this is something that only needs to be done for sections whose number is already known, then you can define a custom conversion; the easiest way to do that is using Lua.

\startluacode
  commands.weirdcounter = function (number, offset) 
    if number < offset then
      context("%s", number)
    elseif number == offset then
      context("%sa", offset)
    else
      context("%s", number - 1)
    end
  end
\stopluacode

\define[1]\weirdcounter{\ctxcommand{weirdcounter(#1,3)}}

\defineconversion[weirdnumbering][\weirdcounter]

\setuphead[section][conversion=weirdnumbering]

\setupinteraction[state=start]

\starttext

\placecontent

\dorecurse{5}
  {\expanded{\section{Section \recurselevel}}}

\stoptext

Use a coupled heading command with ownnumber

For a one off use, you can define a new section head, say ownsection that is coupled with section (so that you inherit style etc), but allows you to set the section number. For example:

\setuphead[section][conversion=weirdnumbering]

\definehead[ownsection][section][ownnumber=yes]

\setupinteraction[state=start]

\starttext

\placecontent

\section{One}
\section{Two}
\ownsection{3a}{Three}
\section{Four}
\section{Five}

\stoptext
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.