3

By now I've spend a lot of hours to find a way to customize the "Counter" inside tcolorbox package. I wanted to put a definition in a tcolorbox as below. enter image description here

In order to get this formation for definition, I used the following command (see \tcbset{...}):

\documentclass[12pt, openany]{book}
\usepackage[T1]{fontenc}
\usepackage{ae,aecompl}
\PassOptionsToPackage{svgnames}{xcolor}
\usepackage[round,sort,comma]{natbib}
\usepackage{amssymb,amsmath,amsfonts,amsthm,caption,subcaption}
\usepackage{mathtools}
\usepackage{enumerate}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{color}  
\usepackage{float,rotfloat} 
\usepackage{wrapfig}
\usepackage{multicol,multirow}
\usepackage{graphicx}   % need for figures
\usepackage{verbatim}   % useful for program listings
\usepackage{listings}
\usepackage{hyperref,url}
\usepackage[most]{tcolorbox}
\usepackage{cleveref}

\tcbset{
defnstyle/.style={fonttitle=\bfseries\upshape, fontupper=\slshape,
arc=0mm, colback=blue!5!white,colframe=blue!75!black},
}
\newtcbtheorem[number within=section,crefname={definition}{definitions}]%
{defn}{Definition}{defnstyle}{defn}

\begin{document}

\chapter{Limits and Derivatives}

\section{First Section}

\begin{defn}{One}{one}
Eine Funktion $f:~I\to\mathbb{R}$ auf einem Intervall $I$ hei\ss{}t in
$x_0\in I$ differenzierbar oder linear approximierbar,
wenn der Grenzwert
\begin{equation*}
\lim\limits_{x\to x_0}\frac{f(x)-f(x_0)}{x-x_0}=
\lim\limits_{h\to 0}\frac{f(x_0+h)-f(x_0)}{h}
\end{equation*}
existiert.
\end{defn}

\section{Second Section}

\begin{defn}{Two}{Two}
Eine Funktion $f:~I\to\mathbb{R}$ auf einem Intervall $I$ hei\ss{}t in
$x_0\in I$ differenzierbar oder linear approximierbar,
wenn der Grenzwert
\begin{equation*}
\lim\limits_{x\to x_0}\frac{f(x)-f(x_0)}{x-x_0}=
\lim\limits_{h\to 0}\frac{f(x_0+h)-f(x_0)}{h}
\end{equation*}

\end{defn}
\end{document}

But instead of counting definitions in the above pattern, I would like to count just 1, 2, 3 in each section (still I am interested to use tcolorbox). No chapter and section indication is necessary (see below). So, I tried to adjust the number within=section command in order to get my desire need but I am not able to get it at all.

enter image description here

It would be great if anyone could answer this question.

I greatly appreciate your time and effort.

  • Remove the option number within=section and add (as normal code later on, not as option) \makeatletter \@addtoreset{tcb@cnt@defn}{section} \makeatother or alternatively \counterwithin*{tcb@cnt@defn}{section} (if you are using a LaTeX version newer than 2018-04-01) instead. – moewe Dec 6 '18 at 21:02
  • You may want to consider writing $f \colon I\to\mathbb{R}$ instead of $f:~I\to\mathbb{R}$. See tex.stackexchange.com/q/37789/35864. Regardless of how you stand on the : vs \colon issue I feel that the space introduced by ~ is much too large in this case. – moewe Dec 6 '18 at 21:07
  • @moewe: Thank you very much! It worked like a miracle! Thanks a lot!! – CMP Dec 6 '18 at 21:15
2

With a new LaTeX kernel (2018-04-01 or newer) you can use \counterwithin*{tcb@cnt@defn}{section} and drop the option number within=section.

\documentclass[12pt, openany]{book}
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage[most]{tcolorbox}
\usepackage{cleveref}

\tcbset{
defnstyle/.style={fonttitle=\bfseries\upshape, fontupper=\slshape,
arc=0mm, colback=blue!5!white,colframe=blue!75!black},
}
\newtcbtheorem[crefname={definition}{definitions}]%
{defn}{Definition}{defnstyle}{defn}

\counterwithin*{tcb@cnt@defn}{section}

\begin{document}

\chapter{Limits and Derivatives}

\section{First Section}

\begin{defn}{One}{one}
Eine Funktion $f \colon I\to\mathbb{R}$ auf einem Intervall $I$ hei\ss{}t in
$x_0\in I$ differenzierbar oder linear approximierbar,
wenn der Grenzwert
\begin{equation*}
\lim\limits_{x\to x_0}\frac{f(x)-f(x_0)}{x-x_0}=
\lim\limits_{h\to 0}\frac{f(x_0+h)-f(x_0)}{h}
\end{equation*}
existiert.
\end{defn}

