cleveref 将多种类型的标签框作为一个整体处理

cleveref 将多种类型的标签框作为一个整体处理

这是我的问题的后续在 cleveref 中缩写“listing”,随着 MWE 变得越来越长:

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

% Code box, numbered, unbreakable
\newtcblisting[auto counter,
               number within=chapter,
               crefname={lis.}{lis.},
               Crefname={Listing}{Listings},
               list inside=lis
              ]{labelboxcode}[4][]{%
    title=#2,
    listing options={language=#2,#1},
    comment={Listing~\thetcbcounter: #4},
    list text={#4},
    label=#3,
    listing and comment
}

% Code box, numbered, breakable
\newtcblisting[use counter from=labelboxcode,
               crefname={lis.}{lis.},
               Crefname={Listing}{Listings},
               list inside=lis
              ]{labelboxcode*}[4][]{%
    breakable,
    title=#2,
    listing options={language=#2,#1},
    comment={Listing~\thetcbcounter: #4},
    list text={#4},
    label=#3,
    listing and comment
}

\begin{document}
\chapter{Demo}

This is a Java program:

\begin{labelboxcode}{Java}{java-listing}{A Java program, contrast with \cref{scala-listing}.}
int abs(int x) {
  if (x > 0)
    return x;
  else
    return -x;
}
\end{labelboxcode}
\Cref{java-listing} can be referenced as \cref{java-listing}.

\Cref{scala-listing} is the same program in Scala:

\begin{labelboxcode}{Scala}{scala-listing}{A Scala program, contrast with \cref{java-listing}.}
def abs(x: Int) = if x > 0 then x else -x
\end{labelboxcode}

\Cref{java-listing,scala-listing} compute the same function.

This is a breakable listing:

\begin{labelboxcode}{ML}{sml-listing}{An SML program.}
fun abs x = if x > 0 then x else ~x;
\end{labelboxcode}

All three listings can be referred to in one swoop: \cref{java-listing,scala-listing,sml-listing}
\end{document}

这个想法是将编程语言的框定义为两种变体,可破坏和不可破坏。上面的代码使用不可破坏框,并且运行良好。特别是,它one swoop: lis. 1.1 to 1.3在末尾生成行。当其中一个框设置为可破坏时,我的问题就开始了。列表仍然正确编号(即在同一系列内),但cleveref在多个引用中不起作用。例如,如果将最后一个labelboxcode更改为labelboxcode*,则输出的最后一行将变为::one swoop: lis. 1.1 and 1.2 and lis. 1.3正确的数字,但 1.3 与 1.1 和 1.2 分开处理。

答案1

经过反复尝试,我想我明白了:添加label type=labelboxcode定义labelboxcode*似乎可以解决问题。

相关内容