1

I've defined a tcolorbox listing as follows:

\newtcblisting[auto counter,
               number within=chapter,
               crefname={Lis.}{Lis.},
               ...

It works fine in books, but I need to turn off number within in smaller documents. I've tried these approaches:

  • \@ifundefined{chapter}{}{number within=chapter},} but that confuses pgfkeys.

  • number within=\@ifundefined{chapter}{???}{chapter}, but I don't know what to use for ??? so the key remains unset (I tried \pgfkeysnovalue, but no luck).

  • \@ifundefined{chapter}{}{\tcbset{new/number within=chapter}} but this doesn't seem to have any effect whatsoever.

What's the right approach for what I'm trying to do?

Here's a code example. It would work if the document is a book, but not an article. The goal is to somehow use \@ifundefined{chapter} somewhere to disable the number within setting when there are no chapters:

\documentclass{article} % works when book
\usepackage{tcolorbox,cleveref}
\tcbuselibrary{skins,listings,breakable}

\newtcblisting[auto counter,
               number within=chapter,
               crefname={Lis.}{Lis.},
               Crefname={Listing}{Listings},
               list inside=lis
              ]{code}[1][]{%
              comment={Listing~\thetcbcounter: #1},
              listing and comment}

\begin{document}

\begin{code}[bar]
foo
\end{code}
\end{document}
1

3 Answers 3

1

Define a new and general utility key /utils/ifundefined={<csname>}{<options if true>}{<options otherwise>}.

\documentclass{article}
\usepackage{tcolorbox,cleveref}
\tcbuselibrary{skins,listings,breakable}

\makeatletter
\pgfkeys{
  /utils/ifundefined/.code n args={3}{%
    \@ifundefined{#1}{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}%
  }
}
\makeatother

\newtcblisting[
  auto counter,
  /utils/ifundefined={chapter}{}{number within=chapter},
  crefname={Lis.}{Lis.},
  Crefname={Listing}{Listings},
  list inside=lis
]{code}[1][]{
  comment={Listing~\thetcbcounter: #1},
  listing and comment
}

\begin{document}
\section{Test}

\begin{code}[bar]
foo
\end{code}

\begin{code}[bar]
foo
\end{code}
\end{document}

enter image description here

With document class report instead of article: enter image description here

2

Define a suitable style for the new family of keys.

\documentclass{article}
\usepackage{tcolorbox,cleveref}
\tcbuselibrary{skins,listings,breakable}

\makeatletter
\tcbset{
  new/.cd,
  number within highest/.estyle={
    number within = \@ifundefined{chapter}{section}{chapter}
  }
}

\newtcblisting[
  auto counter,
  number within highest,
  crefname={Lis.}{Lis.},
  Crefname={Listing}{Listings},
  list inside=lis
]{code}[1][]{
  comment={Listing~\thetcbcounter: #1},
  listing and comment
}

\begin{document}

\section{Test}

\begin{code}[bar]
foo
\end{code}

\end{document}

enter image description here

1
  • When chapter is undefined, I don't want listings numbered by sections. Rather, I want them numbered globally, i.e., without any "number within" at all. Nov 9, 2022 at 16:42
0

Using the previous answer as a starting point, I came up with this:

\@ifundefined{chapter}{\tcbset{new/.cd,number within if chapter/.style={}}}{
\tcbset{new/.cd,number within if chapter/.style={number within = chapter}}}

I still feel this could be simplified, but at least it seems to work for now.

You must log in to answer this question.

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