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
528 stars 74 forks source link

How does safecmd work? #311

Closed bn-peters closed 3 days ago

bn-peters commented 4 days ago

I'm trying to prevent latexdiff from placing certain commands inside of DIFadds to work around another issue (#287). For example, say I'm trying to diff the following two files:

\documentclass{article}
\begin{document}
\newcommand{\mycommand}{123}
\end{document}
\documentclass{article}
\begin{document}
\newcommand{\mycommand}{123}
\[ \mycommand \]
\end{document}

Both when running latexdiff 1.tex 2.tex or ❯ latexdiff --exclude-safecmd=mycommand 1.tex 2.tex I get (abbreviated)

% preamble
\begin{document}
\newcommand{\mycommand}{123}
\DIFaddbegin \[ \DIFadd{\mycommand }\]
 \DIFaddend\end{document}

Is there a way to ensure that \mycommand is never placed inside of a \DIFadd?

ftilmann commented 3 days ago

This only fails because the excluded command is used in math mode with --math-markup=COARSE (default) ot WHOLE options. For a quick work-around you could try --math-markup=FINE, which work on the test document but might cause trouble with more complex equations. The math markup has it's own list of unsafe commands and generally treats everything as safe so ignores the safecmd list and safecmdexcl lists. I have now added a patch, so that commands explicitly excluded with --exclude-safecmd are also added to the list of unsafe math commands, so the example above will still fail without options, but should run with --exclude-safecmd=mycommand. Please let me know if the word you are trying to exclude is a standard command in a widely-used package, and then it can be added to the preset for unsafe math commands (or you could do a pull request by modifying the following line l. 357):

my @UNSAFEMATHCMD=('qedhere','intertext','begin','end' );           

PS: Unfortunately now ever evolving github security does not let me push to my own repository. I hope I will get this fixed; otherwise I find another way to send you the patch.

ftilmann commented 3 days ago

Ok figured this out faster than expected. I hope the fix works for you.