如何使用 tcblisting 里面的部分计数器?

如何使用 tcblisting 里面的部分计数器?

我想将当前图/节号添加到 的标题中tcblisting。目前,我的解决方案是只使用节号,每次我想使用 时手动增加它tcblisting。有没有办法让它自动增加(或者更好的是,让它引用最深的部分,例如,如果我在子节 3 中,它应该被标记为“1.1.3”)。

梅威瑟:

\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

只需使用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}

在此处输入图片描述

对于更深的编号

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

在此处输入图片描述

相关内容