LaTeX-Package-Repositories / geometry

Updating Hideo Umeki's geometry package for LaTeX page dimensions
38 stars 5 forks source link

Wrong margin side with rtl page direction in lualatex #20

Open Udi-Fogiel opened 2 months ago

Udi-Fogiel commented 2 months ago

I don't think this dependency is correct, left should mean left, with no regard to other parameters.

\documentclass{article}
\usepackage[right=10cm]{geometry}
\pagedirection=1\bodydirection=1
\begin{document}
Hello

\newpage
\pagedirection=0\bodydirection=0
Hello
\end{document}
Udi-Fogiel commented 1 month ago

For compatibility with existing code, maybe adding new options, trueright, trueleft etc., that would mean right and left regardless of \pagedirection or other parameters would be better.

u-fischer commented 1 month ago

my knowledge about bidi and directions is rather scarce, but imho geometry can't do anything here. It basically only sets \oddsidemargin, the kernel code uses then \moveright to move the page, and if you change the page direction \moveright moves to the left and so things go wrong.

Which lead to the question why are you changing \pagedir and \bodydir at all? What is the intended effect?

Udi-Fogiel commented 1 month ago

my knowledge about bidi and directions is rather scarce, but imho geometry can't do anything here. It basically only sets \oddsidemargin, the kernel code uses then \moveright to move the page, and if you change the page direction \moveright moves to the left and so things go wrong.

Which lead to the question why are you changing \pagedir and \bodydir at all? What is the intended effect?

Just a bad example. What I'm looking for is a concise syntax that will work in all engines and with all values of \pagedirection. For example, if I would like a 10cm right margin, then in the usual case

\documentclass{article}
\usepackage[right=10cm]{geometry}
\begin{document}
Hello
\end{document}

would simply work, but the following will work only in XeLaTeX and pdfLaTeX , while with LuaLaTeX I'll need to replace right with left.

\documentclass{article}
\usepackage[right=10cm]{geometry}
\usepackage[hebrew,provide=*,bidi=default]{babel}
\begin{document}
Hello
\end{document}

So, to determine what options should I use, I'll need to test for the engine, and the main language being used, and I find it a bit cumbersome, especially when sometimes I get remarks from members of the team about portability of code between engines :)

Maybe the geometry package can determine the value of \pagedirection at the beginning of the document and decide upon the value of \oddsidemargin from that? and again, probably a new option will be better to keep everyone happy...

BTW, I don't think this is not really relevant, but maybe will spark different ideas. LuaTeX does provide four new registers like \hoffset which are used according to \pagedirection's value, so simply setting them will theoretically work, but LaTeX does not use \hoffset in the first place... For an example, OpTeX set \pagerightoffset after calculating \hoffset https://github.com/olsak/OpTeX/blob/86bbcfd3e7f6aaab3a5029668b92df852ff2224a/optex/base/margins.opm#L96-L98

u-fischer commented 4 weeks ago

while with LuaLaTeX I'll need to replace right with left.

Well yes, because babel changes pagedirection. So we are back to the question why this is done.

Maybe the geometry package can determine the value of \pagedirection at the beginning of the document

That wouldn't work for documents not using geometry but setting \oddsidemargin with other means.

If the \moveright in the output routine is wrong, then probably the kernel should change and not a package like geometry try to work around the problem. But to decide what really must be done, one needs some documentation what the direction commands do and how they are meant to be used. The luatex documentation only points back to some other model without explaining anything at all and I don't know where the original documentation is.

u-fischer commented 3 weeks ago

I made a few tests in plain. And what I gather is the following (I'm not sure if the ascii art really comes through ...)

By default TeX places the top and left of the page box at the 1in/1in point. So it spills to the right and bottom:

    1in
1in+----------------
     |  text
     |    
     |

If you set pagedirection to 1, the page box is placed at the \pagerightoffset point which is normally 1in from the right of the page.

                          1in
paperwidth - 1in           +----------------
                            |  text
                            |    
                            |

It then normally would go out of the page as given by the mediabox. If you use \moveright on the box then it moves to right and so even more out of the view.

If you set \bodydirection to 1, then the page box is placed with its right side on the reference point and so spills out of the page to the left:

                     1in
-----------1in+   
          text      |  
                      |    
                      |

Additionally the \moveright changes it meaning and moves to the left.

If you set both to 1, then the page is set on the right reference point and spills to left and so is correctly placed on the page:

                                       1in
---------paperwidth - 1in+
                        text          |  
                                        |    
                                        |

but again \moveright has changed its meaning and moves to the left and so breaks LaTeX expectations how the box behaves on the page.

As you want "left margin" to mean "left margin" imho the correct way to handle this not to set these directions at all. It looks wrong to change all reference points and then to add lots of complicated calculations to get the old behaviour back (and I ignored the additional problem of differences between the TeX page dimensions and the PDF page dimensions/mediabox anyway).

Searching a bit around I found that the main reason that \pagedirection is changed seems to be that this has a side effect on \parshape and so changes also list layouts etc (e.g. https://tex.stackexchange.com/a/717219/2388). I would suggest to investigate if one can avoid to touch to the page directions by setting \shapemode instead. According to the documentation one can set it for more than one paragraph by using negative numbers.