我找到了这个关联解释了如何在 tcolorbox 中为示例添加索引号。但是,我无法修改它以包含章节参考编号(例如,示例 1.1 或 4.3 等)。有什么提示吗?
答案1
更新于 2015-08-29按照@Qasim 的要求,包含一个标签。
tcolorbox
将选项number freestyle
与auto counter
和一起 使用number within=chapter
,如以下 MWE 所示。您还可以label
通过使用可选(第一个)参数来添加\newtcolorbox
(第二个必需参数将是标题)。
\documentclass{book}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{lipsum}
\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexample}[2][]{%
enhanced,
breakable,
fonttitle=\bfseries,
title=Example~\thetcbcounter: #2,
#1
}
\begin{document}
\chapter{First chapter}
\begin{myexample}[label=exfirst]{First example}
\lipsum[4]
\end{myexample}
\begin{myexample}{Second example}
\lipsum[4]
\end{myexample}
\chapter{Second chapter}
\begin{myexample}[label=exthird]{Third example}
\lipsum[4]
\end{myexample}
I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}
如果您还希望标题是可选的,您可以使用以下方法:
\documentclass{book}
\usepackage{etoolbox}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{lipsum}
\tcbset{mytitle/.style={title={Example~\thetcbcounter\ifstrempty{#1}{}{: #1}}}}
\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexample}[1][]{%
enhanced,
breakable,
fonttitle=\bfseries,
mytitle={},
#1
}
\begin{document}
\chapter{First chapter}
\begin{myexample}[mytitle={First example}, label=exfirst]
\lipsum[4]
\end{myexample}
\begin{myexample}
\lipsum[4]
\end{myexample}
\chapter{Second chapter}
\begin{myexample}[mytitle={Third example}, label=exthird]
\lipsum[4]
\end{myexample}
I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}
答案2
您需要重新定义exa
计数器来实现您的目标:
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newcounter{exa}
\renewcommand\theexa{\thechapter.\arabic{exa}}
\tcbmaketheorem{Examples}{Example}{
breakable,
fonttitle=\bfseries,
}{exa}{ex}
\begin{document}
\chapter{Foo}
\begin{Examples}{Additional description}{test}
\lipsum[4]
\end{Examples}
\end{document}