我正在尝试在包装定义环境的 tcolorbox 中包含一个带标题的图形。
我的 MWE 是...
\documentclass{article}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{multido}
\newenvironment{definition}[1][]{
\par\addvspace{\topsep}
\begin{tcolorbox}[colback=yellow!5, arc=0pt, outer arc=0pt, boxrule=0.5pt]
\noindent\sffamily\upshape \textbf{Definition}
\ifx\empty#1\empty\else\ (#1)\fi. % Theorem name (optional)
\ \ignorespaces
}
{
\end{tcolorbox}
\par\nobreak
}
\begin{document}
\begin{definition}
Here is my definition
\begin{figure}[htbp]
\centering
\includegraphics[width=7.5cm]{example-image-a}
\caption{Hello}
\end{figure}
\end{definition}
\end{document}
如果没有标题,图像会显示出来,但如果我添加标题,图像就会消失。我该如何解决这个问题?
答案1
float.sty
除了选项之外H
,还有另一个建议,那MWE
就是:
\documentclass{article}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{multido}
\usepackage{float}
\newenvironment{definition}[1][]{
\par\addvspace{\topsep}
\begin{tcolorbox}[colback=yellow!5, arc=0pt, outer arc=0pt, boxrule=0.5pt]
\noindent\sffamily\upshape \textbf{Definition}
\ifx\empty#1\empty\else\ (#1)\fi. % Theorem name (optional)
\ \ignorespaces
}
{
\end{tcolorbox}
\par\nobreak
}
\begin{document}
\begin{definition}
Here is my definition
\begin{figure}[H]
\centering
\includegraphics[width=7.5cm]{example-image-a}
\caption{Hello}
\end{figure}
\end{definition}
\end{document}
请注意,该选项H
将图像放置在您放置的确切位置
答案2
\captionof
来自caption
包裹是你的朋友。
\documentclass{article}
% \usepackage{xcolor} % auto loaded by tcolorbox
\usepackage[most]{tcolorbox}
% \usepackage{multido}
\usepackage[strut=off]{caption}
\newenvironment{definition}[1][]{
\par\addvspace{\topsep}
\begin{tcolorbox}[colback=yellow!5, arc=0pt, outer arc=0pt, boxrule=0.5pt]
\noindent\sffamily\upshape \textbf{Definition}%
% use \ifstrempty from etoolbox, which is auto loaded by tcolorbox
\ifstrempty{#1}{}{\ (#1)}.% Theorem name (optional)
\ \ignorespaces
}
{
\end{tcolorbox}
\par\nobreak
}
\begin{document}
\listoffigures % \captionof contributes to list of figures
\begin{definition}
Here is my definition
\begin{minipage}{\linewidth}
\centering
\includegraphics[width=7.5cm]{example-image-a}
\captionof{figure}{Hello}
\end{minipage}
\end{definition}
\end{document}