# tcolorbox calculation of the length of the title gets wrong for 10 and 100

I use tikz and tcolorbox to create boxes for theorems. I calculate the length of the title in order to cut the frame. Everything works until the counter gets to 10 or 100, the calculation of the length gets wrong or something else and here is the results :

At first we could think it has to appear when the counter gets to 10,20,30... etc. but it only does this for 10 and 100. It might also appear for 1000.

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
\usepackage{bbm}

\usetikzlibrary{calc}

\newlength{\longueur}

\definecolor{vert}{RGB}{51,92,41}
\definecolor{bleu}{RGB}{64,106,152}
\definecolor{rouge}{RGB}{178,27,15}

\newcommand{\definebox}[4]{
\ifstrequal{#4}{o}{
\newcounter{#1}
\newenvironment{#1}[1][]{
\settowidth{\longueur}{\textbf{\textsc{#3} \arabic{#1}##1}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\refstepcounter{#1}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1}##1}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1}##1}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
{\newenvironment{#1}[1][]{
\settowidth{\longueur}{\textbf{\textsc{#3}##1}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3}##1}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3}##1}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
}
\definebox{bv}{vert}{Définition}{o}
\definebox{bb}{bleu}{Proposition}{o}
\definebox{br}{rouge}{Théorème}{o}

\begin{document}
\begin{bb}[ -- Théorème d'inversion locale]
Si $f:U \to F$ est $C^{1}$ avec $a \in U$ tel que $df(a) \in \textrm{Isom}(E,F)$, alors il existe un voisinage ouvert $V \subset U$ de $a$ et un voisinage ouvert $W$ de $f(a)$ tel que $f_{|V}:V \to W$ est un $C^{1}$-difféomorphisme.
\end{bb}
\end{document}



new MWE for Jinwen :

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
\usepackage{tipa}
\usetikzlibrary{calc}

\newlength{\longueur}

\definecolor{vert}{RGB}{51,92,41}
\definecolor{bleu}{RGB}{64,106,152}
\definecolor{rouge}{RGB}{178,27,15}

\newcommand{\definebox}[4]{
\ifstrequal{#4}{o}{
\newcounter{#1}
\newenvironment{#1}[1][]{
\refstepcounter{#1}
\def\temp{##1}
\ifx\temp\empty
\def\tempdescription{\!\!}
\else
\def\tempdescription{-- ##1}
\fi
\settowidth{\longueur}{\textbf{\textsc{#3} \arabic{#1} \tempdescription}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
{\newenvironment{#1}[1][]{
\settowidth{\longueur}{\textbf{\textsc{#3} \tempdescription}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
}
\definebox{bv}{vert}{Définition}{o}
\definebox{bb}{bleu}{Proposition}{o}
\definebox{br}{rouge}{Théorème}{o}

\begin{document}
\begin{bb}
test
\end{bb}
\end{document}



Since you've put the \refstepcounter after \settowidth, when the counter value is 10, 100, 1000, etc., the width being measured is 9, 99, 999, etc. Thus, to correct this behavior, you need to put \refstepcounter at the beginning.

Also, I've moved your -- part in the theorem description to the tcolorbox setting, so you won't need to write them explicitly yourself. (The code will test if the description is given, and when the description is not empty -- shall be added -- the test uses the traditional \ifx method so it is not always reliable, e.g. when your description contains a conditional \if..., however it should be suffice for your purpose.)

\documentclass{article}

\usepackage[most]{tcolorbox}

\usetikzlibrary{calc}

\newlength{\longueur}

\definecolor{vert}{RGB}{51,92,41}
\definecolor{bleu}{RGB}{64,106,152}
\definecolor{rouge}{RGB}{178,27,15}

\newcommand{\definebox}[4]{
\ifstrequal{#4}{o}{
\newcounter{#1}
\newenvironment{#1}[1][]{
\refstepcounter{#1}
\def\temp{##1}
\ifx\temp\empty
\def\tempdescription{\hspace{-1ex}}
\else
\def\tempdescription{-- ##1}
\fi
\settowidth{\longueur}{\textbf{\textsc{#3} \arabic{#1} \tempdescription}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
{\newenvironment{#1}[1][]{
\settowidth{\longueur}{\textbf{\textsc{#3} \tempdescription}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
}
\definebox{bv}{vert}{Définition}{o}
\definebox{bb}{bleu}{Proposition}{o}
\definebox{br}{rouge}{Théorème}{o}

\begin{document}
\setcounter{bb}{99}
\begin{bb}[Théorème d'inversion locale]
Si $f:U \to F$ est $C^{1}$ avec $a \in U$ tel que $df(a) \in \textrm{Isom}(E,F)$, alors il existe un voisinage ouvert $V \subset U$ de $a$ et un voisinage ouvert $W$ de $f(a)$ tel que $f_{|V}:V \to W$ est un $C^{1}$-difféomorphisme.
\end{bb}

\begin{bb}
Si $f:U \to F$ est $C^{1}$ avec $a \in U$ tel que $df(a) \in \textrm{Isom}(E,F)$, alors il existe un voisinage ouvert $V \subset U$ de $a$ et un voisinage ouvert $W$ de $f(a)$ tel que $f_{|V}:V \to W$ est un $C^{1}$-difféomorphisme.
\end{bb}

\end{document}


• Your solution works but it has a conflict with the package tipa, it says "Argument of \UseTextAccent has an extra }" and "Runaway argument ?" May 26 at 12:40
• @Flowt I cannot reproduce the conflict, directly adding \usepackage{tipa} seems fine. Perhaps you missed a curly bracket in your document. If you cannot find what's wrong, then adding a new MWE to you question or open a new one, I shall take a look at it. May 26 at 12:44
• I edited the comment, see above. May 26 at 12:59
• @Flowt It is the \! that conflicts with package tipa, you can replace it with \hspace{-1ex}. May 26 at 13:22
• It was indeed \! thanks for the additional solution. May 26 at 13:39

Although @Jinwen is the correct answer to the question, one suggestion to simplify the thing: why do not just use a white-filled detached title for the job, so you do not need to calculate anything?

Proof-of-concept example (mostly from the manual, hint from here):

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{many}
\newtcolorbox{mybox}[2][]{colback=white,
colframe=cyan!50!black,fonttitle=\bfseries,
colbacktitle=white,enhanced, coltitle=blue,
attach boxed title to top left={xshift=0.5cm,
yshift=-\tcboxedtitleheight/2},
boxed title style={colframe=white},
title={#2},#1}
\begin{document}
\begin{mybox}[]{Hello there}
This is my own box with a mandatory title
and options.
\end{mybox}
\end{document}


• It was the first style I used, it worked fine until I had to use my boxes on a document where the background was not white. May 26 at 12:56
• @Flowt yes, I see --- correct. May 26 at 14:27