参考列表环境标签未正确显示

参考列表环境标签未正确显示

在下面给出的代码中,我试图让对列表块的引用正确显示,在这种情况下,显示为“代码 1”,而不是当前的“代码 code:test”。谢谢。

以下是代码:

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

\usepackage{calc}
\usepackage{graphicx, booktabs}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}

\definecolor{myblueii}{RGB}{63,200,244}
\newcommand*{\mainColor}{myblueii}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{eso-pic}
\usepackage{listings}
\RequirePackage{mdframed}
\RequirePackage{hyperref}
\tikzset{every picture/.style={remember picture}}

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

\newcommand{\codeTitle}[2]{Code}
\newcommand{\codeTitleContent}{\hspace*{3mm}\begin{minipage}{.75cm}
        \includegraphics[width=\linewidth]{example-image-a}
    \end{minipage}\hspace*{1mm}\begin{minipage}{\textwidth-1.05cm}
    {\sffamily\Large \codeTitle}
\end{minipage}}

\BeforeBeginEnvironment{lstlisting}{
    \begin{mdframed}[
        linecolor=Gray,
        backgroundcolor=myblueii,
        skipabove=4mm,
        skipbelow=0mm,
        innertopmargin=2mm,
        innerbottommargin=0mm,
        innerleftmargin=0mm,
        innerrightmargin=10pt,
        leftmargin=0mm,
        rightline=false,
        topline=false,
        bottomline=false,
        linewidth=1mm
        ]
        \codeTitleContent
        \vspace*{-4mm}
    }
    \AfterEndEnvironment{lstlisting}{
    \end{mdframed}
}

\renewcommand\lstlistingname{Code}

\lstset{
    style = {Matlab-editor},
    numbers=left,
    numbersep= 7mm,
    numberstyle=\color{Gray!80}\scriptsize,
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm, 
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip,
    backgroundcolor=\color{myblueii},
    basicstyle=\ttfamily\scriptsize,
}

\newcommand{\matref}[1]{%
\hyperref[#1]{\color{\mainColor}{Code {#1}}}%
}

\begin{document}

\section{New Code}

\renewcommand{\codeTitle}{Matlab Code}
\begin{lstlisting}[caption=RangeBearingSensor]
V = diag([0.02, 0.5*pi/180].^2);
P0 = diag([0.005, 0.005, 0.001].^2);
W = diag([0.1, 1*pi/180].^2);
map = LandmarkMap(20);
veh = Bicycle('covar', V);
veh.add_driver( RandomPath(map.dim) );
sensor = RangeBearingSensor(veh, map, 'covar', W, 'angle', [-pi/2 pi/2], 'range', 4, 'animate');
\end{lstlisting}\label{code:test}


How come the reference here \matref{code:test} does not show up correctly?

\end{document} 

答案1

定义

\newcommand{\matref}[1]{%
\hyperref[#1]{\color{\mainColor}{Code {#1}}}%
}

不必要地复杂并且在逻辑上也是错误的。

此时显示的\hyperref[foo]{bar}标签虽然会有一个链接,但不会显示标签后面的引用。foobar

链接应该\ref与周围的组一起完成\color{...} \ref{},以防止颜色变化的泄漏。

另一个严重的逻辑错误:\label不应在之后使用,而应作为中的lstlisting可选值,否则标签会抓取环境之外的最后一个命令的值,在本例中为。label=code:test[...]\@currentlabel\refstepcounterlstlistings\refstepcounter{section}

另一个更好的可能性:使用cleveref仅适用于 matlab 代码的专门列表环境或应用于\crefname{listing}{code}{codes}所有\Crefname{listing}{Code}{Codes}列表。

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

\usepackage{calc}
\usepackage{graphicx, booktabs}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}

\definecolor{myblueii}{RGB}{63,200,244}
\newcommand*{\mainColor}{myblueii}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{eso-pic}
\usepackage{listings}
\usepackage{mdframed}
\usepackage{hyperref}
\tikzset{every picture/.style={remember picture}}

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

\newcommand{\codeTitle}[2]{Code}
\newcommand{\codeTitleContent}{\hspace*{3mm}\begin{minipage}{.75cm}
        \includegraphics[width=\linewidth]{example-image-a}
    \end{minipage}\hspace*{1mm}\begin{minipage}{\textwidth-1.05cm}
    {\sffamily\Large \codeTitle}
\end{minipage}}

\BeforeBeginEnvironment{lstlisting}{
    \begin{mdframed}[
        linecolor=Gray,
        backgroundcolor=myblueii,
        skipabove=4mm,
        skipbelow=0mm,
        innertopmargin=2mm,
        innerbottommargin=0mm,
        innerleftmargin=0mm,
        innerrightmargin=10pt,
        leftmargin=0mm,
        rightline=false,
        topline=false,
        bottomline=false,
        linewidth=1mm
        ]
        \codeTitleContent
        \vspace*{-4mm}
    }
    \AfterEndEnvironment{lstlisting}{
    \end{mdframed}
}

\renewcommand\lstlistingname{Code}

\lstset{
    style = {Matlab-editor},
    numbers=left,
    numbersep= 7mm,
    numberstyle=\color{Gray!80}\scriptsize,
    stepnumber=1,
    tabsize=3,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    frame=leftline,
    framesep=0mm,
    xleftmargin=3mm,
    framesep=2mm, 
    framerule=0mm,
    abovecaptionskip=5mm,
    aboveskip=\baselineskip,
    belowskip=\baselineskip,
    backgroundcolor=\color{myblueii},
    basicstyle=\ttfamily\scriptsize,
}

\newcommand{\matref}[1]{%
  %\hyperref[#1]{\color{\mainColor}{Code \ref*{#1}}}%
  {\color{\mainColor}{Code \ref{#1}}}
}

\begin{document}

\section{New Code}

\renewcommand{\codeTitle}{Matlab Code}
\begin{lstlisting}[caption=RangeBearingSensor,label=code:test]
V = diag([0.02, 0.5*pi/180].^2);
P0 = diag([0.005, 0.005, 0.001].^2);
W = diag([0.1, 1*pi/180].^2);
map = LandmarkMap(20);
veh = Bicycle('covar', V);
veh.add_driver( RandomPath(map.dim) );
sensor = RangeBearingSensor(veh, map, 'covar', W, 'angle', [-pi/2 pi/2], 'range', 4, 'animate');
\end{lstlisting}


How come the reference here \matref{code:test} does not show up correctly?

\end{document} 

在此处输入图片描述

相关内容