我正在切换到 来pseudo
展示我的算法。按照文档,我将其与 结合起来tcolorbox
创建了一个名为 的浮动环境algorithm
。
但是当我想使用\cref
fromcleveref
包引用它时,该命令无法捕获与其相关的浮动环境。因此我得到了?? 1
而不是alg: 1
(或类似的东西)。
以下是 MWE:
\documentclass{article}
\usepackage{tcolorbox} % colored box with a lot of flexibility
\tcbuselibrary{skins,theorems} % extra lib for pseudo
\usepackage{pseudo} % for pseudocode
\newtcbtheorem{algorithm}{Algorithm}{pseudo/booktabs, float}{alg} % create a float env for algorithms
\usepackage{cleveref}
\begin{document}
\begin{algorithm}{Euclid’s algorithm, \pr{Euclid}(a, b)}{euclid}
\textbf{Input:} Two positive integers, $a$ and $b$.\\
\textbf{Output:} The greatest common divisor of $a$ and $b$.
\begin{pseudo}[label=\small\arabic*, indent-mark]
while $a \neq b$ \ct{If equal, both are gcd} \\+
if $a > b$ \ct{ Reduce max with multiple of min} \\+
$a = a - b$ \ct{ $a$ is largest} \\-
else $b = b - a$ \ct{ $b$ is largest} \\-
return $a$ \ct{ Both are gcd, so return one}
\end{pseudo}
The running time is quadratic in the number of bits in the input.
\end{algorithm}
A sentence with a clever reference \cref{alg:euclid}.
\end{document}
我该如何解决这个问题?
答案1
只需使用以下命令设置新 tcolorbox 的标签类型label type=algorithm
:
\documentclass{article}
\usepackage{tcolorbox} % colored box with a lot of flexibility
\tcbuselibrary{skins,theorems} % extra lib for pseudo
\usepackage{pseudo} % for pseudocode
\usepackage{cleveref}
\newtcbtheorem{algorithm}{Algorithm}{label type=algorithm,pseudo/booktabs, float}{alg} % create a float env for algorithms
\begin{document}
\begin{algorithm}{Euclid’s algorithm, \pr{Euclid}(a, b)}{euclid}
\textbf{Input:} Two positive integers, $a$ and $b$.\\
\textbf{Output:} The greatest common divisor of $a$ and $b$.
\begin{pseudo}[label=\small\arabic*, indent-mark]
while $a \neq b$ \ct{If equal, both are gcd} \\+
if $a > b$ \ct{ Reduce max with multiple of min} \\+
$a = a - b$ \ct{ $a$ is largest} \\-
else $b = b - a$ \ct{ $b$ is largest} \\-
return $a$ \ct{ Both are gcd, so return one}
\end{pseudo}
The running time is quadratic in the number of bits in the input.
\end{algorithm}
A sentence with a clever reference \cref{alg:euclid}.
\end{document}
这比@egreg 建议的设置内部 tcb 计数器稍微好一些/容易一些\crefname
,因为您不需要处理内部计数器名称,而且它已经处理了\Crefname
,这取决于您的 caps 选项。
或者,如果您愿意或需要设置crefname
,您可以使用的相应选项进行设置tcolorbox
(这几乎是来自pseudo
的用户手册的示例):
\documentclass{article}
\usepackage{tcolorbox} % colored box with a lot of flexibility
\tcbuselibrary{skins,theorems} % extra lib for pseudo
\usepackage{pseudo} % for pseudocode
\usepackage{cleveref}
\newtcbtheorem[crefname = {algorithm}{algorithms}]{algorithm}{Algorithm}{pseudo/booktabs, float}{alg} % create a float env for algorithms
\begin{document}
\begin{algorithm}{Euclid’s algorithm, \pr{Euclid}(a, b)}{euclid}
\textbf{Input:} Two positive integers, $a$ and $b$.\\
\textbf{Output:} The greatest common divisor of $a$ and $b$.
\begin{pseudo}[label=\small\arabic*, indent-mark]
while $a \neq b$ \ct{If equal, both are gcd} \\+
if $a > b$ \ct{ Reduce max with multiple of min} \\+
$a = a - b$ \ct{ $a$ is largest} \\-
else $b = b - a$ \ct{ $b$ is largest} \\-
return $a$ \ct{ Both are gcd, so return one}
\end{pseudo}
The running time is quadratic in the number of bits in the input.
\end{algorithm}
A sentence with a clever reference \cref{alg:euclid}.
\end{document}
就其效果而言,这严格等同于@egreg 的回答,只是您不需要知道/担心计数器的内部名称tcolorbox
。
答案2
你收到警告
LaTeX Warning: cref reference format for label type `tcb@cnt@algorithm'
undefined on input line 26.
定义格式。
并且,如果您不想让读者的眼睛刺痛,请不要在开括号后(或闭括号前)留空格。
\documentclass{article}
\usepackage{tcolorbox} % colored box with a lot of flexibility
\tcbuselibrary{skins,theorems} % extra lib for pseudo
\usepackage{pseudo} % for pseudocode
\usepackage{cleveref}
% create a float env for algorithms
\newtcbtheorem{algorithm}{Algorithm}{pseudo/booktabs, float}{alg}
\crefname{tcb@cnt@algorithm}{algorithm}{algorithms}
\begin{document}
\begin{algorithm}{Euclid’s algorithm, \pr{Euclid}(a, b)}{euclid}
\textbf{Input:} Two positive integers, $a$ and $b$.\\
\textbf{Output:} The greatest common divisor of $a$ and $b$.
\begin{pseudo}[label=\small\arabic*, indent-mark]
while $a \neq b$ \ct{If equal, both are gcd} \\+
if $a > b$ \ct{Reduce max with multiple of min} \\+
$a = a - b$ \ct{$a$ is largest} \\-
else $b = b - a$ \ct{$b$ is largest} \\-
return $a$ \ct{Both are gcd, so return one}
\end{pseudo}
The running time is quadratic in the number of bits in the input.
\end{algorithm}
A sentence with a clever reference to \cref{alg:euclid}.
\end{document}