此代码是从解决方案发布在这里。
我尝试放置一些超链接以链接回特定列表,但由于超链接未生成正确的列表编号,因此链接显示不正确。您能告诉我在这种情况下如何让超链接正常工作吗?谢谢!
以下是代码:
\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}
答案1
在我看来,存在多个错误,但最大的错误\usepackage{hyperref}
几乎就在一开始。
它应该在语句之后移动\numberwithin
,以便hyperref
了解重置功能并可以通过\theH....
宏形成正确的超锚点。
现在,另一个错误是\label{...}
标题等的内部。它将使用列表行号,但这很可能不是想要的,所以它应该引用列表计数器,即它必须放置label=...
在底层tcolorbox
宏或环境的第一个可选参数中。
然而,唉(;-))#1
在框定义选项中从未使用过。我也添加了这个。
\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}