4

in the code given below, I am trying to get the reference to the listing block to show up correctly, in this case, show up as "Code 1" instead of "Code code:test" as it is currently doing. Thanks.

Here is the code:

\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} 
  • Why do you use a \hyperref there? – user31729 Dec 6 '17 at 18:27
3

The definition

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

is unnecessarily complicated and logically wrong as well.

While \hyperref[foo]{bar} will make a link to the label foo displaying bar then, it will not display the reference behind the label.

The link should be done with \ref and a surrounding group around \color{...} \ref{} in order to prevent the leaking of the color change.

Another severe logical error: \label should not used after lstlisting, but as optional value label=code:test in [...], otherwise the label grabs the \@currentlabel value of the last \refstepcounter command outside of the lstlistings environment, in this case the \refstepcounter{section}.

Another and better possibility: Use cleveref for a specialized listing environment that is applied for matlab codes only or apply \crefname{listing}{code}{codes} and \Crefname{listing}{Code}{Codes} for all listings.

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.