1

I have a custom command to display pseudocodes using tcolorbox. The \newtcblisting command creates a custom counter to display the boxes. I'm trying to reference the pseudocodes somewhere in the document showing this custom counter but I only managed to show the chapter counter using \ref.

Here's a sample code:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage[most]{tcolorbox}
\usepackage{minted}
\usepackage[
      colorlinks=true,
]{hyperref} 
\usepackage{etoolbox}

\begin{document}

\newtcblisting[auto counter,list inside=mypseudocodes]{pseudocodeV1}[3][]{% list entry={\thesection.\protect\numberline{\thetcbcounter}#2}, title={Pseudocode \thesection.\thetcbcounter\label{pseudocode:#3}: #2}, listing options={tabsize=2,escapeinside=||,mathescape=true}, listing only, enhanced, #1 }

\newtcblisting[auto counter,list inside=mypseudocodes]{pseudocodeV2}[3][]{% list entry={\thesection.\protect\numberline{\thetcbcounter}#2}, title={Pseudocode \thesection.\thetcbcounter\def@currentlabel{Custom ref Text}{\label{pseudocode:#3}}: #2}, listing options={tabsize=2,escapeinside=||,mathescape=true}, listing only, enhanced, #1 }

\tcblistof[\section*]{mypseudocodes}{List of Pseudocodes} \addcontentsline{toc}{chapter}{List of Pseudocodes}

\section{Test1} PseudocodeV1: \ref{pseudocode:code} \ \noindent PseudocodeV2: \ref{pseudocode:code2} \

\section{Test2}

\begin{pseudocodeV1}{ My Title }{code} a = 1; a = 2; \end{pseudocodeV1}

\section{T1}

\begin{pseudocodeV2}{ My Title }{code2} a = 1; a = 2; \end{pseudocodeV2}

\end{document}

Anyone knows why the command \def\@currentlabel{}{} isnt working or can suggest any workarround?

I've got the \def\@currentlabel{}{} suggestion from: Labeling a text and referencing it later

enter image description here

Qrrbrbirlbel
  • 119,821

1 Answers1

1

The problem was fixed by adding \makeatletter before \newtcblisting! Credit to Ulrike Fischer for the solution in the comments.

The code below illustrates the solution:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage[most]{tcolorbox}
\usepackage{minted}
\usepackage[
      colorlinks=true,
]{hyperref} 
\usepackage{etoolbox}

\begin{document}

\makeatletter

\newtcblisting[auto counter,list inside=mypseudocodes]{pseudocode}[3][]{% list entry={\thesection.\protect\numberline{\thetcbcounter}#2}, title={Pseudocode \thesection.\thetcbcounter\def@currentlabel{\thesection.\thetcbcounter}{\label{pseudocode:#3}}: #2}, listing options={tabsize=2,escapeinside=||,mathescape=true}, listing only, enhanced, #1 }

\newtcblisting[auto counter,list inside=mypseudocodes]{pseudocodeWText}[4][]{% list entry={\thesection.\protect\numberline{\thetcbcounter}#2}, title={Pseudocode \thesection.\thetcbcounter\def@currentlabel{Some Custom Text: #4}{\label{pseudocode:#3}}: #2}, listing options={tabsize=2,escapeinside=||,mathescape=true}, listing only, enhanced, #1 }

\makeatother

\tcblistof[\section*]{mypseudocodes}{List of Pseudocodes} \addcontentsline{toc}{chapter}{List of Pseudocodes}

\section{Test1} Pseudocode: \ref{pseudocode:code1} \ Pseudocode: \ref{pseudocode:code2} \

\section{Test2}

\section{T1}

\begin{pseudocode}{ My Title }{code1} a = 1; a = 2; \end{pseudocode}

\begin{pseudocodeWText}{ My Title }{code2}{Display Text} a = 1; a = 2; \end{pseudocodeWText}

\end{document}

enter image description here