LyX:无法使用 \usepackage{algpseudocode} 编译非英语语言

LyX:无法使用 \usepackage{algpseudocode} 编译非英语语言

我对 LyX 和 Latex 还很陌生。

我正在用希伯来语写一篇论文,我想添加一个英语算法。

因此,我将添加\usepackage{algpseudocode}序言并粘贴一些示例算法。当文档语言为英语时,一切都按预期运行,但当我将其更改为希伯来语时,它会产生大量错误。

这是源代码:

这有效:

% Preview source code

%% LyX 2.1.4 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\makeatother

\usepackage{babel}
\begin{document}
\begin{algorithmic}
\Procedure{CrossValidate}{$\Phi$}
\ForAll{$\phi \in {\Phi_1,\cdots,\Phi_{k-1},\Phi_k}$}
\State $model =$ Train($\Phi \setminus \phi$ )
\ForAll{$s \in \phi$ }
\State \Comment{hej}
\EndFor
\EndFor   
\EndProcedure
\end{algorithmic}
\end{document}

这不:

% Preview source code

%% LyX 2.1.4 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english,hebrew]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9,cp1255]{inputenc}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\makeatother

\usepackage{babel}
\begin{document}
\selectlanguage{english}%
\inputencoding{latin9}\begin{algorithmic}
\Procedure{CrossValidate}{$\Phi$}
\ForAll{$\phi \in {\Phi_1,\cdots,\Phi_{k-1},\Phi_k}$}
\State $model =$ Train($\Phi \setminus \phi$ )
\ForAll{$s \in \phi$ }
\State \Comment{hej}
\EndFor
\EndFor   
\EndProcedure
\end{algorithmic}\selectlanguage{hebrew}%

\end{document}

答案1

包装在不应该使用计数器的地方algorithmicx使用计数器的印刷版本(即\thefoo计数器),而不是计数器本身,例如在之间或在涉及的条件中。foo\csname ... \endcsname\ifnum

由于印度阿拉伯数字应从左到右打印,即使在从右到左的上下文中,babel-hebrew也会影响数字的打印方式,这就是问题的根源。

您可以本地复制该algorithmicx包的代码(使用不同的名称),并将某些地方更改thefoo\number\c@foo

您可以通过在后面babel-hebrew添加行(或在 LyX 的序言中,在序言后添加行if is loaded)来修补如何处理印度阿拉伯数字。它将修复构造问题,但条件仍然会失败。我认为没有聪明的方法可以解决这个问题,但幸运的是,在钩子上只使用一个有问题的条件。将钩子上的代码替换为以下内容\def\@arabic#1{\if@rl\@number{\@@arabic#1}\else\number#1\fi}\usepackage{babel}\AddToHook{package/babel/after}{\def\@arabic#1{\if@rl\@number{\@@arabic#1}\else\number#1\fi}}babel\csnamealgorithmicxenddocument

\AddToHook{enddocument}{%
\ifnum\number\c@ALG@storecount>0\relax
      \PackageError{algorithmicx}{Some stored algorithms are not restored!}{}%
   \fi
}

以下是如何修复你的 mwe

% !TeX TS-program = pdflatex
% Preview source code

%% LyX 2.1.4 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english,hebrew]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9,cp1255]{inputenc}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\AddToHook{package/babel/after}{\def\@arabic#1{\if@rl\@number{\@@arabic#1}\else\number#1\fi}}
\RemoveFromHook{enddocument}[algorithmicx]
\AddToHook{enddocument}{%
\ifnum\number\c@ALG@storecount>0\relax
      \PackageError{algorithmicx}{Some stored algorithms are not restored!}{}%
   \fi
}

\makeatother

\usepackage{babel}

\begin{document}
\selectlanguage{english}%
\inputencoding{latin9}\begin{algorithmic}
\Procedure{CrossValidate}{$\Phi$}
\ForAll{$\phi \in {\Phi_1,\cdots,\Phi_{k-1},\Phi_k}$}
\State $model =$ Train($\Phi \setminus \phi$ )
\ForAll{$s \in \phi$ }
\State \Comment{hej}
\EndFor
\EndFor   
\EndProcedure
\end{algorithmic}\selectlanguage{hebrew}%

\end{document}

在此处输入图片描述

相关内容