Por que o smartef não está capturando float do pseudo+tcolorbox?

Por que o smartef não está capturando float do pseudo+tcolorbox?

Estou mudando para pseudoapresentar meus algoritmos. Seguindo a documentação, combinei-a para tcolorboxcriar um ambiente flutuante chamado algorithm.

Mas quando quero referenciá-lo \crefdo cleverefpacote, o comando não consegue capturar o ambiente flutuante relacionado a ele. Portanto, recebo ?? 1em vez de alg: 1(ou algo parecido).

Aqui está um 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}

Como eu poderia resolver esse problema?

Responder1

Basta definir o tipo de etiqueta para seu novo tcolorbox com 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}

insira a descrição da imagem aqui

Isso é um pouco melhor/mais fácil do que configurar \crefnameo contador tcb interno, conforme sugerido por @egreg, já que você não precisa lidar com o nome do contador interno e ele já lida \Crefnametambém, dependendo das opções de limites.

Alternativamente, se você preferir ou precisar definir crefname, você pode fazê-lo usando tcolorboxa opção correspondente de (este é basicamente o exemplo do pseudomanual do usuário):

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

Isso é estritamente equivalente à resposta de @egreg em seus efeitos, exceto que você não precisa saber/se preocupar com o nome interno do tcolorboxcontador.

Responder2

Você recebe o aviso

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

Defina o formato.

E, por favor, não deixe espaços depois dos parênteses abertos (ou antes dos fechados) se não quiser que os olhos dos seus leitores sangrem.

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

insira a descrição da imagem aqui

informação relacionada