在 tcolorbox 环境中操作计数器

在 tcolorbox 环境中操作计数器

根据这个问题的第一个答案,tcbtheorem 的集合数,我尝试推导一些其他适合我需要的涉及 tcb 计数器的宏,并找到这个宏,例如:

\makeatletter
\newcommand{\addtotcbcounter}[2]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \addtocounter{tcb@cnt@#1}{#2}
  }
}
\makeatother

这给出了我想要的。我试过这个宏,类似于命令\value{counter}

\makeatletter
\newcommand{\valuetcbcounter}[1]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \value{tcb@cnt@#1}
  }
}
\makeatother

但是命令\valuetcbcounter{Theorem}报错。我怎样才能找到这样的宏?

我给出使用的.tex 文件:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcbtheorem[number within=section]{Theorem}{Theorem}{}{Th}

\makeatletter
\newcommand{\providetcbcountername}[1]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    --undefined--%
  }{%
    tcb@cnt@#1%
  }
}

\newcommand{\settcbcounter}[2]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \setcounter{tcb@cnt@#1}{#2}
  }
}

\newcommand{\displaytcbcounter}[1]{% Wrapper for \the...
  \@ifundefined{thetcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \csname thetcb@cnt@#1\endcsname% 
  }%
}
\makeatother
%%%                                 END MANIPULATING COUNTER OF TCOLORBOX   

\makeatletter
\newcommand{\addtotcbcounter}[2]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \addtocounter{tcb@cnt@#1}{#2}
  }
}
\makeatother

\makeatletter
\newcommand{\valuetcbcounter}[1]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \value{tcb@cnt@#1}
  }
}
\makeatother

\begin{document}

\setcounter{section}{6}
\section{My nice section}

\settcbcounter{Theorem}{8}

The counter for theorem is \providetcbcountername{Theorem} and it has the value of \displaytcbcounter{Theorem}

\begin{Theorem}{}{}
  We have $2 + 2 = 4$.
\end{Theorem}

\addtotcbcounter{Theorem}{2}

After adding 2 to tcbcounter, the macro for displaying the value of \providetcbcountername{Theorem} gives \displaytcbcounter{Theorem}.

%\valuetcbcounter{Theorem} 
\end{document}

答案1

在没有通常的宏的情况下显示计数器值\the...需要计数器名称在\the\c@foo哪里或,因此只需在宏之前添加即可。foo\number\value{foo}\number\value\valuetcbcounter

这种方式仍然可以以或的方式\valuetcbcounter使用。\numexpr\ifnum

在此处输入图片描述

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcbtheorem[number within=section]{Theorem}{Theorem}{}{Th}

\makeatletter
\newcommand{\providetcbcountername}[1]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    --undefined--%
  }{%
    tcb@cnt@#1%
  }
}

\newcommand{\settcbcounter}[2]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \setcounter{tcb@cnt@#1}{#2}%
  }
}

\newcommand{\displaytcbcounter}[1]{% Wrapper for \the...
  \@ifundefined{thetcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \csname thetcb@cnt@#1\endcsname% 
  }%
}
\makeatother
%%%                                 END MANIPULATING COUNTER OF TCOLORBOX   

\makeatletter
\newcommand{\addtotcbcounter}[2]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \addtocounter{tcb@cnt@#1}{#2}%
  }
}
\makeatother

\makeatletter
\newcommand{\valuetcbcounter}[1]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \number\value{tcb@cnt@#1}%
  }
}
\makeatother

\begin{document}

\setcounter{section}{6}
\section{My nice section}

\settcbcounter{Theorem}{8}

The counter for theorem is \providetcbcountername{Theorem} and it has the value of \displaytcbcounter{Theorem}

\begin{Theorem}{}{}
  We have $2 + 2 = 4$.
\end{Theorem}

\addtotcbcounter{Theorem}{2}

After adding 2 to tcbcounter, the macro for displaying the value of \providetcbcountername{Theorem} gives \displaytcbcounter{Theorem}.

\valuetcbcounter{Theorem}


Adding a value only for printing, not the value itself
\the\numexpr\valuetcbcounter{Theorem}+2

\ifnum11=\valuetcbcounter{Theorem}%
Hooray!
\else
Nope!
\fi
\end{document}

相关内容