ftilmann / latexdiff

Compares two latex files and marks up significant differences between them. Releases on www.ctan.org and mirrors
GNU General Public License v3.0
518 stars 73 forks source link

Misplaced alignment tab character & when equations contain split/align #258

Closed MBradbury closed 2 years ago

MBradbury commented 2 years ago

Running latexdiff when changing two equations each with a split in it to a single align with both the splits in it leads to a pdflatex compile error. Removing the \DIFadd{ } around the split environment allows the produced file to compile. This is likely to be a duplicate of #109.

latexdiff a.tex b.tex > c.tex

Trying the following does not prevent the \DIFadd{ } from being added around split.

latexdiff --exclude-safecmd "split" a.tex b.tex > c.tex

However, adding the align environment to ARRENV does prevent \DIFadd being added.

~/bin/latexdiff/latexdiff --config ARRENV="(?:(?:aligned)|(?:gathered)|(?:multlined)|(?:array)|(?:[pbvBV]?matrix)|(?:smallmatrix)|(?:cases)|(?:split)|(?:align))" a.tex b.tex > c.tex

a.tex:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}\label{eq:1}
\begin{split}
x &= y \\
& z
\end{split}
\end{equation}

\begin{equation}\label{eq:2}
\begin{split}
a &= b \\
& q
\end{split}
\end{equation}
\end{document}

b.tex:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
\begin{split}
x &= y \\
& z
\end{split} \label{eq:1} \\
\begin{split}
a &= b \\
& q
\end{split} \label{eq:2}
\end{align}
\end{document}

c.tex:

\begin{document}
\DIFdelbegin \begin{displaymath}\DIFdel{%DIFDELCMD < \label{eq:1}%%%
\begin{split}
x &= y \\
& z
\end{split}
}\end{displaymath}%DIFAUXCMD
%DIFDELCMD < 

%DIFDELCMD < %%%
\begin{displaymath}\DIFdel{%DIFDELCMD < \label{eq:2}%%%
\begin{split}
a &= b \\
& q
\end{split}
}\end{displaymath}%DIFAUXCMD
\DIFdelend \DIFaddbegin \begin{align}
\DIFadd{\begin{split}
x &= y \\
& z
\end{split} \label{eq:1} }\\
\DIFadd{\begin{split}
a &= b \\
& q
\end{split} \label{eq:2}
}\end{align}\DIFaddend 
\end{document}
Package amsmath Warning: Cannot use `split' here;
(amsmath)                trying to recover with `aligned' on input line 50.

Package amsmath Warning: Cannot use `split' here;
(amsmath)                trying to recover with `aligned' on input line 59.

! Misplaced alignment tab character &.
\split@ ->\hbox to\wd \thr@@ {}&
                                \vcenter {\vbox {\moveleft \wd \thr@@ \boxz@ }}
l.69 }\end{align}
                 \DIFaddend

Version information (I'm using 6aef2adfd80726c32934e3ec7dab363ddc3f8af8 off master):

latexdiff --version
This is LATEXDIFF 1.3.3a (Algorithm::Diff 1.1903, Perl v5.30.0)
  (c) 2004-2022 F J Tilmann
ftilmann commented 2 years ago

Thanks for preparing MWE. This is working now.

AguirreNicolas commented 1 year ago

It's seems to not be working for latexdiff-vc. Similar example as above:

\begin{align}
  \begin{split}\label{chap3:eq:r2}
      \mathrm{R^2} ={}& 1 - \bigg\lbrack\sum_{i=1}^{N}(z_i - \widehat{z}_i) \bigg/ \sum_{i=1}^{N}(z_i - \overline{z}_i)\bigg\rbrack 
  \end{split}\\
  \begin{split}\label{chap3:eq:mse}
    \mathrm{MSE} ={}& \frac{1}{N}\sum_{i=1}^{N}(z_i - \widehat{z}_i)^{2}
  \end{split}\\
  \begin{split}\label{chap3:eq:mae}
  \mathrm{MAE} ={}&\frac{1}{N}\sum_{i=1}^{N}\lvert(z_i - \widehat{z}_i)\rvert
\end{split}
\end{align}

producing diff such:

\begin{align}
  \DIFadd{\begin{split}\label{chap3:eq:r2}
      \mathrm{R^2} ={}& 1 - \bigg\lbrack\sum_{i=1}^{N}(z_i - \widehat{z}_i) \bigg/ \sum_{i=1}^{N}(z_i - \overline{z}_i)\bigg\rbrack 
  \end{split}}\\
  \DIFadd{\begin{split}\label{chap3:eq:mse}
    \mathrm{MSE} ={}& \frac{1}{N}\sum_{i=1}^{N}(z_i - \widehat{z}_i)^{2}
  \end{split}}\\
  \DIFadd{\begin{split}\label{chap3:eq:mae}
  \mathrm{MAE} ={}&\frac{1}{N}\sum_{i=1}^{N}\lvert(z_i - \widehat{z}_i)\rvert
\end{split}
}\end{align}

using

This is LATEXDIFF 1.3.2 (Algorithm::Diff 1.15 so, Perl v5.26.1)
  (c) 2004-2021 F J Tilmann
ftilmann commented 1 year ago

@AguirreNicolas I tested the example of MBradybury, so your case must be subtly different. Would you mind providing old and new file for your example as it's otherwise ambiguous.

MBradbury commented 1 year ago

Hi both, looks like @AguirreNicolas is using an older version before the fix was included.

AguirreNicolas commented 1 year ago

@ftilmann

main.tex

\documentclass[11pt,twoside]{book}
\usepackage{amsmath,amssymb,amsfonts,amsbsy,latexsym,mathtools,bm}%,bbm} 

