1

Here's my MWE:

\documentclass{article}

\usepackage{varwidth}
\usepackage{thmtools}
\usepackage[most,many,breakable]{tcolorbox}

\tcbuselibrary{theorems,skins,hooks}
\usetikzlibrary{arrows,calc,shadows.blur}

% Create the Definition Environment
\newtcbtheorem[number within=section]{Definition}{Definition}{enhanced,
  before skip=2mm,after skip=2mm, colback=red!5,colframe=red!80!black,boxrule=0.5mm,
  attach boxed title to top left={xshift=1cm,yshift*=1mm-\tcboxedtitleheight}, varwidth boxed title*=-3cm,
  colbacktitle=red!75!black,
  boxed title style={frame code={
      \path[fill=tcbcolback]
      ([yshift=-1mm,xshift=-1mm]frame.north west)
      arc[start angle=0,end angle=180,radius=1mm]
      ([yshift=-1mm,xshift=1mm]frame.north east)
      arc[start angle=180,end angle=0,radius=1mm];
      \path[left color=tcbcolback!60!black,right color=tcbcolback!60!black,
      middle color=tcbcolback!80!black]
      ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
      [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
      -- (frame.south east) -- (frame.south west)
      -- ([xshift=-1mm,yshift=-1mm]frame.north west)
      [sharp corners]-- cycle;
    },interior engine=empty,
  },
  fonttitle=\bfseries,
title={#2},#1}{def}

\begin{document}
  \begin{Definition}{Hello}{}
    Hello
  \end{Definition}
\end{document}

which produces this:

enter image description here

Is there a way that I can use the same environment, but instead of using {}, I can use []. For example:

\begin{document}
  \begin{Definition}[Hello]
    Hello
  \end{Definition}
\end{document}

When I compile the file using the second method, here's the error:

! Package pgfkeys Error: I do not know the key '/tcb/Hello' and I am going to i
gnore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.35     He
           llo
?

I'm using pdflatex to compile btw.

1 Answer 1

1

Define two environments, the second of which is called Definition. In case you need to use the label, I provide a second optional argument.

\documentclass{article}

\usepackage{varwidth}
\usepackage{thmtools}
\usepackage[most,many,breakable]{tcolorbox}

\tcbuselibrary{theorems,skins,hooks}
\usetikzlibrary{arrows,calc,shadows.blur}

% Create the Definition Environment
\newtcbtheorem[number within=section]{Definition*}{Definition}{
  enhanced,
  before skip=2mm,
  after skip=2mm,
  colback=red!5,
  colframe=red!80!black,
  boxrule=0.5mm,
  attach boxed title to top left={xshift=1cm,yshift*=1mm-\tcboxedtitleheight},
  varwidth boxed title*=-3cm,
  colbacktitle=red!75!black,
  boxed title style={frame code={
      \path[fill=tcbcolback]
      ([yshift=-1mm,xshift=-1mm]frame.north west)
      arc[start angle=0,end angle=180,radius=1mm]
      ([yshift=-1mm,xshift=1mm]frame.north east)
      arc[start angle=180,end angle=0,radius=1mm];
      \path[left color=tcbcolback!60!black,right color=tcbcolback!60!black,
      middle color=tcbcolback!80!black]
      ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
      [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
      -- (frame.south east) -- (frame.south west)
      -- ([xshift=-1mm,yshift=-1mm]frame.north west)
      [sharp corners]-- cycle;
    },
    interior engine=empty,
  },
  fonttitle=\bfseries,
  title={#2},
  #1
}{def}

\NewDocumentEnvironment{Definition}{O{}O{}}
 {\begin{Definition*}{#1}{#2}}{\end{Definition*}}

\begin{document}

\begin{Definition}
Hello
\end{Definition}

\begin{Definition}[Hello]
Hello
\end{Definition}

\begin{Definition}[][label-a]
Hello
\end{Definition}

\begin{Definition}[Hello again][label-b]
Hello
\end{Definition}

\ref{def:label-a} and \ref{def:label-b}

\end{document}

enter image description here

3
  • How would I reset the counter for the definition theorem? I created a command called lecture that increments the section counter, but uses \section*{}. How would I reset the theorem without having to create a numbered section? Oct 19, 2022 at 16:57
  • @SingularisArt That's a wholly different question, I believe.
    – egreg
    Oct 19, 2022 at 17:00
  • All right. I'll open another question probably tomorrow. Thanks for your help! Oct 19, 2022 at 17:03

You must log in to answer this question.

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