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:

  \addcontentsline{toc}{chapter}{\numberline {\cftchaptername#1}#2}  
  \addcontentsline{toc}{section}{\numberline {#1}#2}  

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.


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:

% ...


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

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

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.


Here's an attempt using the regular chapter/section numbers as problem/subproblem numbers respectively. If that's not problem (because you're using sections and chapters in your document as well) you might consider overriding paragraph/subparagraph or even defining all new commands.

If there's anything that's not straightforward, please ask for clarification in the comments.




hello problem \theproblem
foo \thesubproblem
bar \thesubproblem
hello \theproblem
foo \thesubproblem
bar \thesubproblem
hello \theproblem
foo \thesubproblem
bar \thesubproblem
  • Thanks for the response, Will (and sorry for the delay in reviewing these answers). I really like the simplistic approach and the use of existing counters. I had been afraid to mess them up, but this made me realize that I can futz with them more than I thought. It does, however, come at the drawback that I cannot use arbitrary text as a question number. Also, using a very large number still causes the number field in the TOC to overflow; I had presumed that LaTeX would take care of this with the proper counters, but I suppose that is wrong. I'm going to keep looking into this. Thanks again! – mbauman Dec 22 '10 at 3:42

Obligatory ConTeXt solution:


Then, for each section head, you must specify the number using

\section[reference]{12(e)}{Fermat's Last Theorem}

This works seamlessly with markings (for headers and footers) and lists (for table of contents).

EDIT: Following Will's comment, to number a particular section by your own number, you can use the start-stop variant of sections.

\startsection[title=First Section]
... content of section ...

\startsection[title=Second Section, ownnumber={12(e)}]
.... content of section ...

\startsection[title=Third Section]
.... content of section ....
.... this section is numbered 3 ...

Again, marking and list are taken care of automatically.

  • I don't suppose you can omit the section/subsection number when it follows sequentially? (Following from the ‘optionally’ requirement in the title of the question.) – Will Robertson Dec 6 '10 at 13:20
  • You can, but that needs a slightly different syntax. I am updating the answer accordingly. – Aditya Dec 6 '10 at 14:29
  • Thanks for the pointer to ConTeXt (and my apologies for the delay in my response). I had heard of it before but hadn't had an opportunity (or excuse) to check it out. It looks great, but I'm stuck with LaTeX for my thesis and paper submissions, and I'm using this as a learning exercise to get better. Perhaps, though, that for personal work and one-offs (such as homework assignments as these) ConTeXt is the better tool for the job. – mbauman Dec 22 '10 at 3:52

Try this:

   \addcontentsline{toc}{section}{Problem #1: #2}
   Problem #1\quad #2.\qquad


 \section{The problems}

  Just an exercise.
 \begin{problem}[12(e)]{Fermat's Last Theorem}
  $a^n + b^n \neq c^n$ for $n > 2$.

 The first problem is \ref{p:exercise}, and the second is \ref{p:fermat}.


Moral: just because it says \theproblem doesn't mean what it writes has to have anything to do with the value of the counter problem.

EDIT: I used environments out of habit, because in my own homeworks I would always use amsthm to state the problems. Since \endproblem is vacuous anyway you can easily turn this into a more \section-like command and have its counter default to \thesection rather than \theproblem. If you want something that really looks section-like then you should use \@startsection to define it; see source2e.pdf (file ltsect.dtx) for the syntax.

  • Thank you for the response, Ryan. I particularly appreciate your edit -- the pointer to amsthm and @startsection are both very valuable. I'm going to keep looking into this. – mbauman Dec 22 '10 at 4:01

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.