4

A demonstration of the problem via a minimal working example

I saved the following LaTeX code in ~/Test.tex.

\documentclass{article}
\usepackage[bidi=basic,hebrew,provide=*,layout=graphics]{babel}
\babelfont{rm}[Renderer=HarfBuzz]{FreeSans}
\usepackage{marginnote}
\usepackage{xcolor}
\begin{document}
מכל אלה שהתהלכתי אתם פעם בעיר מולדתי יקרה לי ביחוד%
\marginnote{\textcolor{blue}{%
שלום רב שובך, ציפורה נחמדת, מארצות החום אל חלוני.%
}} %LTR
בת שכננו מינה, אשר קראו לה גם בשם הגנאי ה’ברודה',%
\marginnote{\textcolor{red}{%
אל קולך כי ערב מה-נפשי כלתה בחורף בעוזבך מעוני.
}}% LTR
והיא נערה מנומרת בכתמי שמש שלא חוננה אמנם ביופי, אבל הן כבר ידוע המשפט של כלי החרס אשר בהם ישומר היין הטוב, וראינו גם אשר דברי אלהים חיים נכתבים על גויל פשוט.
\end{document}

The code creates a document of class article, sets its main (and only) language to Hebrew, and, in the document's body, writes a paragraph of Hebrew text, in which two marginnotes of Hebrew text are embedded in two locations. The text of the first marginnote is colored blue, whereas that of the second one is colored red.

Before compiling the code, I took preemptive steps to avoid a known issue concerning marginnotes in Hebrew documents, by implementing a variation on David Carlisle's hack: I made a local copy of marginnote.sty (originally in /usr/local/texlive/2022/texmf-dist/tex/latex/marginnote/), and replaced its line no. 363 with: \rlap{\kern-2.7\marginparwidth%.see footnote

I then executed the following commands in the Terminal.

> cd ~
> lualatex Test
> lualatex Test

The compilation completed successfully with no warnings in ~/Test.log; in particular, no warnings suggesting to rerun the compilation.

As a result of the compilation, the file ~/Test.pdf was created. When this file was opened in a PDF viewer, it displayed as follows. (I screenshot only the relevant part of the display.)

Two Hebrew margin notes in a Hebrew document

As can be seen, the first (blue) marginnote is positioned correctly in the margin, but the second (red) marginnote is superimposed on the main text.


Questions

  1. Why is the second margin note superimposed on the main text?
  2. What can I do to fix the position of the second (and any other) margin note, while preserving the now correct position of the first margin note?

Footnote

It was Udi Fogiel, in a comment to a previous post of mine now deleted (due to being a duplicate of this question), who made me aware of the hack, and suggested the variation necessary to adapt it to the babel+lualatex combo that I use.

3
  • It might be a problem with LuaTeX and bidi, with XeTeX and polyglossia i don't get that problem.
    – Udi Fogiel
    Dec 28, 2022 at 20:57
  • @UdiFogiel Good to know, but switching to XeTeX+Polyglossia is not an option for me.
    – Evan Aad
    Dec 28, 2022 at 20:59
  • 2
    rename the sty if you change it. Dec 28, 2022 at 21:50

3 Answers 3

6

This here seems to be the correct values. Attention! It doesn't handle reversemarginpar, and twoside hasn't been tested yet.

\documentclass{article}
\usepackage[bidi=basic,hebrew,provide=*,layout=graphics]{babel}
\babelfont{rm}[Renderer=HarfBuzz]{FreeSans}
\usepackage{marginnote}
\usepackage{xcolor,xpatch}
\makeatletter
\patchcmd\@mn@@@marginnote{\kern-\dimexpr\@mn@currxpos}{\kern\dimexpr\@mn@currxpos}{}{\fail}
\patchcmd\@mn@@@marginnote{\kern\oddsidemargin}{\kern-\oddsidemargin}{}{\fail}
\patchcmd\@mn@@@marginnote{\kern\oddsidemargin}{\kern-\oddsidemargin}{}{\fail}
\patchcmd\@mn@@@marginnote{\kern\evensidemargin}{\kern-\evensidemargin}{}{\fail}
\patchcmd\@mn@@@marginnote{\kern 1in}{\kern-1in}{}{\fail}
\patchcmd\@mn@@@marginnote{\kern\marginnotetextwidth}{}{}{\fail}
\makeatother


