Manipulate counters in tcolorbox environments

Based on the first answer of this question, Set number of tcbtheorem, I tried to deduce some other macros involving tcb counters adapted to my needs, and find this macro, for example:

\makeatletter
\@ifundefined{c@tcb@cnt@#1}{%
\GenericError{Error}{counter name #1 is no tcb counter }{}{}%
}{%
}
}
\makeatother


which gives what I want. I have tried this macro, similar to the command \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


But the command \valuetcbcounter{Theorem} gives an error. How can I find such a macro ?

I give the .tex file used:

\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
\@ifundefined{c@tcb@cnt@#1}{%
\GenericError{Error}{counter name #1 is no tcb counter }{}{}%
}{%
}
}
\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}

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

%\valuetcbcounter{Theorem}
\end{document}

• The definition of \valuetcbcounter is correct, but not its usage. You 'must' use \number\valuetcbcounter all the while or place the \number primitive in front of \value inside the macro – user31729 Apr 15 '18 at 6:25

Displaying the counter value without the usual \the... macro needs either \the\c@foo where foo is the counter name or \number\value{foo}, so just add \number before \value in the \valuetcbcounter macro.

This way \valuetcbcounter can still be used in a \numexpr or \ifnum way.

\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
\@ifundefined{c@tcb@cnt@#1}{%
\GenericError{Error}{counter name #1 is no tcb counter }{}{}%
}{%
}
}
\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}

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}