[Tex/LaTex] [Optionally] Specifying arbitrary numbers for sections and subsections

numberingsectioningtable of contents

I'm working on a package to simplify writing my homework and assignments. Unfortunately, not all of my professors assign problem sets with sequential numbering. Thus, I'd like to optionally be able to manually override the automatic numbering of sections.

My goals:

  • A syntax that resembles \section[12(e)]{Fermat's Last Theorem}, where the custom number is in brackets. I'm imagining that I'd ignore the normal section counters and simply define my own macro to contain the manually specified number. In the absence of an optional argument, this macro would simply mirror \thesection.

  • The ability to reference my custom number in headings and footers. I'd like to say something like "Question 12(e) continued on next pageā€¦" instead of referencing the whole section name.

  • And, finally, I want to have all of this work in a table of contents and the hyperref package.

Is this possible? Has it been done already? I'm not tied down to a particular document class; I've attempted hacking this onto both article and Memoir's article mode.

This gets me close:

\documentclass[article]{memoir}
\newcommand*{\theproblem}{}
\renewcommand*{\cftchaptername}{Problem\space}
\renewcommand*{\chaptername}{Problem\space}
\setcounter{tocdepth}{2}
\newcommand*{\problem}[2][\thechapter]{%
  \addtocounter{chapter}{1}
  \setcounter{section}{0}
  \phantomsection
  \addcontentsline{toc}{chapter}{\numberline {\cftchaptername#1}#2}  
  \chapter*{\chaptername#1\quad#2}
  \renewcommand*{\theproblem}{#1}
}
\newcommand*{\subproblem}[2][\thesection]{%
  \addtocounter{section}{1}
  \phantomsection
  \addcontentsline{toc}{section}{\numberline {#1}#2}  
  \section*{#1\quad#2}
  \renewcommand*{\theproblem}{#1}
}

Unfortunately, it doesn't account for longer-than-anticipated numbers in the table of contents. Also, I initially started trying to directly redefine \chapter and \section; this is definitely not necessary, but it would be quite nice (and now I'm curious if it could be possible). And, finally, this is my first bit of package hacking, so I'm not entirely confident I'm doing things in a reasonable manner.

Best Answer

Phew! I think I've finally learned enough about (La)TeX to get a decent solution to this.

I rather thoroughly hack the article class to accomplish it, so I've placed the following in a custom class that bootstraps off article.cls:

% ...
\LoadClass{article}
\RequirePackage{etoolbox}

\let\@@sect\@sect

\def\@sect#1#2#3#4#5#6[#7]#8{ %
  \@sectsplit{#1}{{#2}{#3}{#4}{#5}{#6}}[#7||]{#8}
}

\def\@sectsplit#1#2[#3|#4|#5]#6{ %
  \ifcsundef{@theorig#1}
    {\expandafter\edef\csname @theorig#1\endcsname{\expandafter\expandonce\csname the#1\endcsname}}
    {\relax}
  \ifstrempty{#4}
  {
    \expandafter\edef\csname the#1\endcsname{\expandafter\noexpand\csname @theorig#1\endcsname}
    \@@sect{#1}#2[{#3}]{#6}
  }{
    \expandafter\edef\csname the#1\endcsname{#3}
    \@@sect{#1}#2[{#4}]{#6}
  }
}

It looks more complicated than it is due to the deferred expansion and the complexity of \@sect. It simply augments the standard \section[TOC name]{section name} syntax with an optional prefix: \section[number|TOC name]{section name}.

I place the custom number (if provided) directly into \thesection. This means that subsections pick it up appropriately (if they were defined with reference to \thesection). But I also save the previous value of \thesection, so that the user's preference of eg. arabic or roman numerals is preserved.

Note, too, that this works equally well on all section levels. There is one bug I've noticed: \section[12(e)|]{Fermat's Last Theorem} parses 12(e) as the TOC title, not the custom number.

Related Question