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.

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.

share|improve this question
add comment

4 Answers 4

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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.

\documentclass[article]{memoir}
\usepackage{calc}

\newcommand\theproblem{\arabic{chapter}}
\newcommand\thesubproblem{\arabic{chapter}(\arabic{section})}
\renewcommand\thesection{\arabic{chapter}(\arabic{section})}

\renewcommand*{\cftchaptername}{Problem\space}
\renewcommand*{\chaptername}{Problem\space}
\setcounter{tocdepth}{2}

\newcommand*{\problem}[2][\value{chapter}+1]{%
  \setcounter{chapter}{#1-1}
  \chapter{#2}
}
\newcommand*{\subproblem}[2][\value{section}+1]{%
  \setcounter{section}{#1-1}
  \section{#2}
}
\begin{document}
\tableofcontents*
\problem[2]{problem}
hello problem \theproblem
\subproblem[2]{subproblem}
foo \thesubproblem
\subproblem{subproblem}
bar \thesubproblem
\problem[4]{problem}
hello \theproblem
\subproblem[3]{subproblem}
foo \thesubproblem
\subproblem{subproblem}
bar \thesubproblem
\problem{problem}
hello \theproblem
\subproblem[7]{subproblem}
foo \thesubproblem
\subproblem{subproblem}
bar \thesubproblem
\end{document}
share|improve this answer
    
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! –  Matt B. Dec 22 '10 at 3:42
add comment

Obligatory ConTeXt solution:

\setuphead[ownnumber=yes]

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.

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

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

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

Again, marking and list are taken care of automatically.

share|improve this answer
    
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. –  Matt B. Dec 22 '10 at 3:52
add comment

Try this:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
 \newcounter{problem}
 \setcounter{problem}{0}
 \newenvironment{problem}[2][\theproblem]
  {\refstepcounter{problem}
   \edef\tmpa{#1}
   \let\theproblem=\tmpa
   \phantomsection
   \addcontentsline{toc}{section}{Problem #1: #2}
   Problem #1\quad #2.\qquad
  }
  {}

 \tableofcontents

 \section{The problems}

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

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

\end{document}

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.

share|improve this answer
    
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. –  Matt B. Dec 22 '10 at 4:01
add comment

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.