环境内方程的不同计数器

环境内方程的不同计数器

假设我有一个tcolorbox名为 的环境note。有没有办法让出现在这个环境内的方程式自动假设与外部方程式不同的计数器?自动是指无需定义新的方程式环境?

这是一个例子(没有实现第二个计数器):

\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}

答案1

这是基于上述代码的变体,它可以在任何环境中工作并且兼容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}

在此处输入图片描述

答案2

不需要一个全新的equation环境,只fake需要建立一个计数器,在里面使用即可

\let\c@equation\c@fakeequation

请记住,这\c@foo是计数器的内部表示宏foo,因此这意味着除非另有说明,否则对计数器的所有操作equation实际上都是用完成的fakeequation,因此这个技巧应该成组使用,这是通过code=....定义中的选项完成的\newtcolorbox

cleveref开箱即可使用:

\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}

在此处输入图片描述

答案3

我倾向于避免对计数器名称进行修改。

\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}

在此处输入图片描述

相关内容