我应该如何使用 setcounter{lstlisting}{0} 进行引用?

我应该如何使用 setcounter{lstlisting}{0} 进行引用?

我在lstlisting标题和标签中都指定了。此外,在每个部分之前我都使用:

\setcounter{lstlisting}{0}

我的 PDF 视图列表如下:清单 4.1.2 SomeCaption

当我使用\ref{lst:someKey}它时,它不能正常工作。我的意思是它链接到了其他列表,而不是它应该链接到的列表(在合适的位置之前)。例如 4.1.4 而不是 4.2.4。

有人能告诉我这有什么问题吗?这是我的代码副本:

\documentclass[a4paper,11pt]{report}
\usepackage{latexsym}
\usepackage{listings}
\usepackage{color}
\usepackage{accsupp}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{cite}
\hypersetup{
    colorlinks=true, 
    linktoc=all
}
\lstset{language=Java,
captionpos=b, 
basicstyle=\ttfamily\footnotesize, 
numbers=left,
aboveskip=20pt,
belowskip=20pt,
xleftmargin=40pt}
\author{Marcin}
\title{Latex problem}
\frenchspacing
\begin{document}
\maketitle
\tableofcontents
\renewcommand{\thelstlisting}{\thesection.\arabic{lstlisting}}
\chapter{Chap First}
\section{Section first}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur congue faucibus scelerisque. Proin urna leo, tempor quis imperdiet quis, auctor nec purus. Donec ultricies ligula sapien, nec consequat enim suscipit quis. Nulla rhoncus felis dui, et volutpat enim suscipit varius. Morbi non sem felis. Fusce ullamcorper lacus nunc, et vehicula metus suscipit ut. Donec et iaculis mi, at mollis turpis. Fusce mattis pretium rhoncus. Aenean auctor urna id consectetur porttitor. Vivamus lectus elit, mollis eget tempus vitae, tempor a erat. Suspendisse sed felis non augue porttitor lobortis nec eget nunc. Aliquam id sollicitudin ante. Sed sodales lectus a augue porttitor, et iaculis nisi iaculis. Aliquam varius quis est non viverra. Aenean eget arcu eget sapien posuere rhoncus. 
\begin{lstlisting}[caption={Test1}, label={lst:test1}]
if(a==0){
    //body
}
\end{lstlisting}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur congue faucibus scelerisque. Proin urna leo, tempor quis imperdiet quis, auctor nec purus. Donec ultricies ligula sapien, nec consequat enim suscipit quis. Nulla rhoncus felis dui, et volutpat enim suscipit varius. Morbi non sem felis. Fusce ullamcorper lacus nunc, et vehicula metus suscipit ut. Donec et iaculis mi, at mollis turpis. Fusce mattis pretium rhoncus. Aenean auctor urna id consectetur porttitor. Vivamus lectus elit, mollis eget tempus vitae, tempor a erat. Suspendisse sed felis non augue porttitor lobortis nec eget nunc. Aliquam id sollicitudin ante. Sed sodales lectus a augue porttitor, et iaculis nisi iaculis. Aliquam varius quis est non viverra. Aenean eget arcu eget sapien posuere rhoncus. 
\begin{lstlisting}[caption={Test2}, label={lst:test2}]
if(a!=0){
    //body
}
\end{lstlisting}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur congue faucibus scelerisque. Proin urna leo, tempor quis imperdiet quis, auctor nec purus. Donec ultricies ligula sapien, nec consequat enim suscipit quis. Nulla rhoncus felis dui, et volutpat enim suscipit varius. Morbi non sem felis. Fusce ullamcorper lacus nunc, et vehicula metus suscipit ut. Donec et iaculis mi, at mollis turpis. Fusce mattis pretium rhoncus. Aenean auctor urna id consectetur porttitor. Vivamus lectus elit, mollis eget tempus vitae, tempor a erat. Suspendisse sed felis non augue porttitor lobortis nec eget nunc. Aliquam id sollicitudin ante. Sed sodales lectus a augue porttitor, et iaculis nisi iaculis. Aliquam varius quis est non viverra. Aenean eget arcu eget sapien posuere rhoncus. 
\begin{lstlisting}[caption={Test3}, label={lst:test3}]
if(a>0){
    //body
}
\end{lstlisting}

