我尝试使用 minted 来排版代码
\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}
我可以在源文件中\texthebrew{בְּרֵאשִׁ֖ית}
进行更改\texthebrew{בְּרֵאשִׁ֖ית{
,但这不是一个好的做法。
这只发生在 xelatex 上,而不是 lualatex 上。使用 LuaLaTeX 时,希伯来语文本的顺序是错误的。
答案1
第二次更新:您会采取一个babel
解决lualatex
方案吗?
至少不需要丑陋的黑客......
\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}
首次更新不满意的答案:
括号问题至少部分与字体有关,但也涉及与颜色的相互作用。
例如,考虑一下:
\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}
现在为您的示例提供一些其他的解决方法:
\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}
原始答案:
我认为您无法避免转义命令(minted
有一个escapeinside
选项),因为无论如何您都必须将文本方向设置为 RTL。如果您尝试输入多个希伯来语单词,就会看到这一点。
而且我无法避免弄虚作假,因为在 minted 中它们的颜色意味着只有一个会自动镜像。也许比我聪明的人可以解决这个问题。
但是有没有更容易使用的软糖呢?
\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}