\begin{document}
מכל אלה שהתהלכתי אתם פעם בעיר מולדתי יקרה לי ביחוד%
\marginnote{\textcolor{blue}{%
שלום רב שובך, ציפורה נחמדת, מארצות החום אל חלוני.%
}} %LTR
בת שכננו מינה, אשר קראו לה גם בשם הגנאי ה’ברודה',%
\marginnote{\textcolor{red}{%
אל קולך כי ערב מה-נפשי כלתה בחורף בעוזבך מעוני.
}}% LTR
והיא נערה מנומרת בכתמי שמש שלא חוננה אמנם ביופי, אבל הן כבר ידוע המשפט של כלי החרס אשר בהם 
ישומר היין הטוב, וראינו גם אשר דברי אלהים חיים נכתבים על גויל פשוט. 
',%
\marginnote{\textcolor{green}{%
אל קולך כי ערב מה-נפשי כלתה בחורף בעוזבך מעוני.
}}% LTR
והיא נערה מנומרת בכתמי שמש שלא חוננה אמנם ביופי, אבל הן כבר ידוע המשפט של כלי החרס אשר בהם 
ישומר היין הטוב, וראינו גם אשר דברי אלהים חיים נכתבים על גויל פשוט. 
\end{document}

enter image description here

5
  • Is it necessary to load tikz and zref-savepos?
    – Udi Fogiel
    Dec 28, 2022 at 22:52
  • @UdiFogiel no, that are remains from the tests, I will edit. Dec 28, 2022 at 23:01
  • Thank you! Does your solution need to be used in conjunction with Udi Fogiel's suggestion to my previous post of making a local copy of marginnote.sty and changing its line no. 363? Or does your answer provide a complete solution to the two issues described both in the present question as well as in the previous one?
    – Evan Aad
    Dec 29, 2022 at 0:10
  • why don't you try it? Dec 29, 2022 at 0:21
  • It appears to be a complete solution to the two issues, rendering it unnecessary to make a local copy of marginnote.sty.
    – Evan Aad
    Dec 29, 2022 at 7:48
5

Interestingly, marginnote takes into account RTL texts, but this feature is activated only when bidi is loaded. Apparently, it can be cheated in the following way:

\documentclass{article}
\usepackage[bidi=basic,hebrew,provide=*,layout=graphics]{babel}
\babelfont{rm}[Renderer=HarfBuzz]{FreeSans}

\makeatletter
\newif\if@RTL  \@RTLtrue  % Pretend that `bidi` has been loaded 
\makeatother

\usepackage{marginnote}
\usepackage{xcolor}

\begin{document}
מכל אלה שהתהלכתי אתם פעם בעיר מולדתי יקרה לי ביחוד%
\marginnote{\textcolor{blue}{%
שלום רב שובך, ציפורה נחמדת, מארצות החום אל חלוני.%
}} %LTR
בת שכננו מינה, אשר קראו לה גם בשם הגנאי ה’ברודה',%
\marginnote{\textcolor{red}{%
אל קולך כי ערב מה-נפשי כלתה בחורף בעוזבך מעוני.
}}% LTR
והיא נערה מנומרת בכתמי שמש שלא חוננה אמנם ביופי, אבל הן כבר ידוע המשפט של כלי החרס אשר בהם ישומר היין הטוב, וראינו גם אשר דברי אלהים חיים נכתבים על גויל פשוט.
\end{document}
5
  • Thanks for the answer. However, when I compile your code twice with lualatex the resulting PDF looks like this. In particular, the margin notes are rendered inside the text area and overlap the main text.
    – Evan Aad
    Dec 29, 2022 at 16:22
  • 1
    Works for me. Maybe the versions are not the same. Dec 29, 2022 at 16:30
  • Sorry. I had a modified local copy of marginnote.sty in my testing folder, left over from a hack suggested to a previous post of mine (now deleted). I now removed the local copy, and the PDF file produced by your code looks as expected.
    – Evan Aad
    Dec 29, 2022 at 16:39
  • Is it safe to add your code to documents that don't use bidi? How about if they don't even use babel?
    – Evan Aad
    Dec 29, 2022 at 16:48
  • 1
    It's difficult to know what can happen when one is cheating, but in most cases these definitions should be harmless. Dec 29, 2022 at 16:54
2

Add the following piece of code after loading the marginnote package to (hopefully) correct marginnote's \@mn@if@RTL which is supposed to test whether the current text flow is RTL or not.

\usepackage{ifthen}
\makeatletter
\renewcommand*{\@mn@if@RTL}%
{%
   \ifthenelse%
   {\equal{\the\textdir}{\detokenize{TRT}}}%
   {\@firstoftwo}%
   {\@secondoftwo}%
}
\makeatother

The geometry package should also be loaded if there are margin notes in LTR text, I don't know why, but otherwise the horizontal alignment of the marginnotes is a little off.

This solution relies on the LuaTeX engine's register \textdir, so can be used only with LuaTeX.


An example with both text and math in document whose language is set to Hebrew via babel.

\documentclass[12pt]{article}

\usepackage[showframe]{geometry}

\usepackage[bidi=basic,hebrew,provide=*]{babel}
\babelfont{rm}{FreeSans}

\usepackage{lipsum}