\section{Second Section}

\begin{defn}{Two}{Two}
Eine Funktion $f \colon I\to\mathbb{R}$ auf einem Intervall $I$ hei\ss{}t in
$x_0\in I$ differenzierbar oder linear approximierbar,
wenn der Grenzwert
\begin{equation*}
\lim\limits_{x\to x_0}\frac{f(x)-f(x_0)}{x-x_0}=
\lim\limits_{h\to 0}\frac{f(x_0+h)-f(x_0)}{h}
\end{equation*}

\end{defn}
\end{document}

Two colorboxes with a definition. Both are numbered only as "1" in their respective section.

If you are still using an older version of the kernel you'll want to try

\makeatletter
\@addtoreset{tcb@cnt@defn}{section}
\makeatother

instead of

\counterwithin*{tcb@cnt@defn}{section}

You can of course also go the opposite way

\newtcbtheorem[number within=section,crefname={definition}{definitions}]{defn}{Definition}{defnstyle}{defn}

\makeatletter
\renewcommand*{\thetcb@cnt@defn}{\arabic{\tcb@cnt@defn}}
\makeatother

Both of these methods have the disadvantage that they rely on the internal name of the tcolorbox counter name (tcb@cnt@<counter>). If you stick to tcolorbox-only commands, you can avoid this reliance on the internal name at the cost of a more complex definition with number freestyle

\newtcbtheorem[number within=section,number freestyle=\noexpand\arabic{\tcbcounter},
  crefname={definition}{definitions}]{defn}{Definition}{defnstyle}{defn}
  • I work on Overleaf but is seems \counterwithin*{tcb@cnt@defn}{section} does not work. But the other option perfectly works. Thank you! – CMP Dec 6 '18 at 22:08
  • @CMP Yes, as I said, \counterwithin* needs a new LaTeX kernel. Overleaf run an outdated version of LaTeX, where you can load the chngcntr package to make it work. – moewe Dec 7 '18 at 7:00
4

Use number freestyle to format the number. And don't use the ae package - this is obsolete since a long time.

\documentclass[12pt, openany]{book}
\usepackage[T1]{fontenc}
\PassOptionsToPackage{svgnames}{xcolor}
\usepackage[round,sort,comma]{natbib}
\usepackage{amssymb,amsmath,amsfonts,amsthm,caption,subcaption}
\usepackage{mathtools}
\usepackage{enumerate}
\usepackage{enumitem}
\usepackage{tikz}
\usepackage{color}
\usepackage{float,rotfloat}
\usepackage{wrapfig}
\usepackage{multicol,multirow}
\usepackage{graphicx}   % need for figures
\usepackage{verbatim}   % useful for program listings
\usepackage{listings}
\usepackage{hyperref,url}
\usepackage[most]{tcolorbox}
\usepackage{cleveref}

\tcbset{
defnstyle/.style={fonttitle=\bfseries\upshape, fontupper=\slshape,
arc=0mm, colback=blue!5!white,colframe=blue!75!black},
}
\newtcbtheorem[number within=section,crefname={definition}{definitions},
number freestyle=\noexpand\arabic{\tcbcounter}]%
{defn}{Definition}{defnstyle}{defn}

\begin{document}

\chapter{Limits and Derivatives}

\section{First Section}

\begin{defn}{One}{one}
Eine Funktion $f:~I\to\mathbb{R}$ auf einem Intervall $I$ hei\ss{}t in
$x_0\in I$ differenzierbar oder linear approximierbar,
wenn der Grenzwert
\begin{equation*}
\lim\limits_{x\to x_0}\frac{f(x)-f(x_0)}{x-x_0}=
\lim\limits_{h\to 0}\frac{f(x_0+h)-f(x_0)}{h}
\end{equation*}
existiert.
\end{defn}

\section{Second Section}

\begin{defn}{Two}{Two}
Eine Funktion $f:~I\to\mathbb{R}$ auf einem Intervall $I$ hei\ss{}t in
$x_0\in I$ differenzierbar oder linear approximierbar,
wenn der Grenzwert
\begin{equation*}
\lim\limits_{x\to x_0}\frac{f(x)-f(x_0)}{x-x_0}=
\lim\limits_{h\to 0}\frac{f(x_0+h)-f(x_0)}{h}
\end{equation*}

\end{defn}
\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.