1

this code is building from the solution posted here.

I was attempting to put some hyperrefs to link back to the specific listings, however the links show up incorrectly as the hyperrefs are not generating the correct listing number. Can you tell me how to get the hyperrefs to work correctly in this case? Thanks!

Here is the code:

\documentclass[a4paper, 10pt]{book}

\usepackage{amsmath}
\usepackage{hyperref}

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{listings}
\usepackage{filecontents}
\usepackage[numbered,framed]{matlab-prettifier}

\definecolor{light-gray}{gray}{0.92}
\definecolor{myblueiii}{RGB}{199,234,253}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueiii}{RGB}{199,234,253}

\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}
\newtcbinputlisting[use counter=data]{\inputdata}[3][]{%
    title after break={\centering\footnotesize\itshape\strut Data~\thedata~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=weka},after upper={\centering\strut Data~\thedata:~#2},%
     listing file={#3}}
\newtcbinputlisting[use counter=result]{\inputresult}[3][]{%
enhanced,noparskip,breakable,colback=myblueiii,opacitybacktitle=.8,%
    title after break={\centering\footnotesize\itshape\strut Result~\theresult~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=mystyleresults},after upper={\centering\strut Result~\theresult:~#2},%
     listing file={#3}}

\lstdefinestyle{mystyleresults}{
    commentstyle=\color{codegreen},
    keywordstyle=\color{black},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{black},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=none,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=6
}

\lstdefinestyle{weka}{
    numbers=none,
    numbersep=5pt,
    numberstyle=\tiny\color{codegray},
    commentstyle=\color{codegreen},
    keywordstyle=\color{blue},
    keywordstyle={[2]\color{magenta}},
    stringstyle=\color{codepurple},
    basicstyle=\footnotesize,
    comment=[l]{\%},
    keywords={@relation,@attribute,@data},
    morekeywords=[2]{real,integer,numeric,string,date},
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}

\begin{filecontents}{result1.txt}
=== Run information ===

Correlation coefficient                  0.5941
Mean absolute error                      2.2173
\end{filecontents}

\begin{filecontents}{result2.txt}
% 9 attributes
% 188 instances

@relation FIT

@attribute NUMUORS real   % Number of unique operators
@attribute NUMUANDS real  % Number of unique operands
@attribute TOTOTORS real  % Total number of operators

\end{filecontents}

\numberwithin{data}{chapter}
\numberwithin{result}{chapter}

\newcommand{\dataref}[1]{%
\hyperref[#1]{\textbf{\color{mybluei}Data \ref{#1}}}%
}

\newcommand{\resultref}[1]{%
\hyperref[#1]{\textbf{\color{mybluei}Result \ref{#1}}}%
}

\begin{document}
\chapter{Data}
\section{Data1}
This is a reference for \dataref{data:D1} shown below.
\inputdata{Code Snippet\label{data:D1}}{result2.txt}

This is a reference for \dataref{data:D2} shown below.
\inputdata{Code Snippet\label{data:D2}}{result2.txt}

This is a reference for \dataref{result:R1} shown below.
\inputresult{Result from run 1\label{result:R1}}{result1.txt}
This is a reference for \resultref{result:R1} shown above.

\chapter{Analysis}
\section{Data44}
This is a reference for \dataref{data:D44} shown below.
\inputdata{Code Snippet\label{data:D44}}{result2.txt}
\end{document} 
  • I assume that you want to refer to the listing itself, not to listing line numbers? – user31729 Feb 12 '17 at 22:45
2

There are multiple errors, in my point of view, but the biggest one is the \usepackage{hyperref} almost right at the start.

It should be shifted after the \numberwithin statements such that hyperref is informed about the resetting feature and can form the right hyper anchors via \theH.... macros.

Now, another error is the \label{...} inside of the title etc. It will use listing line numbers, but this is most probably not wanted, so it should refer to the listing counter, i.e. it must be placed as label=... in the first optional argument of the underlying tcolorbox macro or environment.

However, alas (;-)) the #1 was never used in the options of the box definitions. I added this as well.

\documentclass[a4paper, 10pt]{book}

\usepackage{amsmath}

\usepackage{calc}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{pdfpages,graphicx}
\usepackage{listings}
\usepackage{filecontents}
\usepackage[numbered,framed]{matlab-prettifier}

\definecolor{light-gray}{gray}{0.92}
\definecolor{myblueiii}{RGB}{199,234,253}
\definecolor{mainColor}{RGB}{211, 47, 47} % some dark red
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueiii}{RGB}{199,234,253}

\usepackage{tcolorbox}
\newcounter{data}
\newcounter{result}
\newcounter{pythoncode}
\newcounter{matlab}
\tcbuselibrary{skins,breakable,listings}
\newtcbinputlisting[use counter=data]{\inputdata}[3][]{%
    title after break={\centering\footnotesize\itshape\strut Data~\thedata~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=weka},after upper={\centering\strut Data~\thedata:~#2},%
     listing file={#3},#1}
\newtcbinputlisting[use counter=result]{\inputresult}[3][]{%
enhanced,noparskip,breakable,colback=myblueiii,opacitybacktitle=.8,%
    title after break={\centering\footnotesize\itshape\strut Result~\theresult~--~continued},%
     listing only,listing options={xleftmargin=-1mm,#1,style=mystyleresults},after upper={\centering\strut Result~\theresult:~#2},%
     listing file={#3},#1}

\lstdefinestyle{mystyleresults}{
    commentstyle=\color{codegreen},
    keywordstyle=\color{black},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{black},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    numbers=none,
    numbersep=5pt,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=6
}

\lstdefinestyle{weka}{
    numbers=none,
    numbersep=5pt,
    numberstyle=\tiny\color{codegray},
    commentstyle=\color{codegreen},
    keywordstyle=\color{blue},
    keywordstyle={[2]\color{magenta}},
    stringstyle=\color{codepurple},
    basicstyle=\footnotesize,
    comment=[l]{\%},
    keywords={@relation,@attribute,@data},
    morekeywords=[2]{real,integer,numeric,string,date},
    breakatwhitespace=false,
    breaklines=true,
    captionpos=b,
    keepspaces=true,
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2
}

\begin{filecontents}{result1.txt}
=== Run information ===

Correlation coefficient                  0.5941
Mean absolute error                      2.2173
\end{filecontents}

\begin{filecontents}{result2.txt}
% 9 attributes
% 188 instances

@relation FIT

@attribute NUMUORS real   % Number of unique operators
@attribute NUMUANDS real  % Number of unique operands
@attribute TOTOTORS real  % Total number of operators

\end{filecontents}

\usepackage{hyperref}

\numberwithin{data}{chapter}
\numberwithin{result}{chapter}



\newcommand{\dataref}[1]{%
\hyperref[#1]{\textbf{\color{mybluei}Data \ref{#1}}}%
}

\newcommand{\resultref}[1]{%
\hyperref[#1]{\textbf{\color{mybluei}Result \ref{#1}}}%
}

\begin{document}
\chapter{Data}
\section{Data1}
This is a reference for \dataref{data:D1} shown below.
\inputdata[label={data:D1}]{Code Snippet}{result2.txt}

This is a reference for \dataref{data:D2} shown below.
\inputdata[label={data:D2}]{Code Snippet}{result2.txt}

This is a reference for \dataref{result:R1} shown below.
\inputresult[label={result:R1}]{Result from run 1}{result1.txt}
This is a reference for \resultref{result:R1} shown above.

\chapter{Analysis}
\section{Data44}
This is a reference for \dataref{data:D44} shown below.
\inputdata[label=data:D44]{Code Snippet}{result2.txt}
\end{document} 
| improve this answer | |

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.