2

I'm texing my university lecture notes and want to include some problems with solutions in the document.

What I want:

  • While all other amsthm environments are consecutively numbered Definition 1.1, Theorem 1.2, etc., I want to have problem numbers Problem 1.A, Problem 1.B, Problem 2.A, etc.
  • For each problem I want to have a solution with exactly the same printed counter 1.A, 1.B, 2.A, etc. Naturally, one could just use a separate solution counter and call problem and solution the same number of times.
  • The solutions should appear at the end of the document in their own section, so some kind of deferred printing. Thus the above approach doesn't work, since the first number, the section number, doesn't correspond anymore.

Do you have any packages or ideas on implementing this? Should I use ntheorem for that? Any help is welcomed.

Edit (@Werner): My question is actually fairly general, and therefore I think a description wouldn't help as much. But here is my setup anyway: I have my custom package and use amsthm. I use the article documentclass. At the moment, the theorem-like environments look as follows:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}

\RequirePackage[T1]{fontenc}
\RequirePackage[ngerman=ngerman-x-latest]{hyphsubst}
\RequirePackage[ngerman]{babel}

\RequirePackage{amsmath}
\RequirePackage{amsthm}
    \newtheorem{theorem}{Satz}[section]
    \newtheorem{lemma}{Lemma}[section]
    \newtheorem{corollary}{Korollar}[section]
    \newtheorem{axiom}{Axiom}[section]
\theoremstyle{definition}
    \newtheorem{definition}{Definition}[section]
    \newtheorem*{notation}{Bezeichnung}
    \newtheorem{problem}{Aufgabe}[section]  %These two environments
    \newtheorem{solution}{Lösung}[section]  %are of interest
\theoremstyle{remark}
    \newtheorem{example}{Beispiel}[section]
    \newtheorem*{remark}{Bemerkung}
    \newtheorem{case}{Fall}
%Replace all theorem environment counters except `problem` and
%`solution` by `equation` counter. Therfore, they share the counter
%with equation, but still have the preceeding section number.
%Maybe you have a better way, let me know.
\renewcommand*{\c@theorem}{\c@equation}
\renewcommand*{\c@lemma}{\c@equation}
\renewcommand*{\c@corollary}{\c@equation}
\renewcommand*{\c@axiom}{\c@equation}
\renewcommand*{\c@definition}{\c@equation}
\renewcommand*{\c@example}{\c@equation}
%Reset `case` counter
\@addtoreset{case}{theorem}
\@addtoreset{case}{lemma}
\@addtoreset{case}{corollary}
4
  • Can you provide at least a shell of a document structure and notation you're currently using? Even if it doesn't work...
    – Werner
    Nov 20, 2021 at 2:37
  • I've added a code example
    – Gargantuar
    Nov 20, 2021 at 8:34
  • I guess you're in an article type class. Is the 1 in problem 1.A referring to the section? But then you're talking about “the end of each chapter”, so my initial guess seems wrong. Can you please clarify? How should equation numbers be typeset? With the section number or not?
    – egreg
    Nov 20, 2021 at 10:41
  • I've corrected it, you're right. In German, I don't differentiate that much between chapter and section ;) (we even use the word "subchapter" as opposed to section).
    – Gargantuar
    Nov 20, 2021 at 10:56

1 Answer 1

1

Here's my take. Since you essentially want a single counter for equations and statements (theorems, definitions and so on), it's simpler to define the theorem-like environments to use equation.

Also equation numbers must show the section number or the numbering would appear very mysterious. And the number should be at the left.

How to define problems and solutions? Just type in the solution next to the corresponding problem, save the body and print all the stored solutions where you like. The \printsolutions command will also clear the list of stored solutions.

\documentclass[a4paper]{article}

\makeatletter % from here to \makeatother non inclusive is for your package
\RequirePackage[T1]{fontenc}
\RequirePackage[ngerman=ngerman-x-latest]{hyphsubst}
\RequirePackage[ngerman]{babel}

\RequirePackage[leqno]{amsmath}
\RequirePackage{amsthm}

\counterwithin{equation}{section}

\newtheorem{theorem}[equation]{Satz}
\newtheorem{lemma}[equation]{Lemma}
\newtheorem{corollary}[equation]{Korollar}
\newtheorem{axiom}[equation]{Axiom}

\theoremstyle{definition}
\newtheorem{definition}[equation]{Definition}
\newtheorem*{notation}{Bezeichnung}
\newtheorem{problem}{Aufgabe}[section]
\renewcommand{\theproblem}{\thesection.\Alph{problem}}

\newtheorem*{solutionx}{Lösung \thesolutionnumber}

\ExplSyntaxOn
\tl_new:N \g_gargantuar_solution_tl

\NewDocumentEnvironment{solution}{+b}
 {
  \tl_gput_right:Nx \g_gargantuar_solution_tl
   {
    \printsolution{\theproblem}{ \exp_not:n { #1 } }
   }
 }
 {}
\NewDocumentCommand{\printsolutions}{}
 {
  \subsection*{Solutions~to~the~problems}
  \tl_use:N \g_gargantuar_solution_tl
  \tl_gclear:N \g_gargantuar_solution_tl
 }
\NewDocumentCommand{\printsolution}{m +m}
 {
  \cs_set:Npn \thesolutionnumber { #1 }
  \begin{solutionx} #2 \end{solutionx}
 }
\ExplSyntaxOff

\theoremstyle{remark}
\newtheorem{example}{Beispiel}[section]
\newtheorem*{remark}{Bemerkung}
\newtheorem{case}{Fall}
\counterwithin*{case}{equation}
\makeatother

\begin{document}

\section{Title}

\begin{equation}
A=B
\end{equation}

\begin{theorem}
Text
\end{theorem}

\begin{case}
Text
\end{case}

\begin{case}
Text
\end{case}

\begin{lemma}
Text
\end{lemma}

\begin{case}
Text
\end{case}

\begin{equation}
A=B
\end{equation}

\begin{problem}
Text
\end{problem}
\begin{solution}
Solution
\end{solution}
\begin{problem} % no solution for this one
Text
\end{problem}
\begin{problem}
Text
\end{problem}
\begin{solution}
Solution
\end{solution}

\printsolutions

\end{document}

enter image description here

What's the sorcery?

  1. The +b argument to \NewDocumentEnvironment specifies that the body is collected to be used in some way.
  2. The usage, in this case, is to store it in a container (the \g_gargantuar_solutions_tl token list variable) along with the expanded number of the last appearing problem.
  3. The storage is in the form \printsolution{<number>}{<text>}
  4. The command \printsolutions delivers the contents of the variable.
  5. \printsolution changes the meaning of \thesolutionnumber so the auxiliary environment solutionx does the right thing.
5
  • You're a god! What is this sorcery ExplSyntax? Do you have a link or resource to read more about it?
    – Gargantuar
    Nov 20, 2021 at 11:33
  • @Gargantuar ctan.org/pkg/l3kernel
    – user202729
    Nov 20, 2021 at 11:41
  • 1
    @Gargantuar I added some explanations
    – egreg
    Nov 20, 2021 at 11:54
  • @egreg There seems to be a problem if I want to have multiple paragraphs in a solution environment by leaving blank lines. Is there any fix for that?
    – Gargantuar
    Nov 27, 2021 at 9:06
  • 1
    @Gargantuar Sorry, I forgot to take care of it. A small modification is needed: \NewDocumentCommand{\printsolution}{mm} should become \NewDocumentCommand{\printsolution}{m +m}. I'll fix the code.
    – egreg
    Nov 27, 2021 at 9:09

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.