# The algpseudocode/algorithmicx package can't be used with the Hebrew babel package

The following LaTeX document:

\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}


Produces the following error:

 /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.


Replacing the line \usepackage[hebrew, english]{babel} with \usepackage[english]{babel} clears the error, but then Hebrew can't be used.

What can I do to fix this?

• switch to lualatex and use the new babel code for hebrew instead of the old ldf. See e.g. tex.stackexchange.com/a/569584/2388 Nov 9, 2020 at 9:26
• How do I do that without the entire document turning into an RTL document? I want to have only some segments of the document written in Hebrew. Nov 9, 2020 at 9:35
• You don't have to set hebrew as the main language and you can switch languages. See the documentation of babel or search the site for more example. Nov 9, 2020 at 9:39

## 1 Answer

Here's a working example compiled with 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}


Thanks @ulrike-fischer for helping

• don't use ucs and inputenc with lualatex. And put an empty line between the hebrew and the english, not a \\ . Nov 9, 2020 at 10:51
• That looks like a re-working of some of my examples. You need to add [english] as an option to \documentclass for main=english to work; this is working for you anyway because English is the default. Apr 9, 2021 at 15:28
• You can also, in LuaLaTeX, add the option \babelprovide[import, onchar=fonts ids]{hebrew}, and babel will automatically detect when you type in Hebrew and change the direction, font and hyphenation. Apr 9, 2021 at 15:30