# Cleveref with hyperref and clrscode3e

I am looking for a way to use cleveref and hyperref in combination with the clrscode3e package. Unfortunately, the packages do not work together out-of-the-box.

More precisely, I am considering the following use case. For a given codebox, I want to be able to label a line and later reference it in the text using the \cref command. When typeset, this should print a clickable line number hyperlinking back to the line in the codebox. A MWE is depicted below.

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{clrscode3e}

\begin{document}
\begin{codebox}
\Procname{$\proc{MWE}$}
\li $x \gets 1$ \label{lin:assign}
\End
\end{codebox}

As shown in \cref{lin:assign}, \dots
\end{document}


I have posted a similar question not too long ago: Cleveref with clrscode3e. The question however did not involve the use of hyperref and the solution therefore cannot be mapped one-to-one. The reason, more precisely, is that the hyperlinks created by hyperref always point to the first codebox in the document, no matter the codebox containing the label.

You need to ensure that the hyperref destination name is unique. hyperref uses (if it exists) a \theH<counter> command. As your codebox has no counter, one must number the labels:

\documentclass{article}
\usepackage{clrscode3e}
\usepackage{hyperref}
\usepackage{cleveref}
\newcounter{codelabel}
\newcommand\theHcodelinenumber{\thecodelabel.\thecodelinenumber}

\newcommand\clrslabel[1]
{\stepcounter{codelabel}%
\refstepcounter{codelinenumber}%
\label{#1}}

\begin{document}
\begin{codebox}
\Procname{$\proc{MWE}$}
\li $x \gets 1$ \clrslabel{lin:assign}
\End
\end{codebox}

As shown in \cref{lin:assign}, \cref{lin:assign2}\dots

\newpage
\begin{codebox}
\Procname{$\proc{MWE}$}
\li $x \gets 1$ \clrslabel{lin:assign2}
\End
\end{codebox}

\end{document}

• Thanks again for providing a solution. \theHcodelinenumber seems to be defined already. Can I simply renew its definition using \renewcommand without complications? – foobar May 5 at 21:20
• In my example it isn't defined, if your document defines it, I would have to know this definition before being able to say definitivly, if you can redefine it, but the answer is probably yes. – Ulrike Fischer May 5 at 21:24
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.9240149259567261, "perplexity": 2275.362613163036}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2019-43/segments/1570987822458.91/warc/CC-MAIN-20191022155241-20191022182741-00413.warc.gz"}