使用 [] 代替 newtcbtheorem 中的 {}

使用 [] 代替 newtcbtheorem 中的 {}

这是我的 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}

结果是:

在此处输入图片描述

有没有办法让我使用相同的环境,但不使用{},而是使用[]。例如:

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

当我使用第二种方法编译文件时,出现以下错误:

! 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
?

顺便说一下,我正在用来pdflatex编译。

答案1

定义两个环境,第二个环境称为Definition。如果您需要使用标签,我提供了第二个可选参数。

\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}

在此处输入图片描述

相关内容