8

I want to set a counter like picture

enter image description here

I only can do

\documentclass[a4paper, 12pt]{article}
\usepackage{fouriernc}
\usepackage{amsmath}\usepackage[thmmarks,standard,thref]{ntheorem}
\pagestyle{empty}

\theoremseparator{.}
\theorembodyfont{\upshape}
%\theorembodyfont{\normalfont}
\newtheorem{pro}{Problem}
\begin{document}
\begin{pro}
This is a Problem. 
\end{pro}
\begin{pro}
This is a Problem. 
\end{pro}
\begin{pro}
This is a Problem. \end{pro}
\begin{pro}
This is a Problem.
\end{pro}
\begin{pro}
This is a Problem.
\end{pro}
\begin{pro}
This is a Problem.
\end{pro}
\end{document} 

How to set a counter like this picture?

0
12

enter image description here

\documentclass[a4paper, 12pt]{article}
\usepackage{fouriernc}
\usepackage{amsmath}\usepackage[thmmarks,standard,thref]{ntheorem}
\pagestyle{empty}

\theoremseparator{.}
\theorembodyfont{\upshape}
%\theorembodyfont{\normalfont}
\newtheorem{pro}{Problem}
\newcounter{probtype}
\renewcommand\theprobtype{\alph{probtype}}
\renewcommand\thepro{\arabic{pro}\theprobtype}

\begin{document}
\begin{pro}
This is a Problem. 
\end{pro}
\begin{pro}
This is a Problem. 
\end{pro}
\setcounter{probtype}{1}
\begin{pro}
This is a Problem. \end{pro}
\begin{pro}
This is a Problem.
\end{pro}
\setcounter{probtype}{2}
\begin{pro}
This is a Problem.
\end{pro}
\setcounter{probtype}{0}
\begin{pro}
This is a Problem.
\end{pro}
\end{document} 
1
  • Wow! Very nice and simple, +1. – kan Apr 24 '13 at 8:49
7

I am pretty sure I learnt this trick from egreg on the chatroom. Here goes my solution (I'll try to come up with a little more sophisticated solution, but no promises there). Note that this is not very amenable if you are also looking forward to using the \label-\ref mechanism.

\documentclass{amsart} 
\usepackage{amsthm}
\theoremstyle{plain}
\newtheorem*{proaux}{Problem \protect\pronumber}
\newenvironment{pro}[1]{\def\pronumber{#1}\proaux}{\endproaux}
\begin{document}
\begin{pro}{1}
  This is a problem!
\end{pro}
\begin{pro}{2a}
  This is a problem!
\end{pro}
\begin{pro}{3a}
  This is a problem!
\end{pro}
\begin{pro}{2b}
  This is a problem!
\end{pro}
\begin{pro}{3b}\label{pro:3b}
  This is a problem!
\end{pro}
\begin{pro}{4}
  This is a problem!
\end{pro}
In problem \ref{pro:3b}, you should have noticed that 
\end{document}

pro.png

4

Here is a different solution with pgfkeys.

One can:

  • start a sub problem with start sub problem (this prints 2a for example);
  • continue a sub problem with continue sub problem (this prints 2b for example).

These two keys should be used for "continuous" sub problems. If instead, sub problems are no "continuous":

  • start custom sub problem again starts the list, i.e prints 6a;
  • continue sub problem list continues the list, i.e prints 7a;
  • continue custom sub problem changes sub problem, i.e prints 6b.

An example:

\documentclass[a4paper, 12pt]{article}
\usepackage{fouriernc}
\usepackage{amsmath}
\usepackage[thmmarks,standard,thref]{ntheorem}
\usepackage{pgfkeys}
\pagestyle{empty}

\theoremseparator{.}
\theorembodyfont{\upshape}
\newtheorem{prob}{Problem}

\newcounter{subproblem}
\newcounter{auxproblem}

\pgfkeys{/theorem prob/.cd,
  start sub problem/.code={%
    \setcounter{subproblem}{1}
    \renewcommand{\theprob}{\arabic{prob}\alph{subproblem}}
  },
  continue sub problem/.code={
    \stepcounter{subproblem}
    \addtocounter{prob}{-1}
    \renewcommand{\theprob}{\arabic{prob}\alph{subproblem}}
  },
  start custom sub problem/.code={
    \setcounter{subproblem}{1}
    \renewcommand{\theprob}{\arabic{prob}\alph{subproblem}}
    \setcounter{auxproblem}{\value{prob}}
  },
  continue sub problem list/.code={    
    \renewcommand{\theprob}{\arabic{prob}\alph{subproblem}}
  },
  continue custom sub problem/.code={
    \setcounter{prob}{\value{auxproblem}}
    \stepcounter{subproblem}
    \renewcommand{\theprob}{\arabic{prob}\alph{subproblem}}
  },
}

\newenvironment{pro}[1][]{%
  \pgfkeys{/theorem prob/.cd,#1}
  \begin{prob}
}{%
  \end{prob}
}


\begin{document}
\begin{pro}
This is a Problem. 
\end{pro}
\begin{pro}
This is a Problem. 
\end{pro}
\begin{pro}
This is a Problem. 
\end{pro}
\begin{pro}[start sub problem]
This is a Problem.
\end{pro}
\begin{pro}[continue sub problem]\label{pro:4b}
This is a Problem.
\end{pro}
\begin{pro}[continue sub problem]
This is a Problem.
\end{pro}
\begin{pro}
This is a Problem.
\end{pro}
\begin{pro}[start custom sub problem]
This is a Problem.
\end{pro}
\begin{pro}[continue sub problem list]
This is a Problem.
\end{pro}
\begin{pro}[continue sub problem list]
This is a Problem.
\end{pro}
\begin{pro}[continue custom sub problem]
This is a Problem.
\end{pro}
\begin{pro}[continue sub problem list]
This is a Problem.
\end{pro}
\begin{pro}[continue sub problem list]\label{pro:8b}
This is a Problem.
\end{pro}
\begin{pro}[continue custom sub problem]
This is a Problem.
\end{pro}
\begin{pro}[continue sub problem list]
This is a Problem.
\end{pro}
\begin{pro}[continue sub problem list]
This is a Problem.
\end{pro}
\begin{pro}
This is a Problem.
\end{pro}
\begin{pro}
This is a Problem which refers to problem~\ref{pro:4b} and to problem~\ref{pro:8b}.
\end{pro}
\end{document}

The result:

enter image description here

0

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.