0

Linegoal calculates the wrong remaining line width when using the book or scrbook document classes. This problem does not happen with article etc.

MWE:

\documentclass{book}
\usepackage{linegoal}

\begin{document} \overfullrule=5pt % To show the overfull lines

some line \parbox[t]{\linegoal}{some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content}

\clearpage

some different line \parbox[t]{\linegoal}{some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content some parbox content}

\end{document}

Result on page 1: enter image description here

Page 2 is fine: enter image description here

I think I narrowed it down to occur when there are different \evensidemargin/\oddsidemargin.

1 Answers1

0

OK, I figured it out after sinking way too much time into this...

tl;dr Fix

Add this to your preamble (after loading linegoal):

\makeatletter
\let\Z@E@linegoal\relax % undefine the property
\zref@newprop*{linegoal}[\linewidth]{\dimexpr
   \linewidth -\the\pdflastxpos sp
   +\ifodd\c@page
      \oddsidemargin
   \else\evensidemargin
   \fi
   +1in+\hoffset
   \relax
}% linegoal zref-property
\makeatother

Note that this may lead to an endless loop of changed labels if a page break depends on a \linegoal and the \linegoal itself is moved across pages due to this. In such cases, you may have to adjust the page breaks manually.

Background

Basically, linegoal uses an internal counter \LNGL@unique that is increased by one at every \linegoal occurrence. The current value of this counter is used to create labels (in the aux file) that look like this:

\zref@newlabel{linegoal/posx.3}{\page{35}\posx{19599809}}
\zref@newlabel{linegoal.3}{\linegoal{\dimexpr \linewidth -19599809sp +\evensidemargin +1in+\hoffset \relax }}

In this example, the value of \LNGL@unique was 3.

The example also already hints at the problem: Page 35 is an odd page, yet we see an \evensidemargin. However, the linegoal package doesn't just assume \evensidemargin everywhere; it checks whether the page number is even or odd and uses the corresponding margin:

   \ifodd\zref@extractdefault{linegoal/posx.\the\LNGL@unique}{page}\c@page
      \oddsidemargin
   \else\evensidemargin
   \fi

... to get the number of the page on which a \linegoal is located, linegoal uses \zref@extractdefault{linegoal/posx.\the\LNGL@unique}{page}\c@page, i.e., if there is a label linegoal/posx.<counter value> in the aux file, then it extracts the page from there, otherwise it use the current page counter value (\c@page).

However, for some reason, the current value of \LNGL@unique is almost always wrong,¹ and hence, the returned page number is wrong (from the second LaTeX run onwards).

¹: You can try for yourself by editing a copy of linegoal.sty and, e.g., replace the code snippet shown above by

   \ifodd\zref@extractdefault{linegoal/posx.\the\LNGL@unique}{page}\c@page
      \oddsidemargin + \the\LNGL@unique pt
   \else\evensidemargin + \the\LNGL@unique pt
   \fi

then look at the produced labels in the aux file.

The Fix

Instead of relying on \zref@extractdefault (and thus the apparently wrong \LNGL@unique value), the proposed fix always uses the page counter.

  • Thanks for the feedback! I guess for my use-case (putting long comments within algorithmic environments that almost always live on their own page into \parboxes) that wasn't a problem (I have several tens of pages of such algorithms without any issues). I guess I'll have to dig deeper then.... – bitstreichler Feb 09 '24 at 13:16
  • I'm not saying you're wrong, but for me, it does work without any issues. Since my patch does not modify how linegoal creates labels, I can verify the correct selection of odd/evensidemargin by looking at my aux file after a couple of LaTeX runs and compare the selected offset with the corresponding page reference (due to the high number of such labels in my document, I checked this with a small python script). – bitstreichler Feb 12 '24 at 11:42
  • actually I may be wrong, I'll delete the comments as I have no time to check now. the first comment is true that c@page can't be tested in the page but I think I missed you are testing this within the zref label so (I think) it's already delayed to shipout, I'd have to check what zref is doing exactly – David Carlisle Feb 12 '24 at 12:02