The problem I'm having is that page numbers, in a List of Listings, are frequently off by one page.
I'm using the listings
package and I have a List of Listings in the beginning of the document, generated by the command \listoflistings
.
I'm using the following environment for writing the listings.
\lstnewenvironment{javacode}[2]
{
\refstepcounter{listings}%
\label{lst:#2}%
\addcontentsline{lst}{listings}{\protect\numberline{\thelistings}#1}%
\lstset{language=Java,caption=#1,breaklines=true,breakatwhitespace=false,float=phtb}
}
{
}
The issue appears when there isn't enough space for the listing in a page and it is moved to the next one. In those cases, the page number that appears in the list of listing is off by one page, because the anchor remained in its place in the text, but the actual listing got moved to the next page.
Do you know how can I fix it? I guess a solution should exist since I just want to replicate the behavior of figures and tables, and List of Figures and List of Tables.
\refstepcounter
and\addcontentsline
commands?\lstnewenvironment{javacode}[2]{\lstset{language=Java,label=lst:#2,caption=#1,breaklines=true,breakatwhitespace=false,float=phtb}}{}
appears to work fine as-is, and may remove your problem.\addcontentsline
was for adding it automatically to the list. I was using a\listoflistings
, based on the toclof package, instead of the\lstlistoflistings' that package
listingsp provides. Right now I'm incapable of remembering why I was doing that. Thanks for your help.