在 pmatrix 环境之前添加下标时编译 latexdiff 会导致编译错误

在 pmatrix 环境之前添加下标时编译 latexdiff 会导致编译错误

考虑以下两个简单.tex文件:

test_old.tex

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$a \begin{pmatrix}a & b\end{pmatrix}$

\end{document}

test_new.tex

\documentclass{article}
\usepackage{amsmath}

\begin{document}

$a_2 \begin{pmatrix}a & b\end{pmatrix}$

\end{document}

用来latexdiff突出显示这两个文档之间的差异(在 Windows 10 上使用 latexdiff 1.2.0)生成以下diff.tex文件:

\documentclass{article}
%DIF LATEXDIFF DIFFERENCE FILE
%DIF DEL .\test_old.tex   Tue Mar 21 15:42:30 2017
%DIF ADD .\test_new.tex   Tue Mar 21 15:42:31 2017
\usepackage{amsmath}
%DIF PREAMBLE EXTENSION ADDED BY LATEXDIFF
%DIF UNDERLINE PREAMBLE %DIF PREAMBLE
\RequirePackage[normalem]{ulem} %DIF PREAMBLE
\RequirePackage{color}\definecolor{RED}{rgb}{1,0,0}\definecolor{BLUE}{rgb}{0,0,1} %DIF PREAMBLE
\providecommand{\DIFadd}[1]{{\protect\color{blue}\uwave{#1}}} %DIF PREAMBLE
\providecommand{\DIFdel}[1]{{\protect\color{red}\sout{#1}}}                      %DIF PREAMBLE
%DIF SAFE PREAMBLE %DIF PREAMBLE
\providecommand{\DIFaddbegin}{} %DIF PREAMBLE
\providecommand{\DIFaddend}{} %DIF PREAMBLE
\providecommand{\DIFdelbegin}{} %DIF PREAMBLE
\providecommand{\DIFdelend}{} %DIF PREAMBLE
%DIF FLOATSAFE PREAMBLE %DIF PREAMBLE
\providecommand{\DIFaddFL}[1]{\DIFadd{#1}} %DIF PREAMBLE
\providecommand{\DIFdelFL}[1]{\DIFdel{#1}} %DIF PREAMBLE
\providecommand{\DIFaddbeginFL}{} %DIF PREAMBLE
\providecommand{\DIFaddendFL}{} %DIF PREAMBLE
\providecommand{\DIFdelbeginFL}{} %DIF PREAMBLE
\providecommand{\DIFdelendFL}{} %DIF PREAMBLE
%DIF END PREAMBLE EXTENSION ADDED BY LATEXDIFF

\begin{document}

%DIF <  $\mathcal C\begin{pmatrix}a & b\end{pmatrix}$
\DIFdelbegin \DIFdel{$a \begin{pmatrix}a & b\end{pmatrix}$
}\DIFdelend %DIF >  $\mathcal C_2 \begin{pmatrix}a & b\end{pmatrix}$
\DIFaddbegin \DIFadd{$a_2 \begin{pmatrix}a & b\end{pmatrix}$
}\DIFaddend 

\end{document}

编译此文件会导致以下错误:

...\diff.tex:30: Forbidden control sequence found while scanning use of \UL@word. [}]
...\diff.tex:30: Paragraph ended before \UL@word was complete. [}]
...\diff.tex:34: Missing } inserted. [\end{document}]

是什么原因造成的?我该如何预防?

答案1

失败的原因是,如果内联数学表达式具有数组环境,则 ulem 下划线命令会中断。可以通过使用封装数学表达式来实现这一点\mbox{$...$}(这必须在旧文件和新文件中或在生成的 diff 中完成)。由于这是 ulem latexdiff 包含的代码的一个已知弱点,可以在需要时将 \mbox 添加到其输出中。

但是 latexdiff 中有一个模式匹配错误,导致它无法正常工作。在撰写本文时,此错误已在 github 上的开发版本中修复,但尚未包含在发布版本中(它将从 1.2.1 开始包含)。如果您不想克隆 github 版本,您可以手动应用这两个补丁:

    $begin2=pos($delblock) - $len2;
    $mathblock="%\n\\mbox{$AUXCMD\n$1\n}$AUXCMD\n";
-   next unless $mathblock =~ m/\{$ARRENV\}/ ;
+   next unless ( $mathblock =~ /ARRAYBLOCK/ or $mathblock =~m/\{$ARRENV\};
    substr($delblock,$begin2,$len2)=$mathblock;
    pos($delblock) = $begin2 + length($mathblock);

@@ -2608,7 +2615,7 @@ sub postprocess {
    $len2=length($&);
    $begin2=pos($addblock) - $len2;
    $mathblock="%\n\\mbox{$AUXCMD\n$1\n}$AUXCMD\n";
-   next unless $mathblock =~ m/\{$ARRENV\}/ ;
+   next unless ( $mathblock =~ /ARRAYBLOCK/ or $mathblock =~ m/\{$ARRENV\}/) ;
    substr($addblock,$begin2,$len2)=$mathblock;
    pos($addblock) = $begin2 + length($mathblock);
   }

相关内容