2

I'd like to add the current figure/section number to the title of a tcblisting. Right now, my hacky solution is to just use the section number and increment it manually every time I want to use my tcblisting. Is there any way to have it automatically increment (or, even better, make it reference the deepest section, e.g., if I'm in subsubsection 3, it should be labeled as "1.1.3").

MWE:

\documentclass{article}
\newcounter{mylisting}[section]
\renewcommand{\themylisting}{\thesection.\arabic{mylisting}}
\usepackage{etoolbox}
\usepackage[minted,skins]{tcolorbox}

\newtcblisting{MySchemeListing}[2][]{%
    enhanced,
    frame hidden, 
    borderline north = {1pt}{0pt}{black},
    borderline south = {1pt}{0pt}{black},
    opacityfill=0,
    listing only,
    title={\textcolor{black}{Listing \themylisting---#2\ifstrempty{#1}{}{~(\texttt{#1})}}},
    attach boxed title to top left={xshift=-3.55mm,yshift=-0.4mm},
    boxed title style={frame hidden,colback=black!0!white},
    minted language=scheme,
    minted options={autogobble},
}

\begin{document}
\section{Section 1}
\subsection{Subsection 1}
\subsubsection{Subsubsection 1}

\refstepcounter{mylisting}
\begin{MySchemeListing}[main.rkt]{Fibonacci code example in Racket}
    ;; ! : Number -> Number 
    ;; Computes the factorial of a number.
    ;; (define ! ...)
    (define !
      (lambda (n)
        (cond
          [(zero? n) 1]
          [else (* n (! (- n 1)))])))
\end{MySchemeListing}
\end{document}

1 Answer 1

2

Simply use auto counter, number within=section.

\documentclass{article}
\usepackage{etoolbox}
\usepackage[minted,skins]{tcolorbox}

\newtcblisting[auto counter, number within=section]{MySchemeListing}[2][]{%
    enhanced,
    frame hidden, 
    borderline north = {1pt}{0pt}{black},
    borderline south = {1pt}{0pt}{black},
    opacityfill=0,
    listing only,
    title={\textcolor{black}{Listing \thetcbcounter---#2\ifstrempty{#1}{}{~(\texttt{#1})}}},
    attach boxed title to top left={xshift=-3.55mm,yshift=-0.4mm},
    boxed title style={frame hidden,colback=black!0!white},
    minted language=scheme,
    minted options={autogobble},
}

\begin{document}
    \section{Section 1}
    \subsection{Subsection 1}
    \subsubsection{Subsubsection 1}
    
    \begin{MySchemeListing}[main.rkt]{Fibonacci code example in Racket}
        ;; ! : Number -> Number 
        ;; Computes the factorial of a number.
        ;; (define ! ...)
        (define !
        (lambda (n)
        (cond
        [(zero? n) 1]
        [else (* n (! (- n 1)))])))
    \end{MySchemeListing}
\end{document}

enter image description here

For a deeper numbering

\documentclass{article}
\usepackage{etoolbox}
\usepackage[minted,skins]{tcolorbox}

\newtcblisting[auto counter, number within=subsubsection]{MySchemeListing}[2][]{%
    enhanced,
    frame hidden, 
    borderline north = {1pt}{0pt}{black},
    borderline south = {1pt}{0pt}{black},
    opacityfill=0,
    listing only,
    title={\textcolor{black}{Listing \thetcbcounter---#2\ifstrempty{#1}{}{~(\texttt{#1})}}},
    attach boxed title to top left={xshift=-3.55mm,yshift=-0.4mm},
    boxed title style={frame hidden,colback=black!0!white},
    minted language=scheme,
    minted options={autogobble},
}

\begin{document}
    \section{Section 1}
    \subsection{Subsection 1}
    \subsubsection{Subsubsection 1}
    
    \begin{MySchemeListing}[main.rkt]{Fibonacci code example in Racket}
        ;; ! : Number -> Number 
        ;; Computes the factorial of a number.
        ;; (define ! ...)
        (define !
        (lambda (n)
        (cond
        [(zero? n) 1]
        [else (* n (! (- n 1)))])))
    \end{MySchemeListing}
\end{document}

enter image description here

You must log in to answer this question.

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