3

I want to create two environments, problem and subproblem. I will use the problem environment to define main problems, and subproblem to define variations to the main problem. I'd like to have the subproblem numbering as children of the problem numbering. I mean that I like to have something as

Problem 1: Definition of Problem 1
Problem 2: Definition of Problem 2

Problem 1.1: Definition of the first variation of Problem 1
Problem 1.2: Definition of the second variation of Problem 1
Problem 2.1: Definition of the first variation of Problem 2

and so on.

I created the environment problem by means of the \newtheorem command and the first problem as

\theoremstyle{definition}
\newtheorem{problem}{Problem}

\begin{problem}
    \label{prob1}
    Definition...
\end{problem}

Now I'd like to create similarly the subproblem environment. How can I do, so that the subproblem will reference to the numbering of the associated problem?

2

You have to remember the last “subproblem” number. For this it is mandatory that each problem environment you plan to define variations of has a \label.

When problem* is processed, it looks at the last number referring to the same label and steps it.

You can also label the problem variations, as shown in the example.

\documentclass{article}
\usepackage{amsthm}
\usepackage{refcount}

\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem{subproblem}{Problem}

\newenvironment{problem*}[1]
 {%
  \ifcsname problem@#1\endcsname
    \expandafter\xdef\csname problem@#1\endcsname{%
      \the\numexpr\csname problem@#1\endcsname+1\relax
    }%
  \else
    \expandafter\xdef\csname problem@#1\endcsname{1}%
  \fi
  \edef\thesubproblem{%
    \getrefnumber{#1}.\csname problem@#1\endcsname
  }%
  \subproblem
 }
 {\endsubproblem}

\begin{document}

\begin{problem}\label{first}
Definition of Problem 1
\end{problem}

\begin{problem}\label{second}
Definition of Problem 2
\end{problem}

\begin{problem*}{first}
Definition of the first variation of Problem 1
\end{problem*}

\begin{problem*}{first}\label{secondoffirst}
Definition of the second variation of Problem 1
\end{problem*}

\begin{problem*}{second}
Definition of the first variation of Problem 2
\end{problem*}

As we saw in problem~\ref{secondoffirst}

\end{document}

Note that this requires two or three LaTeX runs when a problem is added, but you'll get the message

LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

in case a new run is needed.

enter image description here

  • Great! It works. Just what I was searching for. :) – CaG Nov 14 '19 at 13:32
1

The macro \newtheorem has an option with which you can refer to its parent. So you can use \newtheorem{subproblem}{Subproblem}[problem] to achieve what you want (at least, if I understand you correctly).

\documentclass{article}
\usepackage{amsthm}

\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem{subproblem}{Subproblem}[problem]

\begin{document}

\begin{problem}
    \label{prob1}
    Definition ...
\end{problem}

\begin{subproblem}
    \label{subprob1}
    Definition ...
\end{subproblem}

\end{document}

This results in the following output:

enter image description here


If you need the problems and subproblems in a random order, you can set the problem counter manually:

\documentclass{article}
\usepackage{amsthm}

\theoremstyle{definition}
\newtheorem{problem}{Problem}
\newtheorem{subproblem}{Subproblem}[problem]

\begin{document}

\begin{problem}
    \label{prob1}
    Definition ...
\end{problem}

\begin{problem}
    \label{prob2}
    Definition ...
\end{problem}

\setcounter{problem}{1}
\begin{subproblem}
    \label{subprob11}
    Definition ...
\end{subproblem}

\begin{subproblem}
    \label{subprob12}
    Definition ...
\end{subproblem}

\stepcounter{problem}
\begin{subproblem}
    \label{subprob21}
    Definition ...
\end{subproblem}

\end{document}

enter image description here

  • In this case, I should define the subproblems just after the associated main problem. If I have a situation like the example I provided above (where first the problems are defined and then the subproblems in a random order), this solution does not give what I want. – CaG Nov 14 '19 at 12:38
  • You can use \setcounter{problem}{1} to manually set the problem counter. See my edit. – Jasper Habicht Nov 14 '19 at 12:53

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.