2

I have defined the solution environment as below:

\newtheorem{solution}{Solution}[subsection]

I have two solutions to problem 1.3.3 as shown in the attached screenshot. However, by using \begin{solution} \end{solution} twice, the second solution would be named as 1.3.4. Is there any way to change the counter so that these two solutions both correspond to the same question? Something like 1.3.3-1 & 1.3.3-2 would be great, but if there's a more elegant way, I'd also love to hear it. Thanks!

enter image description here

Vincent
  • 20,157
Skorpion
  • 237
  • There are several packages for formatting problems and their solutions, perhaps one such is a better match than (ab)using theorems for this task... – vonbrand Feb 17 '20 at 03:39

2 Answers2

2

I am assuming you are using amsthm to manage your theorems. In that case you can set the parent counter of your solution environment to be the counter of your theorem, like so

\documentclass{article}

\usepackage{amsthm}

\newtheorem{thm}{Theorem}[subsection]
\newtheorem{solution}{Solution}[thm]
% ^^^ Note that as <parent counter> we are using
%     the thm counter 

\begin{document}
  \section{Sec 1}
  \subsection{Subsec 1}
  \begin{thm}
    Test
  \end{thm}
  \begin{solution}
    Sol 1
  \end{solution}
  \begin{solution}
    Sol 2
  \end{solution}

  \section{Sec 2}
  \subsection{Subsec 2}
  \begin{thm}
    Test
  \end{thm}
  \begin{solution}
    Sol 1
  \end{solution}
  \begin{solution}
    Sol 2
  \end{solution}
\end{document}

yielding

solution

gjkf
  • 585
  • This works, thanks! One question remains — would it be possible to set the counter like this only when I need it for a problem with more than one solution? Because now if I were to do this, every solution has the format of "1.1.1.1", but I'd love to make them "1.1.1.1" only when there's a need for "1.1.1.2" and leave them as "1.1.1" when there's no such need. – Skorpion Feb 16 '20 at 00:19
  • 1
    One immediate solution would be to just simply create a different environment, like singleSolution that has the counter set to subsection. That would definitely do it for you. If you want only one... I'm not really sure to be quite honest. I will try and find a solution tomorrow, until then let's hope more experienced TeXers come and help us :) – gjkf Feb 16 '20 at 00:26
  • https://tex.stackexchange.com/questions/53978/custom-theorem-numbering Quite interesting way, but I believe there gotta be a more elegant way :D – Skorpion Feb 16 '20 at 00:32
  • I feel like the other answer does what you want in a neater way. Too bad for the referencing issue. – gjkf Feb 16 '20 at 09:43
1

The following example should work to number solutions within problems only when there are multiple solutions to the same problem. It basically works by defining a command which stores the number of solutions to a given problem in the .aux file, and by redefining the \thesolution command to display the number of the solution according to the number of solutions to the problem. It's a little complicated, and maybe there's a simpler way to do that, but I couldn't think of any.

Here's the example:

\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{etoolbox}
\newtheorem{problem}{Problem}[subsection]
\theoremstyle{remark}
\newtheorem{solution}{Solution}
\newcounter{numsolution}
\numberwithin{numsolution}{problem}

\makeatletter
\AtBeginEnvironment{problem}{%
    \addtocounter{problem}{1}
    \newcounter{solsofprob\the\value{section}.\the\value{subsection}.\the\value{problem}}
    \addtocounter{problem}{-1}
}
\AtBeginEnvironment{solution}{%
    \addtocounter{solsofprob\the\value{section}.\the\value{subsection}.\the\value{problem}}{1}
    \immediate\write\@mainaux{\string\gdef\string\numsolsofprob\romannumeral\the\value{section}@\romannumeral\the\value{subsection}@\romannumeral\the\value{problem}{\the\value{solsofprob\the\value{section}.\the\value{subsection}.\the\value{problem}}}}%
}
\newcommand{\tempnum}{0}
\renewcommand{\thesolution}{%
    \@ifundefined{numsolsofprob\romannumeral\the\value{section}@\romannumeral\the\value{subsection}@\romannumeral\the\value{problem}}%
        {\def\tempnum{0}}%
        {\edef\tempnum{\csname numsolsofprob\romannumeral\the\value{section}@\romannumeral\the\value{subsection}@\romannumeral\the\value{problem}\endcsname}}%
    \ifnum \tempnum > 1
        \stepcounter{numsolution}%
        \arabic{section}.\arabic{subsection}.\arabic{problem}--\arabic{numsolution}%
    \else
        \arabic{section}.\arabic{subsection}.\arabic{problem}%
    \fi%
}
\makeatother

\begin{document}
\section{First section}
\subsection{First subsection}
\begin{problem}
    A problem.
\end{problem}
\begin{solution}
    The solution.
\end{solution}
\begin{problem}
    Another problem. 
\end{problem}
\begin{solution}
    A first solution.
\end{solution}
\begin{solution}
    A second solution.
\end{solution}
\begin{problem}
    Another problem.
\end{problem}
\begin{solution}
    A solution.
\end{solution}
\subsection{Second subsection}
\begin{problem}
    A problem.
\end{problem}
\begin{solution}
    A solution.
\end{solution}
\section{Second section}
\subsection{A subsection}
\begin{problem}
    A problem.
\end{problem}
\begin{solution}
    A solution.
\end{solution}
\begin{solution}
    Another solution.
\end{solution}
\end{document}

The above codes outputs:

I should also mention that, since this solution works by redefining the \thesolution command in a complicated way, it will not be possible to add a label to a solution to reference it later. I will update the code if I think of something to make to labeling work.

Vincent
  • 20,157
  • Well, we can always refer to the problem instead, and that won't give us too much extra work to scroll down to the solution :) Inspired by @gjkf , I tried to do two separate commands \newtheorem{solution}{Solution}[subsection] and \newtheorem{subsolution}{Solution}[problem] which also works. But I love this little dash more! Thanks. – Skorpion Feb 16 '20 at 15:05