\setcounter{lstlisting}{0}
\section{Section second}
 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur congue faucibus scelerisque. Proin urna leo, tempor quis imperdiet quis, auctor nec purus. Donec ultricies ligula sapien, nec consequat enim suscipit quis. Nulla rhoncus felis dui, et volutpat enim suscipit varius. Morbi non sem felis. Fusce ullamcorper lacus nunc, et vehicula metus suscipit ut. Donec et iaculis mi, at mollis turpis. Fusce mattis pretium rhoncus. Aenean auctor urna id consectetur porttitor. Vivamus lectus elit, mollis eget tempus vitae, tempor a erat. Suspendisse sed felis non augue porttitor lobortis nec eget nunc. Aliquam id sollicitudin ante. Sed sodales lectus a augue porttitor, et iaculis nisi iaculis. Aliquam varius quis est non viverra. Aenean eget arcu eget sapien posuere rhoncus. 
\begin{lstlisting}[caption={Test4}, label={lst:test4}]
if(a==0){
    //body
}
\end{lstlisting}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur congue faucibus scelerisque. Proin urna leo, tempor quis imperdiet quis, auctor nec purus. Donec ultricies ligula sapien, nec consequat enim suscipit quis. Nulla rhoncus felis dui, et volutpat enim suscipit varius. Morbi non sem felis. Fusce ullamcorper lacus nunc, et vehicula metus suscipit ut. Donec et iaculis mi, at mollis turpis. Fusce mattis pretium rhoncus. Aenean auctor urna id consectetur porttitor. Vivamus lectus elit, mollis eget tempus vitae, tempor a erat. Suspendisse sed felis non augue porttitor lobortis nec eget nunc. Aliquam id sollicitudin ante. Sed sodales lectus a augue porttitor, et iaculis nisi iaculis. Aliquam varius quis est non viverra. Aenean eget arcu eget sapien posuere rhoncus. 
\begin{lstlisting}[caption={Test5}, label={lst:test5}]
if(a!=0){
    //body
}
\end{lstlisting}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur congue faucibus scelerisque. Proin urna leo, tempor quis imperdiet quis, auctor nec purus. Donec ultricies ligula sapien, nec consequat enim suscipit quis. Nulla rhoncus felis dui, et volutpat enim suscipit varius. Morbi non sem felis. Fusce ullamcorper lacus nunc, et vehicula metus suscipit ut. Donec et iaculis mi, at mollis turpis. Fusce mattis pretium rhoncus. Aenean auctor urna id consectetur porttitor. Vivamus lectus elit, mollis eget tempus vitae, tempor a erat. Suspendisse sed felis non augue porttitor lobortis nec eget nunc. Aliquam id sollicitudin ante. Sed sodales lectus a augue porttitor, et iaculis nisi iaculis. Aliquam varius quis est non viverra. Aenean eget arcu eget sapien posuere rhoncus. 
Sth interesting happend in listing \ref{lst:test5}

\begin{lstlisting}[caption={Test6}, label={lst:test6}]
if(a>0){
    //body
}
\end{lstlisting}


\end{document}

答案1

这是一个复制您的问题的最小示例(没有任何多余的部分):

\documentclass{article}
\usepackage{listings}
\usepackage{hyperref}
\AtBeginDocument{\renewcommand{\thelstlisting}{\thesection.\arabic{lstlisting}}}
\begin{document}
\section{Section first}
Reference to Test2 is Listing~\ref{lst:test2}.
\begin{lstlisting}[caption={Test1}, label={lst:test1}]
if(a==0){
    //body
}
\end{lstlisting}

\setcounter{lstlisting}{0}
\section{Section second}
Reference to Test1 is Listing~\ref{lst:test1}.
\begin{lstlisting}[caption={Test2}, label={lst:test2}]
if(a==0){
    //body
}
\end{lstlisting}

\end{document}

你会注意到在.log引用

pdfTeX warning (ext4): destination with the same 
  identifier (name{lstlisting.1}) has been already used, 
  duplicate ignored

这意味着超链接目标跳转具有重复的跳转标识符,因此跳转至错误位置。尽管如此,引用仍然正确。

通过做两件事可以解决不正确的超链接问题:

  1. 不要lstlisting在每一节开始时将计数器重置为零(IE,删除\setcounter{lstlisting}{0})。这很容易出错,并且还会造成混淆hyperref

  2. 添加到你的序言中

    \usepackage{chngcntr}% http://ctan.org/pkg/chngcntr
    \AtBeginDocument{\counterwithin{lstlisting}{section}}
    

    这样会lstlisting在每次出现新内容时重置计数器\section,并且还会\thesection在前面加上\thelstlisting(因此您甚至不必执行\AtBeginDocument{\thelstlisting}{\thesection.\arabic{lstlisting}})。

相关内容