6

I do my homework in Latex, and I recently learned about the witharrows package which allows me to draw arrows between lines explaining what I did there. I find this very helpful for my homework assignments. However, there is a weird problem when I try to use the package. I do my homework in our native language, Hebrew, an RTL language. I use the polyglossia and fontspec packages to configure the language settings and fonts of the document.

When I tried to use the witharrows package with this simple example:

דוגמה לחישוב:
\begin{DispWithArrows*}
A &= (a + b)^2 \Arrow{נפתח את הסוגריים בביטוי} \\
&= a^2 + 2ab + b^2
\end{DispWithArrows*}

I got this output:

As you can see, the text next to the arrow is reversed. It looks like it's trying to write it in an LTR setting for some reason. After reading this question, I tried to use the option font=\selectlanguage{hebrew} like so:

\tikzset{
    WithArrows/arrow/.append style = {
        font = \selectlanguage{hebrew},
    }
}

However, it didn't work. I got the same output. Note that I did it in tikzset so it applies to all arrows, but doing it inside the arrow itself: \Arrow[tikz={font={\selectlanguage{hebrew}}}]{נפתח את הסוגריים בביטוי} doesn't work either.

I did some more experiments and I found something very weird: If I add another setting to the font such as changing the color:

\tikzset{
    WithArrows/arrow/.append style = {
        color=blue,
        font = \selectlanguage{hebrew},
    }
}

It suddenly works and renders correctly from right to left, and that's extremely weird. Another solution that I found to work is using \texthebrew:

דוגמה לחישוב:
\begin{DispWithArrows*}
A &= (a + b)^2 \Arrow{\texthebrew{נפתח את הסוגריים בביטוי}} \\
&= a^2 + 2ab + b^2
\end{DispWithArrows*}

which also renders correctly. The first solution is just weird and I'd like to know why adding the color option suddenly fixes it (of course I can use color=black so the color doesn't change but this feels like a dirty hack and not a real solution), and while the second solution makes sense, I don't want to always have to surround the text in my arrows with a \texthebrew command and I couldn't find a way to apply it globally.

Does anyone know how I can fix these issues and get Hebrew text to render correctly in arrows?

Full example code (Note I use the font Assistant):

% !TeX spellcheck = he_HE
\documentclass{article}

\usepackage{witharrows}

% Font and language settings
\usepackage[no-math]{fontspec}
\usepackage{polyglossia}

\newfontfamily\hebrewfont[
    Script=Hebrew,
    ItalicFont=*-Regular,
    ItalicFeatures={FakeSlant=0.3},
]{Assistant}

\setmainlanguage{hebrew}
\setotherlanguage{english}
\setsansfont{Assistant}
\setmonofont{Assistant}

\WithArrowsOptions{displaystyle}
\tikzset{
    WithArrows/arrow/.append style = {
        font = \selectlanguage{hebrew},
    }
}

\begin{document}
דוגמה לחישוב:
\begin{DispWithArrows*}
A &= (a + b)^2 \Arrow{נפתח את הסוגריים בביטוי} \\
&= a^2 + 2ab + b^2
\end{DispWithArrows*}
\end{document}
7
  • Is this correct?
    – Niranjan
    Commented Apr 21, 2023 at 14:10
  • Do you absolutely need the no-math option for fontspec? Because if not you could drop \usepackage{fontspec} as it is already loaded by polyglossia. Your MWE compiles without \usepackage[no-math]{fontspec}.
    – Niranjan
    Commented Apr 21, 2023 at 14:16
  • If the image I have attached is correct, then I am guessing the culprit is the compiler (XeLaTeX). With up-to-date LaTeX installation and LuaLaTeX I was able to produce that output with the exact same code.
    – Niranjan
    Commented Apr 21, 2023 at 14:36
  • @Niranjan Yes, the picture you posted is correct. I am quite sure I need the no-math option. It fixed a different issue I had a month ago. I don't even remember what it was.
    – Shai Avr
    Commented Apr 21, 2023 at 14:38
  • 1
    @ShaiAvr Ah, okay. If the image is correct and you don't want to change a lot of things, just shift to LuaLaTeX. That would be the easiest. There are other advantages of LuaLaTeX too.
    – Niranjan
    Commented Apr 21, 2023 at 14:40

1 Answer 1

7

That problem is not specific to witharrows. It's a problem of the PGF/Tikz nodes (in conjunction with polyglossia and xelatex).

I think that the best solution is to put a command \tikzset{text=.} in the preamble of the document ("." will be the current color at the time of creation of the Tikz node).

\documentclass{article}

\usepackage{witharrows}

% Font and language settings
\usepackage[no-math]{fontspec}
\usepackage{polyglossia}

\newfontfamily\hebrewfont[
    Script=Hebrew,
    ItalicFont=*-Regular,
    ItalicFeatures={FakeSlant=0.3},
]{Assistant}

\setmainlanguage{hebrew}
\setotherlanguage{english}
\setsansfont{Assistant}
\setmonofont{Assistant}


\tikzset{text=.}  % "." is the current color

\WithArrowsOptions{displaystyle,tikz={font=}}

\begin{document}


דוגמה לחישוב:
\begin{DispWithArrows*}
A &= (a + b)^2 \Arrow{נפתח את הסוגריים בביטוי} \\
&= a^2 + 2ab + b^2
\end{DispWithArrows*}
\end{document}

Output of the above code

I think that the origin of the problem is that PGF uses \hbox to construct the boxes containing the contents of its nodes (at least when text width is not used). That command \hbox, which is a primitive of TeX, is not ''color-safe'' (whereas the commands of constructions of boxes of LaTeX, such as \mbox are all ''color-safe''). With a box which is not ``color-safe'', the commands to pop the color from the stack are in fact after the box (inserted with a \aftergroup) and may have bad consequences.

By putting \tikzset{text=.} in the preamble, instructions to put color are inserted at each PGF node, and the protection added (to avoid color links) has probably an action to prevent also problems with RTL mode (I assume the command \hbox is not ''RTL-safe'').

2
  • That doesn't explain why it works without any hacks with lualatex. I'd appreciate it if you could explain this.
    – Shai Avr
    Commented Apr 22, 2023 at 21:05
  • The gestion of the fonts is not the same in lualatex and in xelatex. Both supports OpenType fonts but with different technics. Commented Apr 23, 2023 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .