2

Context : I wanted to create a colored box to make nice looking theorems. Using the tcolorbox package, I managed to do so, using the following code :

\documentclass[12pt]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[cyr]{aeguill}

\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}


\usepackage{amsmath, amssymb, amsthm}
\usepackage{mathrsfs}

\newtheoremstyle{enoncer} 
{\topsep} %1) Espace avant 
{\topsep} %2) Espace apres 
{} %3) Police utilisee par le style du thm 
{} %4) Indentation (vide = aucune, \parindent = indentation paragraphe) 
{\bfseries} %5) Police du titre du thm 
{ :} %6) Ponctuation apres le titre du thm 
{ } %7) Espace apres le titre du thm (\newline = linebreak)
{\thmname{#1}\thmnumber{ #2}\thmnote{ (\normalfont{#3})}} %8) Composants du titre du thm : \thmname = nom du thm, \thmnumber = numéro du thm, \thmnote = sous-titre du thm 

\theoremstyle{enoncer}
%\newtheorem{Thm}{Théorème}[chapter]
\newtcbtheorem[number within=chapter]{Thm}{Théorème}{breakable, enhanced, attach boxed title to top left={xshift=3mm,yshift=-3mm,yshifttext=-1mm}, fonttitle=\bfseries, coltitle=black!100,  colbacktitle=white!100, description delimiters parenthesis, description font=\mdseries, colback=white!100, colframe=red!70!black, separator sign none, terminator sign={\ :}}{Thm}

%\tcolorboxenvironment{Thm}{breakable, enhanced, attach boxed title to top left={xshift=3mm,yshift=-3mm,yshifttext=-1mm}, coltitle=black!100, colbacktitle=white!100, colback=white!100, colframe=red!70!black}

\begin{document}

\chapter{Chapter}
    \section{Introduction}

------------------------------------------------------------------------------------------

        \begin{Thm}{Pythagore}{}
            Text of my theorem
        \end{Thm}

------------------------------------------------------------------------------------------

\end{document}

The result is exactly what I was hoping for :

enter image description here

My problem : I used the tcolorbox manual and found out that I can use my already made theorem environment to make the previous result. I'm quite interested since it could be very usefull :

  1. I will be able to "switch" which type of theorem environment I want to use very easily by commenting only one line for each theorem (instead of two lines otherwise)
  2. It won't take time to adjust to the tcolorbox command I defined in the first part. More precisely, I won't have to write \begin{Thm}{}{} instead of \begin{Thm} everytime I have to write a theorem (and I do a lot).

Anyway, I end up with this code :

\documentclass[12pt]{report}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[cyr]{aeguill}

\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}


\usepackage{amsmath, amssymb, amsthm}
\usepackage{mathrsfs}

\newtheoremstyle{enoncer} 
{\topsep} %1) Espace avant 
{\topsep} %2) Espace apres 
{} %3) Police utilisee par le style du thm 
{} %4) Indentation (vide = aucune, \parindent = indentation paragraphe) 
{\bfseries} %5) Police du titre du thm 
{ :} %6) Ponctuation apres le titre du thm 
{ } %7) Espace apres le titre du thm (\newline = linebreak)
{\thmname{#1}\thmnumber{ #2}\thmnote{ (\normalfont{#3})}} %8) Composants du titre du thm : \thmname = nom du thm, \thmnumber = numéro du thm, \thmnote = sous-titre du thm 

\theoremstyle{enoncer}
\newtheorem{Thm}{Théorème}[chapter]
%\newtcbtheorem[number within=chapter]{Thm}{Théorème}{breakable, enhanced, attach boxed title to top left={xshift=3mm,yshift=-3mm,yshifttext=-1mm}, fonttitle=\bfseries, coltitle=black!100,  colbacktitle=white!100, description delimiters parenthesis, description font=\mdseries, colback=white!100, colframe=red!70!black, separator sign none, terminator sign={\ :}}{Thm}

\tcolorboxenvironment{Thm}{breakable, enhanced, attach boxed title to top left={xshift=3mm,yshift=-3mm,yshifttext=-1mm}, coltitle=black!100, colbacktitle=white!100, colback=white!100, colframe=red!70!black}

\begin{document}

\chapter{Chapter}
    \section{Introduction}

------------------------------------------------------------------------------------------

        \begin{Thm}[Pythagore]
            Text of my theorem
        \end{Thm}

------------------------------------------------------------------------------------------

\end{document}

But in the end, I only get the following result : enter image description here

But I would like to get the same result as in the first picture. What I don't understand is : why does attach boxed title does not work here and what can I do to correct this problem ?

The same questions have been posted here but there as been no satisfying answer.

I'll gladly take any form of help ! Thanks for your time !

2 Answers 2

2

The reason you don't see the attached boxed title is that when you wrap your Thm environment with \tcolorboxenvironment, the resulting box has no title key (\tcolorboxenvironment is a very generic wrapper; tcolorbox doesn't see your optional argument when you use the resulting environment; it is only handled by the amsthm environment). If you are not convinced yet, add title=foo at the end of the second argument of \tcolorboxenvironment:

\tcolorboxenvironment{Thm}{breakable, enhanced, attach boxed title to top
  left={xshift=3mm,yshift=-3mm,yshifttext=-1mm}, coltitle=black!100,
  colbacktitle=white!100, colback=white!100, colframe=red!70!black, title=foo}

and you'll see the boxed “foo” title. Now, regarding what you want to do, I think the best way is to use \newtcbthorem. In order to keep your existing syntax, you can simply wrap the environment produced by \newtcbthorem, like this:

\documentclass{article}
\usepackage{xparse}
\usepackage[breakable, skins, theorems]{tcolorbox}

\newtcbtheorem{TcbThm}{Théorème}{breakable, enhanced,
  label separator=-, % safer char if you have ':' active in your French setup
  attach boxed title to top left={xshift=3mm, yshift=-3mm, yshifttext=-1mm},
  fonttitle=\bfseries, coltitle=black!100, colbacktitle=white!100,
  colback=white!100, colframe=red!70!black,
  separator sign none, description delimiters parenthesis,
  description font=\mdseries}{th}

\NewDocumentEnvironment{Thm}{ O{} O{} } % two optional arguments
  {\TcbThm{#1}{#2}}
  {\endTcbThm}

\begin{document}

\section{Introduction}

\begin{Thm}[Pythagore]
  Text of my theorem
\end{Thm}

\begin{Thm}
  Text of the \emph{Theorem With No Name}. Right, I have no name. If only I
  had a label like theorem~\ref{th-other-unnamed}...
\end{Thm}

\begin{Thm}[][other-unnamed]
  Text of the \emph{Labelled Theorem With No Name}. Hehe, I have a label, I
  can be referenced. :-)
\end{Thm}

\end{document}

enter image description here

2
  • Looks like it is what I'm searching for ! But do you mind explaning how the \NewDocumentEnvironment works ? I do not find explanations about it and it seems usefull... Thanks again ! Feb 6, 2020 at 17:59
  • 1
    Glad to help. \NewDocumentEnvironment is described in the xparse manual. And yes, it is more powerful than \newenvironment. It is a little bit of LaTeX3. :-)
    – frougon
    Feb 6, 2020 at 18:01
0

I'm not sure whether this meets your requirement, but it's a suggestion to you:::

\documentclass{article}
\usepackage{lipsum}
\usepackage{amsmath,amssymb}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}

\newtcolorbox[auto counter]{theoreme}[2][]{%
    colback=white,
    colframe=red!70!black,
    colbacktitle=white!90!red,
    coltitle=black,
    fonttitle=\bfseries, 
    title=Theorem~\thetcbcounter.\ #2,
    enhanced,
    attach boxed title to top left={yshift=-2mm, xshift=0.5cm},%
    #1% 
}




\begin{document}



\begin{theoreme}{A nice theorem}
Text for theorem
\end{theoreme}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .