3

Let's say I have a tcolorbox environment called note. Is there a way for equations that appear inside this environment to automatically assume a different counter from those outside? With automatically I mean without defining a new equation environment?

Here's an example (no second counter implemented):

\documentclass{article}

\usepackage[many]{tcolorbox}
\newtcolorbox{note}[1][]{%
    breakable,
    enhanced jigsaw,
    borderline west={1pt}{0pt}{black},
    sharp corners,
    boxrule=0pt,
    frame hidden,
    left=1ex,right=1ex,
    fonttitle={\bfseries},
    coltitle={black},
    title={Note:\ },
    attach title to upper,
    #1}

\begin{document}

Outside text followed by equation.
\begin{equation}
    a = b.
\end{equation}

\begin{note}
    Inside text followed by equation.
    \begin{equation}
        b = c.
    \end{equation}
\end{note}

More outside text followed by conclusion.
\begin{equation}
    a = c.
\end{equation}

\end{document}
  • Would you be interested in using the same counter, but prefixed, e.g;, after equation (1) in the main body, an equation inside a note would be numbered (N2)? – Bernard Jul 12 '16 at 23:04
  • @Bernard Not really. I need the counters outside the environment to run irrespective of any equations inside environments that might be nestled in between. – Casimir Jul 12 '16 at 23:08
  • 1
    I've posted my code (which rests on Christian Hupfer's). AS you can see, it's enough to define a counter associated to equations in this environment. – Bernard Jul 13 '16 at 0:58
5

Here's a variant based on the preceding code, which should work in any environment and is compatible with cleveref:

\documentclass{article}
\usepackage{ebgaramond, ebgaramond-maths}
\usepackage{chngcntr, mathtools,}
\usepackage{framed,  etoolbox, cleveref}

\makeatletter
\newcounter{framedeqn}
\AtBeginEnvironment{framed}{\let\c@equation\c@framedeqn\def\theequation{N\theframedeqn}}
\makeatother

\begin{document}

Some outside text followed by conclusion in \cref{noteeq:a}:  %
\begin{equation}
    a = c.
\end{equation}

Outside text followed by equation:
\begin{equation}
    a = b. \label{eq-out}
\end{equation}
%
\begin{framed}
\noindent\textsc{Note 1: }\\
 An equation inside some environment,  different from \cref{eq-out,noteeq-b}:
 \belowdisplayskip = 0pt
\begin{align}
  x = y \label{noteeq:a}
\end{align}
\end{framed}
Outside text followed by equation:
\begin{equation}
    c= d.
\end{equation}

\begin{framed}
\noindent\textsc{Note 2: }\\
 Another equation inside the same environment:
 \belowdisplayskip = 0pt
\begin{align}
  z = t \label{noteeq-b}
\end{align}
\end{framed}

\end{document}

enter image description here

  • Also very nice! cleveref compatibility is certainly a bonus as I use it a lot. In that regard, you and Christian both over-delivered. – Casimir Jul 13 '16 at 1:35
4

There's no need for a whole new equation environment, only a fake counter has to be established which is used inside by saying

\let\c@equation\c@fakeequation

Remember, that \c@foo is the internal representation macro of the counter foo, so this means that unless specified otherwise all operations to the equation counter are actually done with fakeequation, so this trick should be used in a group rather, which is done by the code=.... option in the definition of \newtcolorbox.

cleveref works out of the box too:

\documentclass{article}

\usepackage[many]{tcolorbox}
\newcounter{fakeequation}

\usepackage{cleveref}
\makeatletter

\let\latex@theequation\theequation
\newtcolorbox{note}[1][]{%
  code={\let\c@equation\c@fakeequation\renewcommand{\theequation}{N\latex@theequation}},
  breakable,
  enhanced jigsaw,
  borderline west={1pt}{0pt}{black},
  sharp corners,
  boxrule=0pt,
  frame hidden,
  left=1ex,right=1ex,
  fonttitle={\bfseries},
  coltitle={black},
  title={Note:\ },
  attach title to upper,
  #1}
\makeatother

\begin{document}

\setcounter{fakeequation}{100}

Outside text followed by equation.
\begin{equation}
    a = b.
\end{equation}

\begin{note}
    Inside text followed by equation.
    \begin{equation}
        b = c. \label{innerref}
    \end{equation}
\end{note}

More outside text followed by conclusion.
\begin{equation}
    a = c.
\end{equation}

\begin{note}
    Inside text followed by equation.
    \begin{equation}
        b = c. \label{secondref}
    \end{equation}
\end{note}


The inner equation is \eqref{innerref} and \cref{secondref}

\end{document}

enter image description here

  • 2
    Splendid! Don't you think the appearance of the counter should be modified, say prefixed like this (N1), to avoid ambiguities in cross references? – Bernard Jul 12 '16 at 23:38
  • @Bernard: There might be confusion with this scheme of course. I'll change – user31729 Jul 12 '16 at 23:40
  • Meanwhile, I thought it would be more general to patch the counter with \AtBeginEnvironment. I've just tested: it works nicely with an environment like framed, which doesn't have a powerful keys system like tcolorbox. – Bernard Jul 12 '16 at 23:48
  • @Bernard: I've used this trick on some occasions already. It's quite basic and should work for most occurences, also in a patch – user31729 Jul 12 '16 at 23:50
  • Thanks Christian, this works great! @Bernard I'd be interested to see such a solution as well in case you have one ready. – Casimir Jul 12 '16 at 23:57
3

I prefer to avoid monkeying with counter names.

\documentclass{article}

\usepackage[many]{tcolorbox}
\usepackage{cleveref}

\newcounter{savedequation}
\newcounter{tcbequation}
\newtcolorbox{note}[1][]{%
  code={%
    \setcounter{savedequation}{\value{equation}}%
    \setcounter{equation}{\value{tcbequation}}%
    \renewcommand{\theequation}{N\arabic{equation}}%
  },
  after={%
    \setcounter{tcbequation}{\value{equation}}%
    \setcounter{equation}{\value{savedequation}}%
  },
  breakable,
  enhanced jigsaw,
  borderline west={1pt}{0pt}{black},
  sharp corners,
  boxrule=0pt,
  frame hidden,
  left=1ex,right=1ex,
  fonttitle={\bfseries},
  coltitle={black},
  title={Note:\ },
  attach title to upper,
  #1,
}

\begin{document}

Outside text followed by equation.
\begin{equation}\label{A}
    a = b.
\end{equation}

\begin{note}
    Inside text followed by equation.
    \begin{equation}\label{B}
        b = c.
    \end{equation}
\end{note}

More outside text followed by conclusion.
\begin{equation}\label{C}
    a = c.
\end{equation}

\cref{A}, \cref{B}

\end{document}

enter image description here

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.