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
513 stars 72 forks source link

Changing equation to equation* produces non working Latex code with --math-markup=3 #235

Closed torik42 closed 2 years ago

torik42 commented 3 years ago

Applying latexdiff to

% old.tex
\documentclass{article}
\usepackage{amsmath}

\begin{document}
    \begin{equation}
        a = b
    \end{equation}
\end{document}

and

% new.tex
\documentclass{article}
\usepackage{amsmath}

\begin{document}
    \begin{equation*}
        a = b
    \end{equation*}
\end{document}

with option --math-markup=3 results in the following invalid Latex code (skipping the preamble for better readability)

\begin{document}
    \DIFdelbegin %DIFDELCMD < \begin{equation}
%DIFDELCMD <         %%%
\DIFdelend \DIFaddbegin \begin{equation*}
        \DIFaddend a = b
    \DIFdelbegin %DIFDELCMD < \end{equation}
\end{MATHMODE}%DIFAUXCMD
%DIFDELCMD <  %%%
\begin{equation*}%DIFAUXCMD
\DIFdelend \DIFaddbegin \end{equation*}
 \DIFaddend\end{document}

The same also happens for any other changes to the equation type. I don’t know whether this is a known issue and what is the intended behaviour. But I think it is quite common to change the equation type when finalizing a document (i.e. when only small changes occur), most often to remove or add numbering. Since --math-markup=2 produces valid Latex code, when changing the equation type, one could hopefully copy that behaviour.

If only the star is changed, the ultimate solution—in my opinion—would be to use mathtools \usetagform (or redefining \tagform@ if only amsmath is loaded) to add the markup from \DIFadd (if the star is removed) and \DIFdel (if the star is added) to the equation number. I have not used perl before and don’t know the codebase. But if you think this is feasible and could be detected easily I could try to come up with the Latex part and try to implement it. In that case, please point me to where these things are handled.

ftilmann commented 2 years ago

Sorry for the long latency in reacting. Commit 0d71987 now should ensure that compilable code will result from just changing the equation environment with --math-markup=FINE (3). The processing flow is rather different for math-markup 3 on one side and 1 (WHOLE) or 2 (COARSE) on the other.

Unfortunately, it does not achieve the 'ultimate solution'. In FINE mode, you will essentially see the new version of the equation but the change is only visible in the source (this is consistent with general behaviour with regard to format changes, e.g. changes in font family or series are also not visible, depending on the chosen subtype). In WHOLE or COARSE modes, the equation will be shown as deleted and added, which clues you off that something in the formatting was changed; if change is from unnumbered to numberered this will be clear, but if it's from numbered to unnumbered, both equations will be shown as unnumbered. The highlighting of the equation number as you propose is visually intuitive but I can't think of a way to achieve this with any elegance in general. In WHOLE mode it would be the easiest, in COARSE I can also see a relatively straighforward way, but in FINE mode one would have to deal with a lot of special cases.

If it's important to you then open a separate issue on marking up equation numbers (for deleted equation one would also have to manipulate equation numbers to keep the numbers consistent with the new document (which I think they should be, although I guess that's arguable), copying your ideas of how this might be tackled. But I cannot guarantee that I will find the time to address this.