\newtcbinputlisting 内的标签给出了错误的参考编号

\newtcbinputlisting 内的标签给出了错误的参考编号

在下面的代码中,我尝试引用使用环境编写的一些 matlab 代码\newtcbinputlisting。当我将标签附加到代码时,参考编号显示为“1”或“a)”或其他不同的参考编号,而不是所需的“Matlab 算法 1.1”。您能帮我获取所需的参考编号吗?谢谢。

以下是代码

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}

\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\usepackage[pdfencoding=auto]{hyperref}
\definecolor{light-gray}{gray}{0.92}
\definecolor{mygray}{RGB}{243,243,244}

\definecolor{mainColor}{RGB}{211, 47, 47}

\usepackage[numbered,framed]{matlab-prettifier}

\renewcommand\lstlistingname{Code}
\lstset{
    language=Python,
    numbers=left,
    numbersep= 7mm,
    numberstyle=\color{Black},
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    basicstyle=\color{Black}\ttfamily,
    commentstyle=\color{LimeGreen},
    keywordstyle=\color{BurntOrange}\bfseries,
    stringstyle=\color{WildStrawberry},
    keywords={var, func, extends},
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm,
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\usepackage[most]{tcolorbox}
\newcounter{matlab}

\newtcbinputlisting[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlabold}[4][]{%
  enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
  fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#3},%
  title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
  listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor,basicstyle=\ttfamily\small},list entry=#4,
  after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
  frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
  overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
  %
  listing file={#3},#1
}


\DeclareTCBInputListing[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}{O{}+m+O{#2}+m}{%
  enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
  fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#4},%
  title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
  listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor,basicstyle=\ttfamily\small},list entry=#3,
  after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
  frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
  overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
  %
  listing file={#4},#1
}


\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\newcommand{\matref}[1]{%
\hyperref[#1]{\textbf{\color{blue}{Matlab Algorithm \ref*{#1}}}}%
}

\begin{document}
\tcblistof{matlab}{List of codes}
\chapter{Some Code}
I am trying to reference the \matref{mat:code1} below

\inputmatlab{Expansion Algorithm\label{mat:code1}}[Other title]{sample.m}
\end{document} 

答案1

编号tcolorbox环境使用label=...选项来访问交叉引用功能,因此label=mat:code1在这里说可选参数\inputmatlab而不是\label{mat:code1}标题文本,它将使用由宏\@currentlabel设置的外部内容。\refstepcounter{chapter}\chapter

(当然我不推荐使用编号的标签名,因为这mat:code1不是一种好的标签风格)

\documentclass[a4paper, 10pt, oneside, fleqn, openright]{report}

\newcommand{\codeimg}{$\vcenter{\hbox{\includegraphics[height=\baselineskip]{example-image-a}}}$}

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{mdframed}
\usepackage{listings}
\usepackage{filecontents}
\usepackage[pdfencoding=auto]{hyperref}
\definecolor{light-gray}{gray}{0.92}
\definecolor{mygray}{RGB}{243,243,244}

\definecolor{mainColor}{RGB}{211, 47, 47}

\usepackage[numbered,framed]{matlab-prettifier}

\renewcommand\lstlistingname{Code}
\lstset{
    language=Python,
    numbers=left,
    numbersep= 7mm,
    numberstyle=\color{Black},
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    basicstyle=\color{Black}\ttfamily,
    commentstyle=\color{LimeGreen},
    keywordstyle=\color{BurntOrange}\bfseries,
    stringstyle=\color{WildStrawberry},
    keywords={var, func, extends},
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm,
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip
}

\usepackage[most]{tcolorbox}
\newcounter{matlab}



\DeclareTCBInputListing[use counter=matlab,list inside=matlab,number within=chapter]{\inputmatlab}{O{}+m+O{#2}+m}{%
  enhanced,noparskip,breakable,colback=light-gray,colframe=DarkSlateGray,opacitybacktitle=.8,%
  fonttitle=\bfseries,before upper={\hspace*{-1em}\codeimg~#4},%
  title after break={\centering\footnotesize\itshape\strut Matlab Code~\thematlab~--~continued},%
  listing only,listing options={xleftmargin=-1mm,#1,style=Matlab-editor,basicstyle=\ttfamily\small},list entry=#3,
  after upper={\centering\strut {\bfseries Matlab Code~\thematlab:}~#2},
  frame hidden,arc=0pt,outer arc=0pt,boxrule=0pt,
  overlay={\draw[gray,line width=1mm] ([xshift=-0.5pt]frame.north west) -- ([xshift=-0.5pt]frame.south west);},
  %
  listing file={#4},#1
}


\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\newcommand{\matref}[1]{%
\hyperref[#1]{\textbf{\color{blue}{Matlab Algorithm \ref*{#1}}}}%
}

\begin{document}
\tcblistof{matlab}{List of codes}
\chapter{Some Code}
I am trying to reference the \matref{mat:code1} below

\inputmatlab[label=mat:code1]{Expansion Algorithm}[Other title]{sample.m}
\end{document} 

在此处输入图片描述

相关内容