更改标签显示值

更改标签显示值

我有一个自定义命令,可以使用 显示伪代码tcolorbox。该\newtcblisting命令创建一个自定义计数器来显示框。我试图在文档中的某个地方引用伪代码来显示此自定义计数器,但我只设法使用 显示章节计数器\ref

以下是示例代码:

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

有人知道为什么该命令\def\@currentlabel{}{}不起作用或者可以建议任何解决方法吗?

我有\def\@currentlabel{}{}以下建议:标记文本并在稍后引用

在此处输入图片描述

答案1

问题已通过添加\makeatletter之前解决\newtcblisting修复!归功于乌尔丽克·菲舍尔寻求评论中的解决方案。

下面的代码说明了解决方案:

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

在此处输入图片描述

相关内容