3

After poring over this site and the tcolorbox documentation for over an hour, I can't figure out how to reset the equation counter inside my derivation environment. I wrote the old version with mdframed and there it was as simple as \setcounter{equation}{0} at the beginning of the environment, but that obviously doesn't work here. I need the numbering to start over with each instance of derivation. I have a feeling it's trivial and I'm just not seeing it.

My MWE:

% !TEX TS-program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass{article}
\usepackage{amsmath}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcolorbox[auto counter]{derivation}{%
  ams align,%
  title={DERIVATION \thetcbcounter},%
  fonttitle=\bfseries,%
  label={derivation@\thetcbcounter},
  coltitle=black,%
  colback=orange!25,%
  colframe=orange!60%
  %colback=orange!5!white,%
  %colframe=orange!75!black%
}%
\begin{document}
\begin{derivation}
  x + y &= z     && \text{given} \\
      y &= z - x && \text{solve for \(y\)}
\end{derivation}
\begin{derivation}
  \gamma &= \frac{1}{\sqrt{1-v^2}}      && \text{definition} \\
       v &= \sqrt{1-\frac{1}{\gamma^2}} && \text{solve for \(v\)}
\end{derivation}
\end{document}

MWE output: MWE Output

4
  • 1
    You can use the mdframed method here, just add code=\setcounter{equation}{0} to the options of the derivation box. The question is whether this makes sense because then different equations carry the same number.
    – user227987
    Nov 2, 2020 at 1:25
  • That works! Thank you. Can't find it in the tcolorbox docs. I can't imagine seeing derivations in a math or physics textbook with consecutively numbered steps through the book. The individual steps won't be referenced, only the boxes themselves. Am I missing something? Nov 2, 2020 at 1:41
  • 1
    I personally would prefer something of the sort (<number of derivation>-<number of equation in>) derivation, which can be achieved with code=\setcounter{equation}{0}\renewcommand{\theequation}{\thetcbcounter-\arabic{equation}}. This is still not too good if you have ordinary equations in the document, but you could memorize the orginal equation number and restore it after the box in that case. BTW, the code key can be found in section 4.24 Miscellaneous of the tcolorbox manual v4.42.
    – user227987
    Nov 2, 2020 at 1:45
  • Hey I like that! Make that an answer and I'll accept it. Yep I finally found code in the docs. Well hidden! Many thanks! Nov 2, 2020 at 1:58

1 Answer 1

3

There are various hooks that allow one to execute some code at various stages of the construction of the box. The following uses code and after. It also redefines and restores equation numbering, similar to the answers to this question. Note, however, that I do not want to make the impression that this is a super stable solution. I did not check whether this works with hyperref, nor did I do other cross-checks. Yet a conceivably more stable solution, which may or may not make use of future versions of the LaTeX kernel, may possibly use the same keys as in this code.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newcounter{savedeq}
\newtcolorbox[auto counter]{derivation}{%
  code=\setcounter{savedeq}{\value{equation}}%
    \setcounter{equation}{0}\renewcommand{\theequation}{\thetcbcounter-\arabic{equation}},%
  after=\setcounter{equation}{\value{savedeq}},%
  ams align,%
  title={DERIVATION \thetcbcounter},%
  fonttitle=\bfseries,%
  label={derivation@\thetcbcounter},
  coltitle=black,%
  colback=orange!25,%
  colframe=orange!60,%
  %colback=orange!5!white,%
  %colframe=orange!75!black%
}%
%\numberwithin{equation}{derivation}
\begin{document}
\begin{equation}
E=mc^2
\end{equation}
\begin{derivation}
  x + y &= z     && \text{given} \\
      y &= z - x && \text{solve for \(y\)}
\end{derivation}
\begin{equation}
E=h\nu
\end{equation}
\begin{derivation}
  \gamma &= \frac{1}{\sqrt{1-v^2}}      && \text{definition} \\
       v &= \sqrt{1-\frac{1}{\gamma^2}} && \text{solve for \(v\)}
\end{derivation}
\begin{equation}
a^2+b^2=c^2
\end{equation}
\end{document}

enter image description here

4
  • Seems to work fine. If I add list inside=deriv to the init for derivation and put \tcblistof{deriv}{List of Derivations} just after \begin{document} everything looks fine. Then if I \usepackage{hyperref} I get warnings about ignoring duplicate destination. Aside from that, it works. Nov 2, 2020 at 19:54
  • Setting the naturalnames=true option when loading hyperref seems to correct the issue. Nov 2, 2020 at 20:15
  • @LaTeXereXeTaL Thanks for letting me know! This seems to be indeed a bit more "natural than" the posts I have linked to.
    – user227987
    Nov 2, 2020 at 20:16
  • I didn't initially plan to implement links to derivation steps, but I think students will find it useful. Plus, I'm trying to combine this solution with the one found here tex.stackexchange.com/a/547806/218142. Let's see just how much I can break things! Nov 2, 2020 at 20:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .