4

I have a number of different styles defined for different purposes. For two of the styles I would like them to use the same counter. How can I do this?

A sample of code that demonstrates what I currently have and what I am expecting is as follows:

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

Here is an image showing what I currently have. I would like the last box to have the caption number 2.3:

enter image description here

1 Answer 1

6

In the definition of the second tcblisting, called extended, replace auto counter by use counter from=basic. For details, see the tcolorbox manual; in the one for version 3.94 you find the information on counters in section 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}

Note that hyperref should be loaded last.

enter image description here

4
  • You arrived first, I just added an example of code (and a recommendation).
    – egreg
    Commented Sep 14, 2016 at 23:10
  • 1
    @egreg Thanks for blasting my mini-post. Wouldn't it be better to remove the auto counter of the second tcblisting definition? I guess it generates a new counter which remains unused.
    – gernot
    Commented Sep 14, 2016 at 23:17
  • Thank you both of you. And thank you @egreg for the hint of putting hyperref last. I was not aware of that.
    – kojow7
    Commented Sep 14, 2016 at 23:19
  • @gernot Of course!
    – egreg
    Commented Sep 15, 2016 at 6:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .