Почему CleverEf не захватывает float из pseudo+tcolorbox?

Почему CleverEf не захватывает float из pseudo+tcolorbox?

Я переключаюсь на , pseudoчтобы представить свои алгоритмы. Следуя документации, я объединил ее с , tcolorboxчтобы создать плавающую среду под названием algorithm.

Но когда я хочу сослаться на него с помощью \creffrom cleverefpackage, команда не может захватить плавающую среду, связанную с ним. Поэтому я получаю ?? 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}

введите описание изображения здесь

Это немного лучше/проще, чем настройка \crefnameвнутреннего счетчика tcb, как предлагает @egreg, поскольку вам не нужно иметь дело с именем внутреннего счетчика, и он уже с ним работает \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}

введите описание изображения здесь

Связанный контент