TransWikia.com

Hack for reversing page order after some point broken by recent versions of polyglossia

TeX - LaTeX Asked on July 10, 2021

In this question:

Reversing page order from a point onward

user @BrunoLeFloch offered a very useful hack for page reversal using the atbegshi package. Unfortunately, with recent versions of TeXLive, and when using both Hebrew and English, this triggers an error, and since the details are beyond my capabilities, I’d like to ask for help in understanding what’s wrong. Here’s a minimal(ish) example:

documentclass{article}

usepackage{atbegshi}

makeatletter
usepackage{polyglossia}
setmainlanguage{english}
setotherlanguage{french}

newififRP%
newboxRPbox%
setboxRPboxvbox{vskip1pt}%
AtBeginShipout{%
  ifRP
    AtBeginShipoutDiscard%
    globalsetboxRPboxvbox{unvboxRPbox
      boxAtBeginShipoutBoxkernc@page sp}%
  fi
}%
renewcommand{RPtrue}{% reverse page order
  clearpage
  ifRPRPfalsefi
  globalletifRPiftrue
}%

renewcommand{RPfalse}{% resume normal page order
  clearpage
  globalletifRPiffalse
  setboxRPboxvbox{unvboxRPbox
    defprotect{noexpandprotectnoexpand}%
    @whileswifdim0pt=lastskipfi
      {c@pagelastkernunkernshipoutlastbox}%
  }%
}%

makeatother

begin{document}

RPtrue
begin{french}
Fou
newpage
end{french}
Bar % Note: Originally, this question was posted without the extra text.
RPfalse
end{document}

The error message:

! Missing } inserted.
<inserted text> 
                }
l.48 end{document}

I should mention this does work with TeXLive 2019.


edit: Here are the .aux files with TL 2021:

relax 
selectlanguage *{english}
@writefile{toc}{selectlanguage *{english}}
@writefile{lof}{selectlanguage *{english}}
@writefile{lot}{selectlanguage *{english}}
bgroup 
@writefile{toc}{bgroup }
@writefile{lof}{bgroup }
@writefile{lot}{bgroup }
selectlanguage *{french}
@writefile{toc}{selectlanguage *{french}}
@writefile{lof}{selectlanguage *{french}}
@writefile{lot}{selectlanguage *{french}}
gdef @abspage@last{1}

and with 2019:

relax 
protect select@language {english}
@writefile{toc}{protect select@language {english}}
@writefile{lof}{protect select@language {english}}
@writefile{lot}{protect select@language {english}}
protect select@language {french}
@writefile{toc}{protect select@language {french}}
@writefile{lof}{protect select@language {french}}
@writefile{lot}{protect select@language {french}}

2 Answers

There is an internal (etoolbox-style) boolean (xpg@inbiditable) in polyglossia which you can use in your code to swap bgroup and egroup insertion to the aux (UPDATE: provide bool to account for comment below):

documentclass{article}

usepackage{atbegshi}

makeatletter
usepackage{polyglossia}
setmainlanguage{english}
setotherlanguage{french}

newififRP%
newboxRPbox%
setboxRPboxvbox{vskip1pt}%
AtBeginShipout{%
    ifRP
    AtBeginShipoutDiscard%
    globalsetboxRPboxvbox{unvboxRPbox
        boxAtBeginShipoutBoxkernc@page sp}%
    fi
}%
% This boolean is missing in older polyglossia versions
providebool{xpg@inbiditable}
renewcommand{RPtrue}{% reverse page order
    booltrue{xpg@inbiditable}
    clearpage
    ifRPRPfalsefi
    globalletifRPiftrue
}%

renewcommand{RPfalse}{% resume normal page order
    boolfalse{xpg@inbiditable}
    clearpage
    globalletifRPiffalse
    setboxRPboxvbox{unvboxRPbox
        defprotect{noexpandprotectnoexpand}%
        @whileswifdim0pt=lastskipfi
        {c@pagelastkernunkernshipoutlastbox}%
    }%
}%

makeatother

begin{document}
RPtrue
begin{french}
Fou
newpage
Bar
end{french}
RPfalse
end{document}

Correct answer by JSpitzm on July 10, 2021

This is a partial answer but it is the best I can do in reasonable time.

The otherlanguage environment (used by begin{french}...end{french}) inserts on the page a "whatsit" (write@auxout{bgroup} and write@auxout{egroup}) at the start and at the end of the environment. When the page is actually shipped out to the pdf file, the two "whatsit" make TeX write bgroup and egroup to the aux file, to make some selectlanguage settings be local inside the aux file. This serves to ensure the correct language rules are used for titles in the toc and so on.

Since we are reversing the order of pages, the bgroupselectlanguage{french}...egroup becomes egroup bgroupselectlanguage{french}..., which makes TeX complain about a bad nesting (bgroup is the same as { and egroup the same as }).

The simplest workaround is to use selectlanguage{french} ... selectlanguage{english} instead of the french environment, as I do below. However, in order for the language setting to be correct everywhere in the aux file, one needs to reselect the language often. I am not sure how to do that properly; one option seems to be to just put selectlanguage{french} just before every section title, but probably this is not enough.

documentclass{article}
usepackage{lipsum}
usepackage{atbegshi}

makeatletter
usepackage{polyglossia}
setmainlanguage{english}
setotherlanguage{french}

newififRP%
newboxRPbox%
setboxRPboxvbox{vskip1pt}%
AtBeginShipout{%
  ifRP
    AtBeginShipoutDiscard%
    globalsetboxRPboxvbox{unvboxRPbox
      boxAtBeginShipoutBoxkernc@page sp}%
  fi
}%
renewcommand{RPtrue}{% reverse page order
  clearpage
  ifRPRPfalsefi
  globalletifRPiftrue
}%

renewcommand{RPfalse}{% resume normal page order
  clearpage
  globalletifRPiffalse
  setboxRPboxvbox{unvboxRPbox
    defprotect{noexpandprotectnoexpand}%
    @whileswifdim0pt=lastskipfi
      {c@pagelastkernunkernshipoutlastbox}%
  }%
}%

makeatother

begin{document}

section{Start in English}
lipsum[1-10]

RPtrue
selectlanguage{french}
section{Some French}
lipsum[11-20]
Fou
newpage

selectlanguage{french} % needed just before any section/subsection and perhaps in figures etc.
section{More French}
lipsum[21-30]

selectlanguage{english}
section{English stuff}
lipsum[31-40]
RPfalse

selectlanguage{english} % needed otherwise the last selectlanguage (after reversal) was french
section{And finally some English}
lipsum[41-50]

end{document}

Other ways could be to only reverse pages with the same language, or on the contrary make sure that the language environments don't span any page break.

Answered by Bruno Le Floch on July 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP