tcolorbox 出现“\caption{}”错误

tcolorbox 出现“\caption{}”错误

我想使用 tcolorbox 为我的图形添加轮廓。一切正常,直到我在标题中添加命令“\caption”。有人可以帮我修复这个错误。

\documentclass[openany,spanish,10pt, letterpaper]{memoir}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{anysize}
\marginsize{3cm}{2cm}{2cm}{2cm}
\usepackage{amsmath}
\usepackage{float}
\usepackage{polyglossia}
\setdefaultlanguage{spanish}
\usepackage{pgfgantt}
\usepackage[many]{tcolorbox}
\usepackage{varwidth}
\usepackage{xcolor}

\begin{document}
\begin{figure}

\begin{tcolorbox}[enhanced,title={\caption{TEST.\caption{a}}},
colframe=red,colback=white,colbacktitle=white,
fonttitle=\bfseries,coltitle=black,attach boxed title to top center=
{yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},         boxed title style={boxrule=0.5mm,
frame code={ \path[tcb fill frame] ([xshift=-4mm]frame.west)
-- (frame.north west) -- (frame.north east) -- ([xshift=4mm]frame.east)
-- (frame.south east) -- (frame.south west) -- cycle; },
interior code={ \path[tcb fill interior] ([xshift=-2mm]interior.west)
-- (interior.north west) -- (interior.north east)
-- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
-- cycle;}  }]
 \centering
 \includegraphics{example-image-a}
 \end{tcolorbox}    
 \end{figure}

 TEST \ref{a}

 \end{document}

答案1

解决方案之一是模拟标题,如下所示:

title={\refstepcounter{figure}
       Fig. \thefigure: TEST TEST TEST TEST
       \label{fig:a}
       },% <---
       % or Figure or whatever emulate
       % your figure captions style

通过此您将获得:

在此处输入图片描述

答案2

另一个解决方案是使用该blend into=figures选项来适应新的tcolorbox环境。

例如:

\documentclass[openany,spanish,10pt, letterpaper]{memoir}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{anysize}
\marginsize{3cm}{2cm}{2cm}{2cm}
\usepackage{amsmath}
\usepackage{float}
\usepackage{polyglossia}
\setdefaultlanguage{spanish}
\usepackage{pgfgantt}
\usepackage[many]{tcolorbox}
\usepackage{varwidth}
\usepackage{xcolor}

\newtcolorbox[blend into=figures]{myfigure}[2][]{enhanced,
  float=tbp,title={#2},
  colframe=red,colback=white,colbacktitle=white,
  fonttitle=\bfseries,coltitle=black,
  attach boxed title to top center={yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},
  boxed title style={boxrule=0.5mm,
    frame code={\path[tcb fill frame] ([xshift=-4mm]frame.west)
    -- (frame.north west) -- (frame.north east) -- ([xshift=4mm]frame.east)
    -- (frame.south east) -- (frame.south west) -- cycle; },
    interior code={\path[tcb fill interior] ([xshift=-2mm]interior.west)
    -- (interior.north west) -- (interior.north east)
    -- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
    -- cycle;}  },#1}

\begin{document}

  \begin{myfigure}[label=a,center upper]{Test}
    \includegraphics{example-image-a}
  \end{myfigure}

  TEST \ref{a}

\end{document}

得出:

在此处输入图片描述

相关内容