TransWikia.com

How do I reset the equation counter in a tcolorbox align environment?

TeX - LaTeX Asked on November 22, 2020

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

One Answer

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=hnu
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

Correct answer by user227987 on November 22, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP