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 am writing up solutions to qualifying exams at my school, and want to label the sections according to the corresponding solutions. The issue, is that each year there are two quals--one in January, one in August. There is an added complexity in that some years, there are also different exams for the Ph.D. and masters program.

Labeling the sections is no issue, I can just write

\section{Ph.D. January 2007}

The issue, is that I am also using the "theorem" environment, and I want to the theorem environment to reflect these complexities. For example, supposing that my very first section is "Ph.D. August 2004", then I'd like some preamble such that

{\section{Ph.D. August 2004}

\begin{problem} Whatever
\end{problem}

Would output

Ph.D. August 2004


2004.1 (Aug)(Ph.D.): Whatever
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

Here's one possible solution:

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{xstring}
\usepackage{etoolbox}

\newcommand\ExtInf{%
  \StrBetween[1,2]{\sectiontitle}{ }{ }[\pmonth]
  \StrBefore{\sectiontitle}{ }[\ptitle]
  \StrBehind[2]{\sectiontitle}{ }[\pyear]%
}

\declaretheoremstyle[
  spaceabove=\topsep, spacebelow=\topsep,
  headfont=\normalfont\bfseries,
  bodyfont=\normalfont\itshape,
  postheadspace=0em,
  headpunct=,
  headformat=\pyear.\NUMBER~(\pmonth)~(\ptitle):~,
]{mystyle}
\declaretheorem[style=mystyle,name=]{problem}

\makeatletter
\@addtoreset{problem}{section}
\pretocmd{\@sect}{\gdef\sectiontitle{#7}}{}{}
\apptocmd{\@sect}{\ExtInf}{}{}
\makeatother

\begin{document}

\section{Ph.D. August 2004}

\begin{problem}
Test problem.
\end{problem}

\begin{problem}
Test problem.
\end{problem}

\section{M.Sc. September 2010}

\begin{problem}
Test problem.
\end{problem}

\end{document}

enter image description here

Some explanations:

  • The etoolbox packagewas used to capture the title of each section in the macro \sectiontitle.

  • The xstring package was used to extract from the string stored in \sectiontitle the relevant sub-strings; these sub-strings are: the degree, the month, and the year, and they are stored in \ptitle, \pmonth, and \pyear, respectively.

    The macro \ExtInf wraps this process, and etoolbox is used so that \ExtInf is invoked with every new \section.

    The title for each section is expected in the form <degree><space><month><space><year>

  • The thmtools package was used as a front-end to amsthm; with the help of this package, the theorem-like structure problem was defined; using the headformat key, the information about the year, number, month and degree are automatically included with the chosen formatting.

A second simplified version:

This time there's no need to extract substrings; the \ExtInf macro receives three mandatory arguments:

\ExtInf{<title>}{<month>}{<year>}

stores the information in the macros \ptitle, \pmonth, and \pyear and also creates the corresponding \section using the macros; then, thmtools uses those macros in the headformat key to give the desired formatting for the heading of the problem environment:

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{etoolbox}

\newcommand\ExtInf[3]{%
  \gdef\ptitle{#1}
  \gdef\pmonth{#2}
  \gdef\pyear{#3}
  \section{#1~#2~#3}
}

\declaretheoremstyle[
  spaceabove=\topsep, spacebelow=\topsep,
  headfont=\normalfont\bfseries,
  bodyfont=\normalfont\itshape,
  postheadspace=0em,
  headpunct=,
  headformat=\pyear.\NUMBER~(\pmonth)~(\ptitle):~,
]{mystyle}
\declaretheorem[style=mystyle,name=]{problem}

\makeatletter
\@addtoreset{problem}{section}
\makeatother

\begin{document}

\ExtInf{Ph.D.}{August}{2004}

\begin{problem}
Test problem.
\end{problem}

\begin{problem}
Test problem.
\end{problem}

\ExtInf{M.Sc.}{September}{2010}

\begin{problem}
Test problem.
\end{problem}

\end{document}

enter image description here

share|improve this answer
    
That is absolutely amazing! THank you so much! Is there a way to bold everything though? So, \begin{problem} would also bold 2004.1 etc? –  Alex Youcis Nov 29 '12 at 3:05
    
@AlexYoucis sure; should year, number, month and degree all be bold? –  Gonzalo Medina Nov 29 '12 at 3:07
    
@Gonzola Medina: Yes, please! –  Alex Youcis Nov 29 '12 at 3:09
    
@AlexYoucis Done! Please see my updated answer. –  Gonzalo Medina Nov 29 '12 at 3:11
    
@Gonzola Medina: I would upvote you to oblivion if I could. One last request, is there any way to make the actual "problem" italicized. So, if I said \begin{problem} Let $u:\C\to\R$ be harmonic. \end{problem} that "Let $u:\C\to\R$ be harmonic" is italicized? Thanks again! –  Alex Youcis Nov 29 '12 at 3:29
show 4 more comments

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.