3

Cleveref package doesn't find the label inside the float environment. Instead, it shows ???

Do I need to add additional properties to float?

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath,amssymb}
\usepackage{amsthm}
\usepackage{mathrsfs}
\usepackage{cleveref}
\usepackage{float}

\newfloat{Code}{htbp}{lop}

\pagenumbering{gobble}

\begin{document}

\Cref{some-reference}

\begin{Code}
   \begin{algorithmic}
    \Procedure{\texttt{AG\_EVAL}}{$\hat{r}, \mathit{Val}$}
        \If{$\hat{r} \equiv v_0 \texttt{=} g( v_1, \dots, v_n)$}
            \For{$\forall i \in [1, n]$}
                \If{$v_i \not \in \mathit{Val}$}
                    \State Error: \emph{Cannot read attribute instance value before its ready.}
                \EndIf
            \EndFor
            \State $\mathit{Val}(v_0) \gets g( \mathit{Val}(v_1), \dots, \mathit{Val}(v_n))$
        \EndIf
    \EndProcedure
\end{algorithmic}
\caption{This is an important code}
\label{some-reference}
\end{Code}


\end{document}

enter image description here

4
  • 2
    The \label directive must come after, not before, the \caption statement.
    – Mico
    Oct 10, 2021 at 17:29
  • @Mico updated the question. Didn't solve it.
    – Node.JS
    Oct 10, 2021 at 17:31
  • @Mico can you please elaborate futher? I am not very familiar with Latex, how to get around this problem?
    – Node.JS
    Oct 10, 2021 at 17:40
  • Please see the answer I just posted.
    – Mico
    Oct 10, 2021 at 17:47

1 Answer 1

5

The replacement of a suitable label string with ?? happens because the OP defines and uses a new float type, called Code. While cleveref is quite clever, it ain't nearly clever enough to figure out on its own which label string to use for newly-hatched floating environments. Fortunately, the cleveref package provides the macros \crefname and \Crefname just so that users may inform the package which text labels should be used with a newly devised float type.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath,amssymb,amsthm,mathrsfs}

\usepackage{tikz}
\usetikzlibrary{positioning,calc,arrows.meta}%arrows is deprecated

\usepackage{framed,blindtext}
\pagenumbering{gobble}

\usepackage{float}
\newfloat{Code}{htbp}{lop} % <-- run this *before* loading `cleveref`

\usepackage[colorlinks,allcolors=blue]{hyperref} % optional
\usepackage[nameinlink]{cleveref} % load this package *last*
\crefname{Code}{code}{codes} % <-- new
\Crefname{Code}{Code}{Codes} % <-- new

\begin{document}

\noindent
\Cref{some-reference}

\begin{Code}
   \begin{algorithmic}
    \Procedure{\texttt{AG\_EVAL}}{$\hat{r}, \mathit{Val}$}
        \If{$\hat{r} \equiv v_0 \mathrel{\texttt{=}} g( v_1, \dots, v_n)$}
            \For{$\forall i \in [1, n]$}
                \If{$v_i \not \in \mathit{Val}$}
                    \State Error: \emph{Cannot read attribute instance value before its ready.}
                \EndIf
            \EndFor
            \State $\mathit{Val}(v_0) \gets g( \mathit{Val}(v_1), \dots, \mathit{Val}(v_n))$
        \EndIf
    \EndProcedure
\end{algorithmic}
\caption{This is an important code}
\label{some-reference} % <-- always place \label after \caption
\end{Code}
\end{document}
2
  • I would remove it. In stead it you may address importance of order of packages loading (in my comment should be stated "It seems that geometry packages should be loaded after cleveref instead of a sentence in broken English). What you thing about this?
    – Zarko
    Oct 10, 2021 at 18:38
  • 1
    You are right, My new test show, that placement of geometry in preamble doesn't mater.
    – Zarko
    Oct 10, 2021 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .