latex3 / babel

The babel system for LaTeX, LuaLaTeX and XeLaTeX
LaTeX Project Public License v1.3c
124 stars 34 forks source link

Misplaced text with multicol, otherlanguage and RTL #228

Closed jbezos closed 1 year ago

jbezos commented 1 year ago

From https://tex.stackexchange.com/questions/680079/some-fonts-do-not-work-properly-with-multicols-and-babel.

The MWE (with a couple of minimal changes) is.

\documentclass{book}

\usepackage{multicol, lipsum}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{english}
\babelprovide[import]{arabic}

\babelfont[english]{rm}{EB Garamond}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

\begin{multicols}{2}
    \lipsum[1-6]

    \begin{otherlanguage}{arabic}
        لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
        نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
        يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
        السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
        الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
        ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.\par
    \end{otherlanguage}
\end{multicols}

\end{document}

It seems twocolumn is preventing babel from adjusting the correct \pagedir or \bodydir (not yet sure).

Workaround. Remove the par.

Note. The original example also included linebreaker. There is also some incompatibility with it, and I’m not sure this package works with RTL text.

hpvd commented 1 year ago

as shown starting from here https://github.com/michal-h21/linebreaker/issues/7#issuecomment-1469845271the false/broken alignments also occur, even

simply using basic \twocolumn

jbezos commented 1 year ago

@hpvd Thank you for the pointer, but please report babel bugs when you find them or mention me in the original issue so that I can join the conversation.

hpvd commented 1 year ago

....please report babel bugs when you find them or mention me in the original issue so that I can join the conversation.

sure, we weren't aware that's a babel issue yet. First we looked for linebreaker, then multicol, then the font and now babel :-)

jbezos commented 1 year ago

Or even a combination of all tree 🙂. There is clearly a bug in babel, but the misalignment of the last line is, I think, an issue in linebreaker. I must investigate, but I wonder if linebreaker makes any sense in unhyphenated languages, with different justification methods.

hpvd commented 1 year ago

When enhancing babel testing / compatibility even further, maybe it's a good idea not only take https://ctan.org/pkg/linebreaker into account, but also directly https://ctan.org/pkg/lua-widow-control

both

hpvd commented 1 year ago

@michal-h21 could locate and solve an issue in linebreaker package:

I've found the source of the issue with Linebreaker. It didn't pass the parameters table (which contained the info about pardir) to the line-breaking function when there was an overfull line in the paragraph. This is the reason why it was so random and why it happened with shorter line widths. The last update should fix that.

see: https://github.com/michal-h21/linebreaker/issues/7#issuecomment-1476246888

hpvd commented 1 year ago

just a simple example to reproduce the issue: (linebreaker disabled, so only lipsum and babel package and 2 fonts activ) lualatex, Miktex, windows

This is LuaHBTeX, Version 1.16.0 (MiKTeX 23.1)

=> you only have to change: \lipsum[1-4] to\lipsum[1-5] in the following code to make it visible:

with \lipsum[1-5] the first page looks empty, and if you select the (invisible) content in the pdf, it looks strange

\documentclass[12pt,a4paper]{book}

%\usepackage{linebreaker}
\usepackage{lipsum}
\usepackage[bidi = basic]{babel}

\babelprovide[main, import]{english}
\babelprovide[import]{arabic}

\babelfont[english]{rm}{EB Garamond}
\babelfont[arabic]{rm}{Amiri}

\begin{document}

\twocolumn
    \lipsum[1-4]

    \begin{otherlanguage}{arabic}
        لكن لا بد أن أوضح لك أن كل هذه الأفكار المغلوطة حول استنكار  النشوة وتمجيد الألم
        نشأت بالفعل، وسأعرض لك التفاصيل لتكتشف حقيقة وأساس تلك السعادة البشرية، فلا أحد
        يرفض أو يكره أو يتجنب الشعور بالسعادة، ولكن بفضل هؤلاء الأشخاص الذين لا يدركون بأن
        السعادة لا بد أن نستشعرها بصورة أكثر عقلانية ومنطقية فيعرضهم هذا لمواجهة الظروف
        الأليمة، وأكرر بأنه لا يوجد من يرغب في الحب ونيل المنال ويتلذذ بالآلام، الألم هو الألم
        ولكن نتيجة لظروف ما قد تكمن السعاده فيما نتحمله من كد وأسي.

    \end{otherlanguage}
\end{document}

2023-03-20_15h30_29

2023-03-20_15h29_03

hpvd commented 1 year ago

many thanks for this quick fix!

@amarakon confirmed everything works as expected now, see https://github.com/michal-h21/linebreaker/issues/7#issuecomment-1478555439

btw: would be great to release a new version of babel at ctan...

hpvd commented 1 year ago

@jbezos there seems to be a remaining issue please see https://github.com/michal-h21/linebreaker/issues/7#issuecomment-1478635488

jbezos commented 1 year ago

@hpvd I release a new version every 3 weeks, more or less, but when TeXLive is released I usually make a little pause. Very likely next week.