cleveref が pseudo+tcolorbox から float をキャプチャしないのはなぜですか?

cleveref が pseudo+tcolorbox から float をキャプチャしないのはなぜですか?

アルゴリズムのプレゼンテーションに切り替えますpseudo。ドキュメントに従って、 と組み合わせて、tcolorboxというフローティング環境を作成しましたalgorithm

\crefしかし、 from packageで参照したい場合cleveref、コマンドはそれに関連するフローティング環境をキャプチャできません。そのため、?? 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内部カウンター名を処理する必要がなく、\Crefnamecaps オプションに応じて既に処理されているためです。

あるいは、 を設定したい場合や設定する必要がある場合は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}

ここに画像の説明を入力してください

関連情報