\usepackage{marginnote}

% marginnote fix begin
\usepackage{ifthen}
\makeatletter
\renewcommand*{\@mn@if@RTL}%
{%
   \ifthenelse%
   {\equal{\the\textdir}{\detokenize{TRT}}}%
   {\@firstoftwo}%
   {\@secondoftwo}%
}
\makeatother
% marginnote fix end
\begin{document}
\lipsum[1-1][1-1] \marginnote{Margin note in text mode}
\lipsum[1-1][2-8]
\reversemarginpar
\[
x + y = z\marginnote{Margin note in math mode}
\]
\end{document}

Margin notes from text and math in a Hebrew document


The example of the original post:

\documentclass{article}
% \usepackage[pass]{geometry}
\usepackage[bidi=basic,hebrew,provide=*,layout=graphics]{babel}
\babelfont{rm}[Renderer=HarfBuzz]{FreeSans}
\usepackage{marginnote}
\usepackage{xcolor}

% marginnote fix begin
\usepackage{ifthen}
\makeatletter
\renewcommand*{\@mn@if@RTL}%
{%
   \ifthenelse%
   {\equal{\the\textdir}{\detokenize{TRT}}}%
   {\@firstoftwo}%
   {\@secondoftwo}%
}
\makeatother
% marginnote fix end
\begin{document}
מכל אלה שהתהלכתי אתם פעם בעיר מולדתי יקרה לי ביחוד%
\marginnote{\textcolor{blue}{%
שלום רב שובך, ציפורה נחמדת, מארצות החום אל חלוני.%
}} %LTR
בת שכננו מינה, אשר קראו לה גם בשם הגנאי ה’ברודה',%
\marginnote{\textcolor{red}{%
אל קולך כי ערב מה-נפשי כלתה בחורף בעוזבך מעוני.
}}% LTR
והיא נערה מנומרת בכתמי שמש שלא חוננה אמנם ביופי, אבל הן כבר ידוע המשפט של כלי החרס אשר בהם ישומר היין הטוב, וראינו גם אשר דברי אלהים חיים נכתבים על גויל פשוט.
\end{document}

Two overlapping margin notes

In this case the loading of the geometry package is optional, because the text is entirely RTL.


Acknowledgments

  1. Ulrike Fischer for telling me about the \textdir register.

  2. David Carlisle for showing me how to query said register.


Remarks

This is not a perfect solution. There are cases in which despite the implementation of this solution, the margin notes are misplaced, for instance the following example in which the document's left and right margins are asymetrical.

\documentclass{article}
\usepackage[hmarginratio=1:3,showframe]{geometry}

\usepackage[bidi=basic, english, hebrew, provide=*]{babel}
\babelfont{rm}{FreeSans}

\usepackage{marginnote}

\usepackage{ifthen}
\makeatletter
\renewcommand*{\@mn@if@RTL}%
{%
   \ifthenelse%
   {\equal{\the\textdir}{\detokenize{TRT}}}%
   {\@firstoftwo}%
   {\@secondoftwo}%
}
\makeatother
\usepackage{lipsum}
\begin{document}
\reversemarginpar
\[%LTR
x + y = z\marginnote{math note}
\]
\normalmarginpar
word\marginpar{text note}
\end{document}

Asymetrical margins

5
  • +1 Great solution! is there anyway doing that using conditionals from the latex kernel only and not loading ifthen?
    – Udi Fogiel
    Dec 31, 2022 at 13:47
  • Also, I've noticed that the output of the original \@mn@if@RTL is \expandafter\@firstoftwo, or \expandafter\@secondoftwo. There might be an important expansion order in some cases. Is there a reason i missed for why you dropped the \expandafter prefix? I'm not sure about what should be the correct implementation.
    – Udi Fogiel
    Dec 31, 2022 at 13:56
  • @UdiFogiel 1. "Great solution" - Thanks. 2. "Is there anyway doing that [...] not loading ifthen?" - I'm sure there's a way of doing that without loading ifthen, but I don't know how off the top of my head, and I don't feel like spending time exploring this at the moment.
    – Evan Aad
    Dec 31, 2022 at 15:48
  • @UdiFogiel 3. "I've noticed that the output of the original \@mn@if@RTL is \expandafter\@firstoftwo [...] Is there a reason i missed for why you dropped the \expandafter?" Yes, there's a reason you missed. The only way \@mn@if@RTL is used in the marginnote code is as follows: \@mn@if@RTL{\PackageInfo{marginnote}... When \@mn@if@RTL expands to, say, \expandafter\@firstoftwo, the \expandafter will expand the left brace { just following the token \@mn@if@RTL, which doesn't accomplish anything.
    – Evan Aad
    Dec 31, 2022 at 15:48
  • Thank you for the explenation
    – Udi Fogiel
    Dec 31, 2022 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.