为什么当 `lstlisting` 从新页面开始时 `\pageref` 会给出错误的页码?

为什么当 `lstlisting` 从新页面开始时 `\pageref` 会给出错误的页码?

我注意到,当特定列表从新页面开始并且前一页的垂直框未满时,nor 不会\pageref提供\vpageref正确的页码。我认为,如果我在前一页上填充更多文本,使其不至于未满,我就会得到正确的页码。

在下面的 MWE 中,列表从第 5 页开始。但是\pagerefvpageref给我第 4 页。

有没有什么办法可以解决这个问题?

\documentclass[twoside, openright]{book}

\usepackage[nospace]{varioref}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{blindtext}
\usepackage{xcolor}


\lstnewenvironment{lstSimple}[1][]{
    \lstset{
        #1,
        language     = Java,
        %frame = single,
        %floatplacement=tbp,
        captionpos=t,
        breaklines=true,
        tabsize=8,
        %basicstyle   = \linespread{1}\monospacefnt\addfontfeatures{Color=myParColor, Scale=0.8},
        commentstyle = \color{gray},
        keywordstyle = \color{darkgray}\textbf,
        %stringstyle  = \color{green!70!black},
        %stringstyle  = \color{red},
        columns      = fullflexible,
        numberstyle  = \scriptsize\sffamily\color{gray},
        showstringspaces = false,
        %frame=single,
        %framerule=0pt,
        %framextopmargin=1pt,
        %framexbottommargin=1pt,
        %framexleftmargin=1pt,
        %xleftmargin=4pt,
        framesep=6pt, % if 
        %xleftmargin=3pt, % to move a little to the left if background color is used
        linewidth={\dimexpr \linewidth-3pt},
        aboveskip=4pt,
        belowskip=1pt,
        belowcaptionskip=5pt,        
    }%
}{ }

\begin{document}

\chapter{Chapter 1}

\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext
When I tried vref, even if the referenced item is on the facing/or preceding page, I still get the result with the page number like "Figure 1 on page 23" instead of "Figure 1 on the facing/previous page". If the reference and the referenced item are on the same page, the result seems to work. I get just "Figure 1" without the page number if it's on the same page on which vref is used to refer to it.the result seems to work. I get just "Figure 1" without the page number if it's on the same page on which vref is used to refer to it.

\begin{lstSimple}[caption={[MeetKarel]{MeetKarel}}, label={lst:meet_karel}]
import stanford.karel.Karel;
public class MeetKarel extends Karel { 
        public void run() {
                move();
                move();
                move();
                pickBeeper();
                turnLeft();
                move();
                move();
                turnLeft();
                turnLeft();
                turnLeft();
                move();
                putBeeper();
                move();
        }        
}
\end{lstSimple}
\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext

Please see the listing on page \pageref{lst:meet_karel}

Please see the listing on page \vpageref{lst:meet_karel}

\end{document}

在此处输入图片描述

在此处输入图片描述

答案1

有人想知道为什么它不会更频繁地弹出。列表允许页面在\refstepcounter和标题之间中断。加载列表后尝试此操作:

\usepackage{etoolbox}
\makeatletter
\patchcmd\lst@MakeCaption{\ifx #1t\allowbreak \fi}{}{}{\fail}
\patchcmd\lst@MakeCaption{\ifx #1t}{\ifx #1t \allowbreak}{}{\fail}
\makeatletter

这会将 移到\allowbreak之前\refstepcounter。您还应该向维护者报告此事。

相关内容