# XeLaTeX, minted and hebrew

I try to use minted to typeset code

\documentclass{article}
\usepackage{fontspec}
\usepackage{minted}
\setmonofont{Cousine}%%https://www.fontsquirrel.com/fonts/cousine > font which have monospace hebrew characters
\begin{document}
\begin{minted}{latex}
\documentclass{article}
\usepackage{polyglossia}
\setmainlanguage{french}
\setotherlanguage{hebrew}
\usepackage{fontspec}
\setmainfont{Libertinus Serif}
\newfontfamily\hebrewfont[Scale=0.8,Script=Hebrew]{Ezra SIL}
\begin{document}
Le premier mot du livre de la Genèse est : \texthebrew{בְּרֵאשִׁ֖ית}.
\end{document}
\end{minted}
\end{document}


Unfortunatly, I get a reverse { in the ouput

I can change \texthebrew{בְּרֵאשִׁ֖ית} to \texthebrew{בְּרֵאשִׁ֖ית{ in the source file, but that is not a good practice.

That happens only with xelatex, not lualatex. With LuaLaTeX, the hebrew text is in the wrong order.

Second update: Would you take a babel and lualatex solution?

At least no ugly hacks are required…

\documentclass{article}
\usepackage[nil,bidi=basic]{babel}
\babelprovide[import=en-AU,main]{australian}
\babelprovide[import=he]{hebrew}
\babelfont[australian]{rm}{Latin Modern Roman}
\babelfont[hebrew]{rm}[Scale=0.8]{Ezra SIL}
\babelfont{tt}{Cousine}
\pagestyle{empty}
\usepackage{minted}
\begin{document}
\section*{Source}
\begin{minted}{latex}
\documentclass{article}
\usepackage[nil,bidi=basic]{babel}
\babelprovide[import=en-AU,main]{australian}
\babelprovide[import=he]{hebrew}
\babelfont[australian]{rm}{Latin Modern Roman}
\babelfont[hebrew]{rm}[Scale=0.8]{Ezra SIL}
\babelfont{tt}{Cousine}
\begin{document}
The first two words of Genesis are: \foreignlanguage{hebrew}{בראשית ברא}.
\end{document}
\end{minted}

\section*{Output}
The first two words of Genesis are: \foreignlanguage{hebrew}{בראשית ברא}.
\end{document}


The brace problem is at least partially font related, but also involves interplay with colour.

e.g., consider this:

\documentclass{article}
\TeXXeTstate=1
\usepackage{fontspec}
\usepackage{xcolor}
\pagestyle{empty}
\begin{document}
\setmainfont{Cousine}
\{בראשית ברא\}\par
\textcolor{red}{\{}בראשית ברא\textcolor{red}{\}}\par
English \{בראשית ברא\} English\par
English \textcolor{red}{\{}בראשית ברא\textcolor{red}{\}} English\par
\setmainfont{FreeMono}
\{בראשית ברא\}\par
\textcolor{red}{\{}בראשית ברא\textcolor{red}{\}}\par
English \{בראשית ברא\} English\par
English \textcolor{red}{\{}בראשית ברא\textcolor{red}{\}} English\par
\end{document}


And now for some other work around options for your example:

\documentclass{article}
\TeXXeTstate=1
\usepackage{fontspec}
\usepackage{minted}
\setmonofont{FreeMono}
\pagestyle{empty}
\begin{document}
\begin{minted}{latex}
% Hebrew word order is wrong and uses FreeMono
\begin{document}
The first two words of Genesis are: \texthebrew{בראשית ברא}.
\end{document}
\end{minted}
\begin{minted}[escapeinside=||]{latex}
% correct, but requires escapeinside and FreeMono
\begin{document}
The first two words of Genesis are: \texthebrew{|\beginR|בראשית ברא|\endR|}.
\end{document}
\end{minted}
\setmonofont{Cousine}[Script=Hebrew]
\def\PYGZob{\}}
\def\PYGZcb{\{}
\begin{minted}{latex}
% use Cousine, but Hebrew word order is wrong and requires an ugly hack
\begin{document}
The first two words of Genesis are: \texthebrew{בראשית ברא}.
\end{document}
\end{minted}
\end{document}


I think you can't avoid escaping commands (minted has an escapeinside option), because you have to set the text direction to RTL no matter what. You'll see this if you try and put in more than one Hebrew word.

And I couldn't avoid fudging the braces because something about how they get coloured in minted means only one gets automatically mirrored. Maybe someone smarter than me can figure that out.

But what about an easier to use fudge?

\documentclass{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage{xcolor}
\usepackage{minted}
\pagestyle{empty}
\setmainlanguage{english}
\setotherlanguage{hebrew}
\setmonofont{Courier New}
\newfontfamily\hebrewfont{Ezra SIL}[Scale=0.8,Script=Hebrew]
\newcommand{\texthebrewverb}[1]{%
\PYG{k}{\PYGZbs{}texthebrew}%
\texthebrew{\ttfamily\PYG{n+nb}{\PYGZcb{}}#1\PYG{n+nb}{\PYGZcb{}}}}
\begin{document}
\begin{minted}[escapeinside=||]{latex}
\documentclass{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{english}
\setotherlanguage{hebrew}
\newfontfamily\hebrewfont{Ezra SIL}[Scale=0.8,Script=Hebrew]
\begin{document}
The first two words of Genesis are: \texthebrew{בראשית ברא}.% wrong
The first two words of Genesis are: |\texthebrewverb{בראשית ברא}|. % right
\end{document}
\end{minted}
\end{document}


• The main problem, for me, is that the code is not more ready-to-use as  |\texthebrewverb{בראשית ברא}| contain |. So if, for example, I want to have colored code + execute it "on the fly", I can't – Maïeul Sep 8 '17 at 8:50
• :( You have to have \beginR in there somehow. Maybe minted can be trained to treat \texthebrew in a special way. – David Purton Sep 8 '17 at 10:28
• what is strange is the fact that is automatically reverse clsoing } – Maïeul Sep 8 '17 at 11:12
• The brace problem is somehow font related. It works with FreeMono. I'm not sure why, but one difference is that fonts that work explicitly support Latin script. By comparison Courier New only explicitly support Arabic script and Cousine only explicitly supports Hebrew script. (check with otfinfo -s) – David Purton Sep 12 '17 at 2:11