Warum erfasst Cleveref kein Float von Pseudo+Tcolorbox?

Warum erfasst Cleveref kein Float von Pseudo+Tcolorbox?

Ich wechsle zu , pseudoum meine Algorithmen vorzustellen. Gemäß der Dokumentation habe ich es mit kombiniert, tcolorboxum eine schwebende Umgebung namens zu erstellen algorithm.

\crefWenn ich es aber mit from package referenzieren möchte cleveref, kann der Befehl die zugehörige Floating-Umgebung nicht erfassen. Daher erhalte ich ?? 1stattdessen alg: 1(oder so ähnlich).

Hier ist ein 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}

Wie könnte ich dieses Problem lösen?

Antwort1

Legen Sie einfach den Beschriftungstyp für Ihre neue Farbbox fest mit 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}

Bildbeschreibung hier eingeben

Dies ist etwas besser/einfacher als die Einstellung \crefnamefür den internen TCP-Zähler, wie von @egreg vorgeschlagen, da Sie sich nicht mit dem Namen des internen Zählers befassen müssen und dieser sich \Crefname, abhängig von Ihren Caps-Optionen, bereits darum kümmert.

Wenn Sie es vorziehen oder müssen crefname, können Sie dies alternativ mit tcolorboxder entsprechenden Option von tun (dies ist so ziemlich das Beispiel aus pseudodem Benutzerhandbuch von):

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

Dies entspricht in seinen Auswirkungen strikt der Antwort von @egreg, außer dass Sie den internen Namen des tcolorboxZählers nicht kennen/sich darüber Gedanken machen müssen.

Antwort2

Sie erhalten die Warnung

LaTeX Warning: cref reference format for label type `tcb@cnt@algorithm' 
undefined on input line 26.

Definieren Sie das Format.

Und verwenden Sie bitte keine Leerzeichen nach öffnenden Klammern (oder vor schließenden), wenn Sie Ihren Lesern keine Augen bluten lassen möchten.

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

Bildbeschreibung hier eingeben

verwandte Informationen