1

I noticed neither \pageref nor \vpageref gives me the correct page number when a particular listing starts on a new page and there also is an underfull vbox on the previous page. I think if I fill more text on the previous page just enough so that it is not underfull, I get the correct page number.

In the below MWE, the listing starts on page 5. But \pageref and vpageref give me page number 4.

Is there any workaround for this problem?

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

enter image description here

enter image description here

1 Answer 1

1

One wonders why this doesn't pop up more often. listings allows the page to break between the \refstepcounter and the caption. Try this after loading listings:

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

This moves the \allowbreak before the \refstepcounter. You should also report this to the maintainer.

1
  • Thanks a lot indeed! As suggested, I've reported this problem to the maintainer as well.
    – Pyi Soe
    Sep 2, 2022 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .