1

I'm creating a box for theorems, I need to calculate the length of the title to cut the top of the frame in order to not cover my title. The code works but if I replace #3 \arabic{#1}##1 by \textbf{\textsc{#3} \arabic{#1}##1} the width is not correct and my title is covered. How can I measure the correct width of the text when it's in textbf or textsc ?

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage{afterpage}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\definecolor{rednew}{RGB}{178,27,15}
\begin{document}

\newcommand{\definebox}[4]{ \ifstrequal{#4}{o}{ \newcounter{#1} \newenvironment{#1}[1][]{ \refstepcounter{#1} \begin{tcolorbox}[enhanced,opacityback=0,attach boxed title to top left={yshift=-\tcboxedtitleheight/2,xshift=4mm},center title,opacityframe=0,before skip=0.5cm,left skip=0.16cm,left=0.2cm,right=0cm,top=0.2cm,bottom=0cm,breakable,boxed title style={opacityback=0,opacityframe=0,colframe=white,size=fbox,arc=0mm},overlay unbroken and first={\pgfmathsetmacro{\textPosX}{width("#3 \arabic{#1}##1")1pt/1cm} \node[anchor=west] at ($(interior.north west)+(0.345,0.05)$){#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)+(\textPosX,0)+(0.215,0)$);},overlay middle and last={\draw[very thick](frame.north west)--(frame.south west);},after={\vspace{1ex} \noindent}]} {\end{tcolorbox}}} {\newenvironment{#1}[1][]{ \begin{tcolorbox}[enhanced,opacityback=0,attach boxed title to top left={yshift=-\tcboxedtitleheight/2,xshift=4mm},center title,opacityframe=0,before skip=0.5cm,left skip=0.16cm,left=0.2cm,right=0cm,top=0.2cm,bottom=0cm,breakable,boxed title style={opacityback=0,opacityframe=0,colframe=white,size=fbox,arc=0mm},overlay unbroken and first={\pgfmathsetmacro{\textPosX}{width("#3##1")1pt/1cm} \node[anchor=west] 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)+(\textPosX,0)+(0.215,0)$);},overlay middle and last={\draw[very thick](frame.north west)--(frame.south west);},after={\vspace{1ex} \noindent}]} {\end{tcolorbox}}} } \definebox{theoremtest}{red}{Theorem name}{o}

\begin{theoremtest} test \end{theoremtest} \end{document}

Ingmar
  • 6,690
  • 5
  • 26
  • 47
Flowt
  • 181

1 Answers1

1

Modified after comment

            \documentclass{article}
    \begin{document}
    \newlength{\longueurA}
    \settowidth{\longueurA}{Theorem name}
    \the\longueurA
\newlength{\longueurB}
\settowidth{\longueurB}{\textbf{\textsc{Theorem name}}}
\the\longueurB
%https://tex.stackexchange.com/questions/41370/what-are-the-possible-dimensions-sizes-units-latex-understands/41371#41371
%
%https://tex.stackexchange.com/questions/8260/what-are-the-various-units-ex-em-in-pt-bp-dd-pc-expressed-in-mm
\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother

\convertto{cm}{\longueurB}cm
\end{document}

Modified 30/05/22 the conversion with expl3

    \documentclass{article}
    % the doc The LATEX3 Interfaces
    % section 25.7 Using dim expressions and variables
    \ExplSyntaxOn
    \NewDocumentCommand{\myconversion}{m}{%
        \dim_to_decimal_in_unit:nn { #1 } { 1cm }
    }
    \ExplSyntaxOff
    \begin{document}
    \newlength{\longueurB}
    \settowidth{\longueurB}{\textbf{\textsc{Theorem name}}}
    \the\longueurB
\myconversion{\longueurB}cm
\end{document}

pascal974
  • 4,652
  • The problem is that it is in pt, tikz needs cm. – Flowt May 16 '22 at 11:57
  • Sorry to read this message so late. I found on this link https://tex.stackexchange.com/questions/41370/what-are-the-possible-dimensions-sizes-units-latex-understands/41371#41371, see the answer

    I edited the answer.

    – pascal974 May 17 '22 at 07:10