\begin{document}

\include{A}

\end{document}

A.tex (pre-commit)

\chapter{Chap name}

Pre-align:

A.tex (commited)

\chapter{Chap name}

Pre-align:

Post-align/split:

\begin{align}
  \begin{split}\label{chap3:eq:r2}
      \mathrm{R^2} ={}& 1 - \bigg\lbrack\sum_{i=1}^{N}(z_i - \widehat{z}_i) \bigg/ \sum_{i=1}^{N}(z_i - \overline{z}_i)\bigg\rbrack 
  \end{split}\\
  \begin{split}\label{chap3:eq:mse}
    \mathrm{MSE} ={}& \frac{1}{N}\sum_{i=1}^{N}(z_i - \widehat{z}_i)^{2}
  \end{split}\\
  \begin{split}\label{chap3:eq:mae}
  \mathrm{MAE} ={}&\frac{1}{N}\sum_{i=1}^{N}\lvert(z_i - \widehat{z}_i)\rvert
\end{split}
\end{align}

bla bla bla

terminal cmd:

latexdiff-vc --flatten --git -r xxx_comit_hash_xxx main.tex > diff.tex

main-diff.tex

\documentclass[11pt,twoside]{book}
%DIF LATEXDIFF DIFFERENCE FILE
%DIF DEL /tmp/TRaKNRRQ2s/latexdiff-vc-8c1adf67796d252c21c57531ebed54c196968fbb/main.tex   Wed Nov  9 06:41:55 2022
%DIF ADD main.tex                                                                         Wed Nov  9 06:41:27 2022
\usepackage{amsmath,amssymb,amsfonts,amsbsy,latexsym,mathtools,bm}%,bbm} 
%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
\providecommand{\DIFmodbegin}{} %DIF PREAMBLE
\providecommand{\DIFmodend}{} %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 COLORLISTINGS PREAMBLE %DIF PREAMBLE
\RequirePackage{listings} %DIF PREAMBLE
\RequirePackage{color} %DIF PREAMBLE
\lstdefinelanguage{DIFcode}{ %DIF PREAMBLE
%DIF DIFCODE_UNDERLINE %DIF PREAMBLE
  moredelim=[il][\color{red}\sout]{\%DIF\ <\ }, %DIF PREAMBLE
  moredelim=[il][\color{blue}\uwave]{\%DIF\ >\ } %DIF PREAMBLE
} %DIF PREAMBLE
\lstdefinestyle{DIFverbatimstyle}{ %DIF PREAMBLE
    language=DIFcode, %DIF PREAMBLE
    basicstyle=\ttfamily, %DIF PREAMBLE
    columns=fullflexible, %DIF PREAMBLE
    keepspaces=true %DIF PREAMBLE
} %DIF PREAMBLE
\lstnewenvironment{DIFverbatim}{\lstset{style=DIFverbatimstyle}}{} %DIF PREAMBLE
\lstnewenvironment{DIFverbatim*}{\lstset{style=DIFverbatimstyle,showspaces=true}}{} %DIF PREAMBLE
%DIF END PREAMBLE EXTENSION ADDED BY LATEXDIFF

\begin{document}

 \newpage \chapter{Chap name}

Pre-align:
\DIFaddbegin 

\DIFadd{Post-align/split:
}

\begin{align}
  \DIFadd{\begin{split}\label{chap3:eq:r2}
      \mathrm{R^2} ={}& 1 - \bigg\lbrack\sum_{i=1}^{N}(z_i - \widehat{z}_i) \bigg/ \sum_{i=1}^{N}(z_i - \overline{z}_i)\bigg\rbrack 
  \end{split}}\\
  \DIFadd{\begin{split}\label{chap3:eq:mse}
    \mathrm{MSE} ={}& \frac{1}{N}\sum_{i=1}^{N}(z_i - \widehat{z}_i)^{2}
  \end{split}}\\
  \DIFadd{\begin{split}\label{chap3:eq:mae}
  \mathrm{MAE} ={}&\frac{1}{N}\sum_{i=1}^{N}\lvert(z_i - \widehat{z}_i)\rvert
\end{split}
}\end{align}

\DIFadd{bla bla bla }\DIFaddend \newpage 

\end{document}
AguirreNicolas commented 1 year ago

@MBradbury @ftilmann You are absolutly right. I was in 1.3.2 (from CTAN) Just update to 1.3.3., and work properly.

Thank you guys!

ftilmann commented 1 year ago

Great to hear this. Apologies for the delayed update of the CTAN version. I have initiated this now, and 1.3.3 should be distributed through CTAN in the next few days.