algpseudocode/algorithmicx 包不能与希伯来语 babel 包一起使用

algpseudocode/algorithmicx 包不能与希伯来语 babel 包一起使用

以下 LaTeX 文档:

\documentclass{article}

\usepackage{ucs}
\usepackage[utf8x]{inputenc}
\usepackage[hebrew, english]{babel}

\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother

\begin{document}
\selectlanguage{hebrew}{להלן אלגוריתם לדוגמא}\selectlanguage{english}
Here is an example algorithm:

\begin{algorithm}
\caption{My algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{MyProcedure}{}
\State $\textit{stringlen} \gets \text{length of }\textit{string}$
\State $i \gets \textit{patlen}$
\BState \emph{top}:
\If {$i > \textit{stringlen}$} \Return false
\EndIf
\State $j \gets \textit{patlen}$
\BState \emph{loop}:
\If {$\textit{string}(i) = \textit{path}(j)$}
\State $j \gets j-1$.
\State $i \gets i-1$.
\State \textbf{goto} \emph{loop}.
\State \textbf{close};
\EndIf
\State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
\State \textbf{goto} \emph{top}.
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document} 

产生以下错误:

 /usr/local/texlive/2020/texmf-dist/tex/latex/algorithmicx/algorithmicx.sty, line 636

Missing \endcsname inserted.

<to be read again> 
                   \protect 
l.636 \algnewlanguage{default}
                              %
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.

用 替换该行\usepackage[hebrew, english]{babel}可以\usepackage[english]{babel}清除错误,但希伯来语无法使用。

我该如何修复此问题?

答案1

这是一个用 LuaLaTEX 编译的工作示例:

\documentclass{article}

\usepackage[main=english, bidi=basic, layout=sectioning.tabular]{babel}
\usepackage{fontspec}
\babelprovide[import]{hebrew}

\babelfont{rm}
          [Ligatures={Common,Discretionary,TeX}]{Libertinus Serif} % Or any font that supports Hebrew.
\babelfont{sf}
          [Ligatures={Common,Discretionary,TeX}]{Libertinus Sans}
\babelfont{tt}
          [Ligatures=TeX]{Libertinus Mono}


\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother

\begin{document}
\selectlanguage{hebrew}{להלן אלגוריתם לדוגמא}\selectlanguage{english}

Here is an example algorithm

\begin{algorithm}
\caption{My algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{MyProcedure}{}
\State $\textit{stringlen} \gets \text{length of }\textit{string}$
\State $i \gets \textit{patlen}$
\BState \emph{top}:
\If {$i > \textit{stringlen}$} \Return false
\EndIf
\State $j \gets \textit{patlen}$
\BState \emph{loop}:
\If {$\textit{string}(i) = \textit{path}(j)$}
\State $j \gets j-1$.
\State $i \gets i-1$.
\State \textbf{goto} \emph{loop}.
\State \textbf{close};
\EndIf
\State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
\State \textbf{goto} \emph{top}.
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document} 

感谢@ulrike-fischer 的帮助

相关内容