如何在 tcblisting 之间共享计数器?

如何在 tcblisting 之间共享计数器?

我为不同目的定义了多种不同的样式。对于其中两种样式,我希望它们使用相同的计数器。我该怎么做?

下面是一个示例代码,演示了我当前拥有的内容以及我所期望的内容:

\documentclass{book}
\usepackage{hyperref}
\usepackage[table]{xcolor}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{inconsolata}
\usepackage{graphicx}
\tcbuselibrary{breakable}

\newtcblisting[auto counter,number within=chapter]{basic}[2][]{sharp corners, breakable,
    fonttitle=\bfseries, colframe=gray, listing only, 
    listing options={basicstyle=\ttfamily,language=php, showstringspaces=false, breaklines=true,tabsize=4}, 
    title=Basic Data Set \thetcbcounter: #2, #1}

\newtcblisting[auto counter,number within=chapter]{extended}[2][]{sharp corners, breakable,
    fonttitle=\bfseries, colframe=blue, listing only, 
    listing options={basicstyle=\ttfamily,language=php, showstringspaces=false, breaklines=true,tabsize=4}, 
    title=Extended Data Set \thetcbcounter: #2, #1}


\begin{document}

\chapter{Chapter One}

\chapter{Chapter Two}


\begin{basic}{}
    This is box one. It is 2.1.

\end{basic}

\begin{basic}{}
    This is box two. It is 2.2.

\end{basic}

\begin{extended}{}
    This is box three. It is using a different style. I would like it to be 2.3 instead of 2.1.
\end{extended}


\end{document}

这是一张显示我目前拥有的图片。我希望最后一个框的标题编号为 2.3:

在此处输入图片描述

答案1

在第二个定义中tcblisting,称为extendedauto counter用替换use counter from=basic。有关详细信息,请参阅tcolorbox 手册;在 3.94 版本中,您可以在第 5.1 节找到有关计数器的信息。

\documentclass{book}
\usepackage{inconsolata}

\usepackage[table]{xcolor}
\usepackage{graphicx}

\usepackage{listings}
\usepackage[most]{tcolorbox}
\tcbuselibrary{breakable}
\usepackage{hyperref}

\newtcblisting[auto counter,number within=chapter]{basic}[2][]{
  sharp corners,
  breakable,
  fonttitle=\bfseries,
  colframe=gray,
  listing only, 
  listing options={
    basicstyle=\ttfamily,
    language=php,
    showstringspaces=false,
    breaklines=true,
    tabsize=4
  }, 
  title=Basic Data Set \thetcbcounter: #2, #1
}

\newtcblisting[use counter from=basic]{extended}[2][]{
  sharp corners,
  breakable,
  fonttitle=\bfseries,
  colframe=blue,
  listing only, 
  listing options={
    basicstyle=\ttfamily,
    language=php,
    showstringspaces=false,
    breaklines=true,
    tabsize=4
  }, 
  title=Extended Data Set \thetcbcounter: #2, #1
}


\begin{document}

\chapter{Chapter One}

\chapter{Chapter Two}


\begin{basic}{}
    This is box one. It is 2.1.

\end{basic}

\begin{basic}{}
    This is box two. It is 2.2.

\end{basic}

\begin{extended}{}
    This is box three. It is using a different style. I would like it to be 2.3 instead of 2.1.
\end{extended}


\end{document}

请注意,应该hyperref最后加载。

在此处输入图片描述

相关内容