Академический Документы
Профессиональный Документы
Культура Документы
Font cannot be
embedded. ]%%
%%[ Warning: Times-Bold not found, using Font Substitution. Font cannot be
embedded. ]%%
%%[ Warning: Times-Roman not found, using Font Substitution. Font cannot be
embedded. ]%%
%%[ Warning: Helvetica not found, using Font Substitution. Font cannot be embedded.
]%%
%%[ Warning: Times-BoldItalic not found, using Font Substitution. Font cannot be
embedded. ]%%
#!/usr/bin/env perl
##!/usr/bin/perl -w
# latexdiff - differences two latex files on the word level
# and produces a latex file with the differences marked up.
#
# Copyright (C) 2004-16 F J Tilmann (tilmann@gfz-potsdam.de)
#
# Repository/issue tracker: https://github.com/ftilmann/latexdiff
# CTAN page: http://www.ctan.org/pkg/latexdiff
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Detailed usage information at the end of the file
#
use Getopt::Long ;
use strict ;
use warnings;
use utf8 ;
my ($algodiffversion)=split(/ /,$Algorithm::Diff::VERSION);
my ($versionstring)=<<EOF ;
This is LATEXDIFF 1.3.0 (Algorithm::Diff $Algorithm::Diff::VERSION, Perl $^V)
(c) 2004-2018 F J Tilmann
EOF
# Hash with defaults for configuration variables. These marked undef have default
values constructed from list defined in the DATA block
# (under tag CONFIG)
my %CONFIG=(
MINWORDSBLOCK => 3, # minimum number of tokens to form an independent block
# shorter identical blocks will be merged to the previous
word
SCALEDELGRAPHICS => 0.5, # factor with which deleted figures will be scaled down
(i.e. 0.5 implies they are shown at half linear size)
# this is only used for --graphics-markup=BOTH option
FLOATENV => undef , # Environments in which FL variants of defined commands
are used
PICTUREENV => undef , # Environments in which all change markup is removed
MATHENV => undef , # Environments turning on display math mode (code
also knows about \[ and \])
MATHREPL => 'displaymath', # Environment introducing deleted maths blocks
MATHARRENV => undef , # Environments turning on eqnarray math mode
MATHARRREPL => 'eqnarray*', # Environment introducing deleted maths blocks
ARRENV => undef , # Environments making arrays in math mode. The underlining
style does not cope well with those - as a result in-text math environments are
surrounded by \mbox{ } if any of these commands is used in an inline math block
COUNTERCMD => undef,
# COUNTERCMD textcmds which are associated
with a counter
# If any of these commands occur in a
deleted block
# they will be succeeded by an
\addtocounter{...}{-1}
# for the associated counter such that the
overall numbers
# should be the same as in the new file
LISTENV => undef , # list making environments - they will generally be kept
VERBATIMENV => undef, # Environments whose content should be treated as
verbatim text and not be touched
VERBATIMLINEENV => undef, # Environments whose content should be treated as
verbatim text and processed in line diff mode
CUSTOMDIFCMD => undef,# Custom dif command. Is defined in the document as a
\DELcommand and \ADDcommand version to be replaced by the diff
ITEMCMD => 'item' # command marking item in a list
environment
);
# Configuration variables: these have to be visible from the subroutines
my ($ARRENV,
$COUNTERCMD,
$FLOATENV,
$ITEMCMD,
$LISTENV,
$MATHARRENV,
$MATHARRREPL,
$MATHENV,
$MATHREPL,
$MINWORDSBLOCK,
$PICTUREENV,
$SCALEDELGRAPHICS,
$VERBATIMENV,
$VERBATIMLINEENV,
$CUSTOMDIFCMD
);
# Markup strings
# If at all possible, do not change these as parts of the program
# depend on the actual name (particularly post-processing)
# At the very least adapt subroutine postprocess to new tokens.
my $ADDMARKOPEN='\DIFaddbegin '; # Token to mark begin of appended text
my $ADDMARKCLOSE='\DIFaddend '; # Token to mark end of appended text
my $ADDOPEN='\DIFadd{'; # To mark begin of added text passage
my $ADDCLOSE='}'; # To mark end of added text passage
my $ADDCOMMENT='DIF > '; # To mark added comment line
my $DELMARKOPEN='\DIFdelbegin '; # Token to mark begin of deleted text
my $DELMARKCLOSE='\DIFdelend '; # Token to mark end of deleted text
my $DELOPEN='\DIFdel{'; # To mark begin of deleted text passage
my $DELCLOSE='}'; # To mark end of deleted text passage
my $DELCMDOPEN='%DIFDELCMD < '; # To mark begin of deleted commands (must begin
with %, i.e., be a comment
my $DELCMDCLOSE="%%%\n"; # To mark end of deleted commands (must end with a new
line)
my $AUXCMD='%DIFAUXCMD' ; # follows auxiliary commands put in by latexdiff to make
difference file legal
# auxiliary commands must be on a line of their own
# Note that for verbatim environment openings the
%DIFAUXCMD cannot be placed in
# the same line as this would mean they are shown
# so the special form "%DIFAUXCMD NEXT" is used to
indicate that the next line
# is an auxiliary command
# Similarly "%DIFAUXCMD LAST" would indicate the
auxiliary command is in previous line (not currently used)
my $DELCOMMENT='DIF < '; # To mark deleted comment line
my $VERBCOMMENT='DIFVRB '; # to mark lines which are within a verbatim environment
# Note I need to declare this with "our" instead of "my" because later in the code
I have to "local"ise these
our @SAFECMDLIST=(); # array containing patterns of safe commands (which do not
break when in the argument of DIFadd or DIFDEL)
our @SAFECMDEXCL=();
my @MBOXCMDLIST=(); # patterns for commands which are in principle safe but which
need to be surrounded by an \mbox
my @MBOXCMDEXCL=(); # all the patterns in MBOXCMDLIST will be appended to
SAFECMDLIST
my ($i,$j,$l);
my ($old,$new);
my ($line,$key);
my (@dumlist);
my ($newpreamble,$oldpreamble);
my (@newpreamble,@oldpreamble,@diffpreamble,@diffbody);
my ($latexdiffpreamble);
my ($oldbody, $newbody, $diffbo);
my ($oldpost, $newpost);
my ($diffall);
# Option names
my ($type,$subtype,$floattype,$config,$preamblefile,$encoding,$nolabel,
$visiblelabel,
$showpreamble,$showsafe,$showtext,$showconfig,$showall,
$replacesafe,$appendsafe,$excludesafe,
$replacetext,$appendtext,$excludetext,
$replacecontext1,$appendcontext1,
$replacecontext2,$appendcontext2,
$help,$verbose,$driver,$version,$ignorewarnings,
$enablecitmark,$disablecitmark,$allowspaces,$flatten,$nolinks,$debug,
$earlylatexdiffpreamble); ###$disablemathmark,
my ($mboxsafe);
# MNEMNONICS for mathmarkup
my $mathmarkup;
use constant {
OFF => 0,
WHOLE => 1,
COARSE => 2,
FINE => 3
};
# MNEMNONICS for graphicsmarkup
my $graphicsmarkup;
use constant {
NONE => 0,
NEWONLY => 1,
BOTH => 2
};
my ($mboxcmd);
my (@configlist,@addtoconfiglist,@labels,
@appendsafelist,@excludesafelist,
@appendmboxsafelist,@excludemboxsafelist,
@appendtextlist,@excludetextlist,
@appendcontext1list,@appendcontext2list,
@packagelist);
my ($assign,@config);
# Hash where keys corresponds to the names of all included packages (including the
documentclass as another package
# the optional arguments to the package are the values of the hash elements
my ($pkg,%packages);
# Defaults
$mathmarkup=COARSE;
$verbose=0;
# output debug and intermediate files, set to 0 in final distribution
$debug=0;
# insert preamble directly after documentclass - experimental feature, set to 0 in
final distribution
# Note that this failed with mini example (or other files, where packages used in
latexdiff preamble
# are called again with incompatible options in preamble of resulting file)
$earlylatexdiffpreamble=0;
my %verbhash;
Getopt::Long::Configure('bundling');
GetOptions('type|t=s' => \$type,
'subtype|s=s' => \$subtype,
'floattype|f=s' => \$floattype,
'config|c=s' => \@configlist,
'add-to-config=s' => \@addtoconfiglist,
'preamble|p=s' => \$preamblefile,
'encoding|e=s' => \$encoding,
'label|L=s' => \@labels,
'no-label' => \$nolabel,
'visible-label' => \$visiblelabel,
'exclude-safecmd|A=s' => \@excludesafelist,
'replace-safecmd=s' => \$replacesafe,
'append-safecmd|a=s' => \@appendsafelist,
'exclude-textcmd|X=s' => \@excludetextlist,
'replace-textcmd=s' => \$replacetext,
'append-textcmd|x=s' => \@appendtextlist,
'replace-context1cmd=s' => \$replacecontext1,
'append-context1cmd=s' => \@appendcontext1list,
'replace-context2cmd=s' => \$replacecontext2,
'append-context2cmd=s' => \@appendcontext2list,
'exclude-mboxsafecmd=s' => \@excludemboxsafelist,
'append-mboxsafecmd=s' => \@appendmboxsafelist,
'show-preamble' => \$showpreamble,
'show-safecmd' => \$showsafe,
'show-textcmd' => \$showtext,
'show-config' => \$showconfig,
'show-all' => \$showall,
'packages=s' => \@packagelist,
'allow-spaces' => \$allowspaces,
'math-markup=s' => \$mathmarkup,
'graphics-markup=s' => \$graphicsmarkup,
'enable-citation-markup|enforce-auto-mbox' => \$enablecitmark,
'disable-citation-markup|disable-auto-mbox' => \$disablecitmark,
'verbose|V' => \$verbose,
'ignore-warnings' => \$ignorewarnings,
'driver=s'=> \$driver,
'flatten' => \$flatten,
'no-links' => \$nolinks,
'version' => \$version,
'help|h' => \$help,
'debug!' => \$debug ) or die "Use latexdiff -h to get help.\n" ;
if ( $help ) {
usage() ;
}
if ( $version ) {
die $versionstring ;
}
if (defined($mathmarkup)) {
$mathmarkup=~tr/a-z/A-Z/;
if ( $mathmarkup eq 'OFF' ){
$mathmarkup=OFF;
} elsif ( $mathmarkup eq 'WHOLE' ){
$mathmarkup=WHOLE;
} elsif ( $mathmarkup eq 'COARSE' ){
$mathmarkup=COARSE;
} elsif ( $mathmarkup eq 'FINE' ){
$mathmarkup=FINE;
} elsif ( $mathmarkup !~ m/^[0123]$/ ) {
die "latexdiff Illegal value: ($mathmarkup) for option--math-markup. Possible
values: OFF,WHOLE,COARSE,FINE,0-3\n";
}
# else use numerical value
}
if ( defined($driver) ) {
# for changebar only
$latexdiffpreamble=~s/\[dvips\]/[$driver]/sg;
}
# setting up @SAFECMDLIST and @SAFECMDEXCL
if (defined($replacesafe)) {
init_regex_arr_ext(\@SAFECMDLIST,$replacesafe);
} else {
init_regex_arr_data(\@SAFECMDLIST, "SAFE COMMANDS");
}
foreach $appendsafe ( @appendsafelist ) {
init_regex_arr_ext(\@SAFECMDLIST, $appendsafe);
}
foreach $excludesafe ( @excludesafelist ) {
init_regex_arr_ext(\@SAFECMDEXCL, $excludesafe);
}
# setting up @MBOXCMDLIST and @MBOXCMDEXCL
foreach $mboxsafe ( @appendmboxsafelist ) {
init_regex_arr_ext(\@MBOXCMDLIST, $mboxsafe);
}
foreach $mboxsafe ( @excludemboxsafelist ) {
init_regex_arr_ext(\@MBOXCMDEXCL, $mboxsafe);
}
my @addtoconfig=();
foreach $config ( @addtoconfiglist ) {
if (-f $config || lc $config eq '/dev/null' ) {
open(FILE,$config) or die ("Couldn't open addd-to-config file $config: $!");
while (<FILE>) {
chomp;
next if /^\s*#/ || /^\s*%/ || /^\s*$/ ;
push (@addtoconfig,$_);
}
close(FILE);
}
else {
# foreach ( split(",",$config) ) {
# push @addtoconfig,$_;
# }
push @addtoconfig,split(",",$config)
}
}
# Map from hash to variables (we do this to have more concise code later, change
from comma-separated list)
foreach ( keys(%CONFIG) ) {
if ( $_ eq "MINWORDSBLOCK" ) { $MINWORDSBLOCK = $CONFIG{$_}; }
elsif ( $_ eq "FLOATENV" ) { $FLOATENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "ITEMCMD" ) { $ITEMCMD = $CONFIG{$_} ; }
elsif ( $_ eq "LISTENV" ) { $LISTENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "PICTUREENV" ) { $PICTUREENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "MATHENV" ) { $MATHENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "MATHREPL" ) { $MATHREPL = $CONFIG{$_} ; }
elsif ( $_ eq "MATHARRENV" ) { $MATHARRENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "MATHARRREPL" ) { $MATHARRREPL = $CONFIG{$_} ; }
elsif ( $_ eq "ARRENV" ) { $ARRENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "VERBATIMENV" ) { $VERBATIMENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "VERBATIMLINEENV" ) { $VERBATIMLINEENV =
liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "CUSTOMDIFCMD" ) { $CUSTOMDIFCMD = liststringtoregex($CONFIG{$_}) ;
}
elsif ( $_ eq "COUNTERCMD" ) { $COUNTERCMD = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "SCALEDELGRAPHICS" ) { $SCALEDELGRAPHICS = $CONFIG{$_} ; }
else { die "Unknown configuration variable $_.";}
}
if ( @ARGV != 2 ) {
print STDERR "2 and only 2 non-option arguments required. Write latexdiff -h to
get help\n";
exit(2);
}
my $pat0 = '(?:[^{}])*';
my $pat_n = $pat0;
# if you get "undefined control sequence MATHBLOCKmath" error, increase the maximum
value in this loop
for (my $i_pat = 0; $i_pat < 20; ++$i_pat){
$pat_n = '(?:[^{}]|\{'.$pat_n.'\}|\\\\\{|\\\\\})*';
# Actually within the text body, quoted braces are replaced in pre-processing.
The only place where
# the last part of the pattern matters is when processing the arguments of
context2cmds in the preamble
# and these contain a \{ or \} combination, probably rare.
# It should thus be fine to use the simpler version below.
### $pat_n = '(?:[^{}]|\{'.$pat_n.'\})*';
}
my $brat0 = '(?:[^\[\]]|\\\[|\\\])*';
my $brat_n = $brat0;
for (my $i_pat = 0; $i_pat < 4; ++$i_pat){
$brat_n = '(?:[^\[\]]|\['.$brat_n.'\]|\\\[|\\\])*';
### $brat_n = '(?:[^\[\]]|\['.$brat_n.'\])*'; # Version not taking into
account escaped \[ and \]
}
my $abrat0 = '(?:[^<>])*';
my $quotemarks = '(?:\'\')|(?:\`\`)';
my $punct='[0.,\/\'\`:;\"\?\(\)\[\]!~\p{IsNonAsciiPunct}\p{IsNonAsciiS}]';
my $number='-?\d*\.\d*';
my $mathpunct='[+=<>\-\|]';
my $and = '&';
my $coords= '[\-.,\s\d]*';
# quoted underscore - this needs special treatment as perl treats _ as a letter
(\w) but latex does not
# such that a\_b is interpreted as a{\_}b by latex but a{\_b} by perl
my $quotedunderscore='\\\\_';
# word: sequence of letters or accents followed by letter
my $word_ja='\p{Han}+|\p{InHiragana}+|\p{InKatakana}+';
my $word='(?:' . $word_ja . '|(?:(?:[-\w\d*]|\\\\[\"\'\`~^][A-Za-z\*])(?!(?:' .
$word_ja . ')))+)';
my $cmdleftright='\\\\(?:left|right|[Bb]igg?[lrm]?|middle)\s*(?:[<>()\
[\]|\.]|\\\\(?:[|{}]|\w+))';
my $cmdoptseq='\\\\[\w\d@\*]+'.$extraspace.'(?:(?:<'.$abrat0.'>|\['.$brat_n.'\]|\
{'. $pat_n . '\}|\(' . $coords .'\))'.$extraspace.')*';
my $defseq='\\\\def\\\\[\w\d@\*]+(?:#\d+|\[#\d+\])+(?:\{'. $pat_n . '\})?';
my $backslashnl='\\\\\n';
my $oneletcmd='\\\\.\*?(?:\['.$brat_n.'\]|\{'. $pat_n . '\})*';
my $math='\$(?:[^$]|\\\$)*?\$|\\\\[(](?:.|\n)*?\\\\[)]';
## the current maths command cannot cope with newline within the math expression
my $comment='%[^\n]*\n';
my $pat=qr/(?:\A\s*)?(?:${and}|${quotemarks}|${number}|${word}|$quotedunderscore|
${defseq}|$cmdleftright|${cmdoptseq}|${math}|${backslashnl}|${oneletcmd}|$
{comment}|${punct}|${mathpunct}|\{|\})\s*/ ;
my ($oldtime,$newtime,$oldlabel,$newlabel);
if (defined($labels[0])) {
$oldlabel=$labels[0] ;
} else {
$oldtime=localtime((stat($oldfile))[9]);
$oldlabel="$oldfile " . " "x($oldlabel_n_spaces) . $oldtime;
}
if (defined($labels[1])) {
$newlabel=$labels[1] ;
} else {
$newtime=localtime((stat($newfile))[9]);
$newlabel="$newfile " . " "x($newlabel_n_spaces) . $newtime;
}
$old=read_file_with_encoding($oldfile,$encoding);
$new=read_file_with_encoding($newfile,$encoding);
# reset time
exetime(1);
($oldpreamble,$oldbody,$oldpost)=splitdoc($old,'\\\\begin\{document\}','\\\\end\
{document\}');
($newpreamble,$newbody,$newpost)=splitdoc($new,'\\\\begin\{document\}','\\\\end\
{document\}');
if ($flatten) {
$oldbody=flatten($oldbody,$oldpreamble,$oldfile,$encoding);
$newbody=flatten($newbody,$newpreamble,$newfile,$encoding);
# flatten preamble
$oldpreamble=flatten($oldpreamble,$oldpreamble,$oldfile,$encoding);
$newpreamble=flatten($newpreamble,$newpreamble,$newfile,$encoding);
my @auxlines;
# boolean variab
my ($ulem)=0;
# If a command is defined in the preamble of the new file, and only uses safe
commands, then it can be considered to be safe) (contribution S. Gouezel)
# Base this assessment on the new preamble
add_safe_commands($newpreamble);
if (defined $packages{"hyperref"} ) {
# deleted lines should not generate or appear in link names:
print STDERR "hyperref package detected.\n" if $verbose ;
$latexdiffpreamble =~ s/\{\\DIFadd\}/{\\DIFaddtex}/g;
$latexdiffpreamble =~ s/\{\\DIFdel\}/{\\DIFdeltex}/g;
$latexdiffpreamble .= join "\n",(extrapream("HYPERREF"),"");
if($nolinks){
$latexdiffpreamble .= "\n\\hypersetup{bookmarks=false}";
}
### $latexdiffpreamble .= '%DIF PREAMBLE EXTENSION ADDED BY LATEXDIFF FOR
HYPERREF PACKAGE' . "\n";
### $latexdiffpreamble .= '\providecommand{\DIFadd}[1]
{\texorpdfstring{\DIFaddtex{#1}}{#1}}' . "\n";
### $latexdiffpreamble .= '\providecommand{\DIFdel}[1]
{\texorpdfstring{\DIFdeltex{#1}}{}}' . "\n";
### $latexdiffpreamble .= '%DIF END PREAMBLE EXTENSION ADDED BY LATEXDIFF FOR
HYPERREF PACKAGE' . "\n";
}
# If listings is being used or can be found in the latexdiff search path, add to
the preamble auxiliary code to enable line-by-line markup
if ( defined($packages{"listings"}) or `kpsewhich listings.sty` ne "" ) {
my @listingpreamble=extrapream("LISTINGS");
my @listingDIFcode=();
my $replaced;
# note that in case user supplies preamblefile the type might not reflect well
the
@listingDIFcode=extrapream("-nofail","DIFCODE_" . $type) unless
defined($preamblefile);
if (!(@listingDIFcode)) {
# if listingDIFcode is empty try to guess a suitable one from the preamble
if ($latexdiffpreamble =~ /\\RequirePackage(?:\[$brat_n\])?\{color\}/ and $ulem
) {
@listingDIFcode=extrapream("DIFCODE_UNDERLINE");
} elsif ( $latexdiffpreamble =~ /\\RequirePackage(?:\[$brat_n\])?\{color\}/ ) {
# only colour used
@listingDIFcode=extrapream("DIFCODE_CFONT");
} else {
# fall-back solution
@listingDIFcode=extrapream("DIFCODE_BOLD");
}
}
# now splice it in
$replaced=0;
###print STDERR "DEBUG: listingDIFcode: ",join("\n",@listingDIFcode),"|||\n" if
$debug;
@listingpreamble=grep {
# only replace if this has not been done already (use short-circuit property of
and)
if (!$replaced and $_ =~ s/^.*%DIFCODE TEMPLATE.*$/join("\n",@listingDIFcode)/e
) {
###print STDERR "DEBUG: Replaced text $_\n" if $debug;
$replaced=1;
1;
} else {
# return false for those lines matching %DIFCODE TEMPLATE (so that they are
not included in output)
not m/%DIFCODE TEMPLATE/;
}
} @listingpreamble;
### print STDERR "DEBUG: listingpreamble @listingpreamble\n";
$latexdiffpreamble .= join "\n",(@listingpreamble,"");
} else {
print STDERR "WARNING: listings package not detected. Disabling mark-up in
verbatim environments \n" ;
# if listings does not exist disable line-by-line markup and treat all verbatim
environments as opaque
$VERBATIMENV = liststringtoregex($CONFIG{VERBATIMENV}.";".
$CONFIG{VERBATIMLINEENV});
$VERBATIMLINEENV = "";
}
# insert dummy first line such that line count begins with line 1 (rather than
perl's line 0) - just so that line numbers inserted by linediff are correct
unshift @newpreamble,'';
unshift @oldpreamble,'';
@diffpreamble = linediff(\@oldpreamble, \@newpreamble);
# remove dummy line again
shift @diffpreamble;
# add filenames, modification time and latexdiff mark
defined($nolabel) or splice @diffpreamble,1,0,
"%DIF LATEXDIFF DIFFERENCE FILE",
,"%DIF DEL $oldlabel",
"%DIF ADD $newlabel";
if ( @auxlines ) {
push @diffpreamble,"%DIF DELETED TITLE COMMANDS FOR MARKUP";
push @diffpreamble,join("\n",@auxlines);
}
if ( $earlylatexdiffpreamble) {
# insert latexdiff command directly after documentclass at beginning of
preamble
# note that grep is only run for its side effect
( grep { s/^([^%]*\\documentclass.*)$/$1$latexdiffpreamble/ } @diffpreamble )
==1 or die "Could not find documentclass statement in preamble";
} else {
# insert latexdiff commands at the end of preamble (default behaviour)
push @diffpreamble,$latexdiffpreamble;
}
push @diffpreamble,'\begin{document}';
if (defined $packages{"hyperref"} && $nolinks) {
push @diffpreamble, '\begin{NoHyper}';
}
}
elsif ( !length $oldpreamble && !length $newpreamble ) {
@diffpreamble=();
} else {
print STDERR "Either both texts must have preamble or neither text must have the
preamble.\n";
exit(2);
}
# Special: treat all cite commands as safe except in UNDERLINE and FONTSTRIKE mode
# (there is a conflict between citation and ulem package, see
# package documentation)
# Use post-processing
# and $packages{"apacite"}!~/natbibpapa/
if (defined $packages{"siunitx"} ) {
# protect SI command by surrounding them with an \mbox
# this is done to get around an incompatibility between the ulem and siunitx
package
print STDERR "siunitx package detected.\n" if $verbose ;
my $mboxcmds='SI,ang,numlist,numrange,SIlist,SIrange';
init_regex_arr_list(\@SAFECMDLIST,'num,si');
if ( $enablecitmark || ( $ulem && ! $disablecitmark )) {
init_regex_arr_list(\@MBOXCMDLIST,$mboxcmds);
} else {
init_regex_arr_list(\@SAFECMDLIST,$mboxcmds);
}
}
if (defined $packages{"cleveref"} ) {
# protect selected command by surrounding them with an \mbox
# this is done to get around an incompatibility between ulem and cleveref package
print STDERR "cleveref package detected.\n" if $verbose ;
my $mboxcmds='[Cc]ref(?:range)?\*?,labelcref,(?:lc)?name[cC]refs?' ;
if ( $enablecitmark || ( $ulem && ! $disablecitmark )) {
init_regex_arr_list(\@MBOXCMDLIST,$mboxcmds);
} else {
init_regex_arr_list(\@SAFECMDLIST,$mboxcmds);
}
}
if (defined $packages{"glossaries"} ) {
# protect selected command by surrounding them with an \mbox
# this is done to get around an incompatibility between ulem and glossaries
package
print STDERR "glossaries package detected.\n" if $verbose ;
my $mboxcmds='[gG][lL][sS](?:|pl|disp|link|first|firstplural|desc|user[iv][iv]?
[iv]?),[aA][cC][rR](?:long|longpl|full|fullpl),[aA][cC][lfp]?[lfp]?';
init_regex_arr_list(\@SAFECMDLIST,'[gG][lL][sS](?:(?:entry)?(?:text|plural|name|
symbol)|displaynumberlist|entryfirst|entryfirstplural|entrydesc|entrydescplural|
entrysymbolplural|entryuser[iv][iv]?[iv]?|entrynumberlist|entrydisplaynumberlist|
entrylong|entrylongpl|entryshort|entryshortpl|entryfull|entryfullpl),
[gG]lossentry(?:name|desc|symbol),[aA][cC][rR](?:short|shortpl),[aA]csp?');
if ( $enablecitmark || ( $ulem && ! $disablecitmark )) {
init_regex_arr_list(\@MBOXCMDLIST,$mboxcmds);
} else {
init_regex_arr_list(\@SAFECMDLIST,$mboxcmds);
}
}
if (defined $packages{"mhchem"} ) {
print STDERR "mhchem package detected.\n" if $verbose ;
init_regex_arr_list(\@SAFECMDLIST,'ce');
push(@UNSAFEMATHCMD,'ce','cee');
# The next command would be needed to allow highlighting the interior of \cee
commands in math environments
# but the redefinitions in chemformula are too deep to make this viable
# push(@MATHTEXTCMDLIST,'cee');
}
my ( $citpat);
if ( defined $packages{"apacite"} ) {
print STDERR "apacite package detected.\n" if $verbose ;
$citpat='(?:mask)?(?:full|short|no)?cite(?:A|author|year|meta)?(?:NP)?';
} else {
# citation command pattern for all other citation schemes
$citpat='(?:cite\w*|nocite)';
};
if ( ! $ulem ) {
# modes not using ulem: citation is safe
push (@SAFECMDLIST, $citpat);
} else {
### Experimental: disable text and emph commands
push(@SAFECMDEXCL, qr/^emph$/, qr/^text..$/);
# replace \cite{..} by \mbox{\cite{..}} in added or deleted blocks in post-
processing
push(@MBOXCMDLIST,$citpat) unless $disablecitmark;
if ( uc($subtype) eq "COLOR" or uc($subtype) eq "DVIPSCOL" ) {
# remove \cite command again from list of safe commands
pop @MBOXCMDLIST;
# deleted cite commands
}
}
push(@MBOXCMDLIST,$citpat) if $enablecitmark ;
$diffall .= "$diffbo" ;
if (defined $packages{"hyperref"} && $nolinks) {
$diffall .= "\\end{NoHyper}\n";
}
$diffall .= "\\end{document}$newpost" if length $newpreamble ;
if ( lc($encoding) ne "utf8" && lc($encoding) ne "ascii" ) {
print STDERR "Encoding output file to $encoding\n" if $verbose;
$diffall=Encode::encode($encoding,$diffall);
binmode STDOUT;
}
print $diffall;
# liststringtoregex(liststring)
# expands string with semi-colon separated list into a regular expression
corresponding
# matching any of the elements
sub liststringtoregex {
my ($liststring)=@_;
my @elements=grep /\S/,split(";",$liststring);
if ( @elements) {
return('(?:(?:' . join(')|(?:',@elements) .'))');
} else {
return "";
}
}
# show_configuration
# note that this is not encapsulated but uses variables from the main program
# It is provided for convenience because in the future it is planned to allow
output
# to be modified based on what packages are read etc - this works only if the input
files are actually read
# whether or not additional files are provided
sub show_configuration {
if ($showpreamble) {
print "\nPreamble commands:\n";
print $latexdiffpreamble ;
}
if ($showsafe) {
print "\nsafecmd: Commands safe within scope of $ADDOPEN $ADDCLOSE and $DELOPEN
$DELCLOSE (unless excluded):\n";
print_regex_arr(@SAFECMDLIST);
print "\nsafecmd-exlude: Commands not safe within scope of $ADDOPEN $ADDCLOSE
and $DELOPEN $DELCLOSE :\n";
print_regex_arr(@SAFECMDEXCL);
print "\nmboxsafecmd: Commands safe only if they are surrounded by \\mbox
command:\n";
print_regex_arr(@MBOXCMDLIST);
print "\nnmboxsafecmd: Commands not safe:\n";
print_regex_arr(@MBOXCMDEXCL);
}
if ($showtext) {
print "\nCommands with last argument textual (unless excluded) and safe in
every context:\n";
print_regex_arr(@TEXTCMDLIST);
print "\nContext1 commands (last argument textual, command will be disabled in
deleted passages, last argument will be shown as plain text):\n";
print_regex_arr(@CONTEXT1CMDLIST);
print "\nContext2 commands (last argument textual, command and its argument
will be disabled in deleted passages):\n";
print_regex_arr(@CONTEXT2CMDLIST);
print "\nExclude list of Commands with last argument not textual (overrides
patterns above):\n";
print_regex_arr(@TEXTCMDEXCL);
}
if ($showconfig) {
print "Configuration variables:\n";
print "ARRENV=$ARRENV\n";
print "COUNTERCMD=$COUNTERCMD\n";
print "FLOATENV=$FLOATENV\n";
print "ITEMCMD=$ITEMCMD\n";
print "LISTENV=$LISTENV\n";
print "MATHARRENV=$MATHARRENV\n";
print "MATHARRREPL=$MATHARRREPL\n";
print "MATHENV=$MATHENV\n";
print "MATHREPL=$MATHREPL\n";
print "MINWORDSBLOCK=$MINWORDSBLOCK\n";
print "PICTUREENV=$PICTUREENV\n";
print "SCALEDELGRAPHICS=$SCALEDELGRAPHICS\n";
print "VERBATIMENV=$VERBATIMENV\n";
print "VERBATIMLINEENV=$VERBATIMLINEENV\n";
print "CUSTOMDIFCMD=$CUSTOMDIFCMD\n";
}
}
## guess_encoding(filename)
## reads the first 20 lines of filename and looks for call of inputenc package
## if found, return the option of this package (encoding), otherwise return utf8
sub guess_encoding {
my ($filename)=@_;
my ($i,$enc);
open (FH, $filename) or die("Couldn't open $filename: $!");
$i=0;
while (<FH>) {
next if /^\s*%/; # skip comment lines
if (m/\\usepackage\[(\w*?)\]\{inputenc\}/) {
close(FH);
return($1);
}
last if (++$i > 20 ); # scan at most 20 non-comment lines
}
close(FH);
### return("ascii");
return("utf8");
}
sub read_file_with_encoding {
my ($output);
my ($filename, $encoding) = @_;
if (lc($encoding) eq "utf8" ) {
open (FILE, "<:utf8",$filename) or die("Couldn't open $filename: $!");
local $/ ; # locally set record operator to undefined, ie. enable whole-file
mode
$output=<FILE>;
} elsif ( lc($encoding) eq "ascii") {
open (FILE, $filename) or die("Couldn't open $filename: $!");
local $/ ; # locally set record operator to undefined, ie. enable whole-file
mode
$output=<FILE>;
} else {
require Encode;
open (FILE, "<",$filename) or die("Couldn't open $filename: $!");
local $/ ; # locally set record operator to undefined, ie. enable whole-file
mode
$output=<FILE>;
print STDERR "Converting $filename from $encoding to utf8\n" if $verbose;
$output=Encode::decode($encoding,$output);
}
close FILE;
if ($^O eq "linux" ) {
$output =~ s/\r\n/\n/g ;
}
return $output;
}
## %packages=list_packages(@preamble)
## scans the arguments for \documentclass,\RequirePackage and \usepackage
statements and constructs a hash
## whose keys are the included packages, and whose values are the associated
optional arguments
#sub list_packages {
# my (@preamble)=@_;
# my %packages=();
# foreach $line ( @preamble ) {
# # get rid of comments
# $line=~s/(?<!\\)%.*$// ;
# if ( $line =~ m/\\(?:documentclass|usepackage|RequirePackage)(?:\[(.+?)\])?\
{(.*?)\}/ ) {
## print STDERR "Found something: |$line|\n" if $debug;
# if (defined($1)) {
# $packages{$2}=$1;
# } else {
# $packages{$2}="";
# }
# }
# }
# return (%packages);
#}
# %packages=list_packages($preamble)
# scans the arguments for \documentclass,\RequirePackage and \usepackage statements
and constructs a hash
# whose keys are the included packages, and whose values are the associated
optional arguments
# if argument of \usepackage or \RequirePackage is comma separated list, treat as
different packages
sub list_packages {
my ($preamble)=@_;
my %packages=();
my $pkg;
# remove comments
$preamble=~s/(?<!\\)%.*$//mg ;
# sometimes, class options are defined in such a way that they imply the loading
and/or presence of a package
# so we also treat all class options as 'packages.
if ( $preamble =~ m/\\documentclass\s*\[($brat_n)\]\s*\{.*?\}/s ) {
foreach $pkg ( split /,/,$1 ) {
$pkg =~ s/\s//g ; # remove space and newline characters
$packages{$pkg}="" unless exists($packages{$pkg});
}
}
return (%packages);
}
"$begline$replacement";
}/exgm;
if ($command eq 'verbatiminput' ) {
$verbenv = "verbatim" ;
} elsif ($command eq 'verbatiminput*' ) {
$verbenv = "verbatim*" ;
} elsif ($command eq 'lstinputlisting' ) {
$verbenv = "lstlisting" ;
} else {
die "Internal errorL Unexpected verbatim input type $command.\n";
}
print STDERR "DEBUG Beg of line match |$begline|\n" if $debug ;
print STDERR "Include file $fname verbatim\n" if $verbose;
print STDERR "DEBUG looking for file ",File::Spec->catfile($dirname,$fname),
"\n" if $debug;
# content of file becomes replacement value (do not use recursion), add
\newpage if the command was include
###$replacement=read_file_with_encoding(File::Spec->catfile($dirname,$fname),
$encoding) or die "Couldn't find file ",File::Spec->catfile($dirname,$fname),":
$!";
$replacement=read_file_with_encoding(File::Spec->catfile($dirname,$fname),
$encoding) or die "Couldn't find file ",File::Spec->catfile($dirname,$fname),":
$!";
# Add a new line if it not already there (note that the matching operator
needs to use different delimiters, as we are still inside an outer scope that takes
precedence
$replacement .= "\n" unless $replacement =~ m(\n$) ;
"$begline\\begin{$verbenv}$verboptions\n$replacement\\end{$verbenv}\n";
}/exgm;
return($text);
}
# print_regex_arr(@arr)
# prints regex array without x-ism expansion put in by pearl to stdout
sub print_regex_arr {
my $dumstring;
$dumstring = join(" ",@_); # PERL generates string (?-xism:^ref$) for quoted
refex ^ref$
$dumstring =~ s/\(\?-xism:\^(.*?)\$\)/$1/g; # remove string and ^,$ marks
before output
print $dumstring,"\n";
}
# @lines=extrapream($type,...)
# reads line from appendix or external file
# (end of file after __END__ token)
# if $type is a filename, it will read the file instead of reading from the
appendix
# otherwise it will screen appendix for line "%DIF $TYPE" and copy everything up to
line
# '%DIF END $TYPE' (where $TYPE is upcased version of $type)
# extrapream('-nofail',$type) will---instead of failing---simply return nothing if
# it does not find the matching line in a appendix (do not use -nofail option with
multiple types!)
sub extrapream {
my @types=@_;
my ($type,$arg);
my $nofail=0;
###my @retval=("%DIF PREAMBLE EXTENSION ADDED BY LATEXDIFF") ;
my @retval=();
my ($copy);
# ($part1,$part2,$part3)=splitdoc($text,$word1,$word2)
# splits $text into 3 parts at $word1 and $word2.
# if neither $word1 nor $word2 exist, $part1 and $part3 are empty, $part2 is $text
# If only $word1 or $word2 exist but not the other, output an error message.
# NB this version avoids $` and $' for performance reason although it only makes a
tiny difference
# (in one test gain a tenth of a second for a 30s run)
sub splitdoc {
my ($text,$word1,$word2)=@_;
my ($part1,$part2,$part3)=("","","");
my ($rest,$pos);
if ( $text =~ m/(^[^%]*)($word1)/mg ) {
$pos=pos $text;
$part1=substr($text,0,$pos-length($2));
$rest=substr($text,$pos);
if ( $rest =~ m/(^[^%]*)($word2)/mg ) {
$pos=pos $rest;
$part2=substr($rest,0,$pos-length($2));
$part3=substr($rest,$pos);
}
else {
die "$word1 and $word2 not in the correct order or not present as a pair." ;
}
} else {
$part2=$text;
die "$word2 present but not $word1." if ( $text =~ m/(^[^%]*)$word2/ms );
}
return ($part1,$part2,$part3);
}
# bodydiff($old,$new)
sub bodydiff {
my ($oldwords, $newwords) = @_;
my @retwords;
if ( $debug ) {
open(TOKENOLD,">","latexdiff.debug.tokenold");
print TOKENOLD join("***\n",@oldwords);
close(TOKENOLD);
open(TOKENNEW,">","latexdiff.debug.tokennew");
print TOKENNEW join("***\n",@newwords);
close(TOKENNEW);
}
print STDERR "(",exetime()," s)\n","Pass 2: inserting DIF tokens and mark up. "
if $verbose;
if ( $debug ) {
open(TOKENOLD,">","latexdiff.debug.tokenold2");
print TOKENOLD join("***\n",@oldwords);
close(TOKENOLD);
open(TOKENNEW,">","latexdiff.debug.tokennew2");
print TOKENNEW join("***\n",@newwords);
close(TOKENNEW);
}
@retwords=pass2(\@oldwords, \@newwords);
return(@retwords);
}
# @words=splitlatex($string)
# split string according to latex rules
# Each element of words is either
# a word (including trailing spaces and punctuation)
# a latex command
# if there is white space in the beginning return that as first token
sub splitlatex {
my ($inputstring) = @_ ;
my $string=$inputstring ;
# if input is empty, return empty list
length($string)>0 or return ();
$string=~s/^(\s*)//s;
my $leadin=$1;
length($string)>0 or return ($leadin);
my @retval=($string =~ m/$pat/osg);
if (length($string) != length(join("",@retval))) {
print STDERR "\nWARNING: Inconsistency in length of input string and parsed
string:\n This often indicates faulty or non-standard latex code.\n In many
cases you can ignore this and the following warning messages.\n Note that character
numbers in the following are counted beginning after \\begin{document} and are only
approximate." unless $ignorewarnings;
print STDERR "DEBUG Original length ",length($string)," Parsed length
",length(join("",@retval)),"\n" if $debug;
print STDERR "DEBUG Input string: |$string|\n" if (length($string)<500) &&
$debug;
print STDERR "DEBUG Token parsing: |",join("+",@retval),"|\n" if
(length($string)<500) && $debug ;
@retval=();
# slow way only do this if other m//sg method fails
my $last = 0;
while ( $string =~ m/$pat/osg ) {
my $match=$&;
if ($last + length $& != pos $string ) {
my $pos=pos($string);
my $offset=30<$last ? 30 : $last;
my $dum=substr($string,$last-$offset,$pos-$last+2*$offset);
my $dum1=$dum;
my $cnt=$#retval;
my $i;
$dum1 =~ s/\n/ /g;
unless ($ignorewarnings) {
print STDERR "\n$dum1\n";
print STDERR " " x 30,"^" x ($pos-$last)," " x 30,"\n";
print STDERR "Missing characters near word " . (scalar @retval) . "
character index: " . $last . "-" . pos($string) . " Length: " . length($match) . "
Match: |$match| (expected match marked above).\n";
}
# put in missing characters `by hand'
push (@retval, substr($dum,$offset,$pos-$last-length($match)));
# Note: there seems to be a bug in substr with utf8 that made the following
line output substr which were too long,
# using dum instead appears to work
# push (@retval, substr($string,$last, pos($string)-$last-length($match)));
}
push (@retval, $match);
$last=pos $string;
}
}
unshift(@retval,$leadin) if (length($leadin)>0);
return @retval;
}
# pass1( \@seq1,\@seq2)
# Look for differences between seq1 and seq2.
# Where an common-subsequence block is flanked by deleted or appended blocks,
# and is shorter than $MINWORDSBLOCK words it is appended
# to the last deleted or appended word. If the block contains tokens other than
words
# or punctuation it is not merged.
# Deleted or appended block consisting of words and safe commands only are
# also merged, to prevent break-up in pass2 (after previous isolated words have
been removed)
# If there are commands with textual arguments (e.g. \caption) both in
corresponding
# appended and deleted blocks split them such that the command and opening bracket
# are one token, then the rest is split up following standard rules, and the
closing
# bracket is a separate token, ie. turn
# "\caption{This is a textual argument}" into
# ("\caption{","This ","is ","a ","textual ","argument","}")
# No return value. Destructively changes sequences
sub pass1 {
my $seq1 = shift ;
my $seq2 = shift ;
my ($last1,$last2)=(-1,-1) ;
my $cnt=0;
my $block=[];
my $addblock=[];
my $delblock=[];
my $todo=[];
my $instruction=[];
my $i;
my (@delmid,@addmid,@dummy);
my ($addcmds,$delcmds,$matchindex);
my ($addtextblocks,$deltextblocks);
my ($addtokcnt,$deltokcnt,$mattokcnt)=(0,0,0);
my ($addblkcnt,$delblkcnt,$matblkcnt)=(0,0,0);
my $adddiscard = sub {
if ($cnt > 0 ) {
$matblkcnt++;
# just after an unchanged block
# print STDERR "Unchanged block $cnt, $last1,$last2 \n";
if ($cnt < $MINWORDSBLOCK
&& $cnt==scalar (
grep { /^$wpat/ || ( /^\\((?:[`'^"~=.]|[\w\d@*]+))
((?:\[$brat_n\]|\{$pat_n\})*)/o
&&
iscmd($1,\@SAFECMDLIST,\@SAFECMDEXCL)
&& scalar(@dummy=split(" ",$2))<3 ) }
@$block) ) {
# merge identical blocks shorter than $MINWORDSBLOCK
# and only containing ordinary words
# with preceding different word
# We cannot carry out this merging immediately as this
# would change the index numbers of seq1 and seq2 and confuse
# the algorithm, instead we store in @$todo where we have to
merge
push(@$todo, [ $last1,$last2,$cnt,@$block ]);
}
$block = [];
$cnt=0; $last1=-1; $last2=-1;
}
};
my $discard=sub { $deltokcnt++;
&$adddiscard; #($_[0],$_[1]);
push(@$delblock,[ $seq1->[$_[0]],$_[0] ]);
$last1=$_[0] };
$addblock=[];
$delblock=[];
}
push(@$block,$seq2->[$_[1]]);
$cnt++ };
if ($verbose) {
print STDERR "\n";
print STDERR " $mattokcnt matching tokens in $matblkcnt blocks.\n";
print STDERR " $deltokcnt discarded tokens in $delblkcnt blocks.\n";
print STDERR " $addtokcnt appended tokens in $addblkcnt blocks.\n";
}
}
# extracttextblocks(\@blockindex)
# $blockindex has the following format
# [ [ token1, index1 ], [token2, index2],.. ]
# where index refers to the index in the original old or new word sequence
# Returns: reference to an array of the form
# [[ $index, $textblock, $cnt ], ..
# where $index index of block to be merged
# $textblock contains all the words to be merged with the word at $index (but
does not contain this word)
# $cnt is length of block
#
# requires: iscmd
#
sub extracttextblocks {
my $block=shift;
my ($i,$token,$index);
my $textblock=[];
my $last=-1;
my $wpat=qr/^(?:[a-zA-Z.,'`:;?()!]*)[\s~]*$/; #'
my $retval=[];
# extractcommands( \@blockindex )
# $blockindex has the following format
# [ [ token1, index1 ], [token2, index2],.. ]
# where index refers to the index in the original old or new word sequence
# Returns: reference to an array of the form
# [ [ "\cmd1", index, "\cmd1[optarg]{arg1}{", "arg2" ,"} " ],..
# where index is just taken from input array
# command must have a textual argument as last argument
#
# requires: iscmd
#
sub extractcommands {
my $block=shift;
my ($i,$token,$index,$cmd,$open,$mid,$closing);
my $retval=[];
# pass2( \@seq1,\@seq2)
# Look for differences between seq1 and seq2.
# Mark begin and end of deleted and appended sequences with tags $DELOPEN and
$DELCLOSE
# and $ADDOPEN and $ADDCLOSE, respectively, however exclude { } & and all comands,
unless
# they match an element of the whitelist (SAFECMD)
# For words in TEXTCMD but not in SAFECMD, enclose interior with $ADDOPEN and
$ADDCLOSE brackets
# Deleted comment lines are marked with %DIF <
# Added comment lines are marked with %DIF >
sub pass2 {
my $seq1 = shift ;
my $seq2 = shift ;
my ($addtokcnt,$deltokcnt,$mattokcnt)=(0,0,0);
my ($addblkcnt,$delblkcnt,$matblkcnt)=(0,0,0);
my $retval = [];
my $delhunk = [];
my $addhunk = [];
if ($verbose) {
print STDERR "\n";
print STDERR " $mattokcnt matching tokens. \n";
print STDERR " $deltokcnt discarded tokens in $delblkcnt blocks.\n";
print STDERR " $addtokcnt appended tokens in $addblkcnt blocks.\n";
}
return(@$retval);
}
# marktags($openmark,$closemark,$open,$close,$opencmd,$closecmd,$comment,\@block)
# returns ($openmark,$open,$block,$close,$closemark) if @block contains no commands
(except white-listed ones),
# braces, ampersands, or comments
# mark comments with $comment
# exclude all other exceptions from scope of open, close like this
# ($openmark, $open,...,$close, $opencmd,command, command,$closecmd, $open, ...,
$close, $closemark)
# If $opencmd begins with "%" marktags assumes it is operating on a deleted block,
otherwise on an added block
sub marktags {
my ($openmark,$closemark,$open,$close,$opencmd,$closecmd,$comment,$block)=@_;
my $word;
my (@argtext);
my $retval=[];
my $noncomment=0;
my $cmd=-1; # -1 at beginning 0: last token written is a ordinary word
# 1: last token written is a command
# for keeping track whether we are just in a command sequence or in
a word sequence
my $cmdcomment= ($opencmd =~ m/^%/); # Flag to indicate whether opencmd is a
comment (i.e. if we intend to simply comment out changed commands)
my ($command,$commandword,$closingbracket) ; # temporary variables needed below
to remember sub-pattern matches
#used in preprocess
sub take_comments_and_enter_from_frac() {
#*************take the \n and % between frac and {}***********
#notice all of the substitution are made none global
while( m/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)}(.*?)\\frac(([\s]*%
[^\n]*?)*[\r\n|\r|\n])+\{(.*?)\\end\{\1}/s ) {
# if there isn't any % or \n in the pattern $2 then there should be an
\\end{...} in $2
### print STDERR "Match the following in take_comments
and_enter_from_frac(1):\n****$&****\n" if $debug;
if( $2 !~ m/\\end\{$1}/s ) {
# take out % and \n from the next match only (none global)
s/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)}(.*?)\\frac(([\s]*%
[^\n]*?)*[\r\n|\r|\n])+\{(.*?)\\end\{\1}/\\begin{$1}$2\\frac{$5\\end{$1}/s;
}
else{
#there are no more % and \n in $2, we want to find the next one so we clear
the begin-end from the pattern
s/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)}(.*?)\\end\
{\1}/MATHBLOCK$1\{$2\}MATHBLOCKEND/s;
}
}
###cleaning up
while( s/MATHBLOCK($MATHENV|$MATHARRENV|SQUAREBRACKET)\
{(.*?)\}MATHBLOCKEND/\\begin{$1}$2\\end{$1}/s ){}
###*************take the \n and % between frac and {}***********
}
###cleaning up
while( s/MATHBLOCK($MATHENV|$MATHARRENV|SQUAREBRACKET)\
{(.*?)\}MATHBLOCKEND/\\begin{$1}$2\\end{$1}/s ){}
s/FRACSTART/\\frac/g;
###***************take the \n and % between {} and {} of the
frac*********************
}
# preprocess($string, ..)
# carry out the following pre-processing steps for all arguments:
# 1. Remove leading white-space
# Change \{ to \QLEFTBRACE and \} to \QRIGHTBRACE and \& to \AMPERSAND
# #. Change {,} in comments to \CLEFTBRACE, \CRIGHTBRACE
# 2. mark all first empty line (in block of several) with \PAR tokens
# 3. Convert all '\%' into '\PERCENTAGE ' and all '\$' into \DOLLAR to make parsing
regular expressions easier
# 4. Convert all \verb|some verbatim text| commands (where | can be an arbitrary
character)
# into \verb{hash} (also lstinline)
# 5. Convert \begin{verbatim} some verbatim text \end{verbatim} into
\verbatim{hash} (not only verbatim, all patterns matching VERBATIMENV)
# 6. Convert _n into \SUBSCRIPTNB{n} and _{nnn} into \SUBSCRIPT{nn}
# 7. Convert ^n into \SUPERSCRIPTNB{n} and ^{nnn} into \SUPERSCRIPT{nn}
# 8. a. Convert $$ $$ into \begin{DOLLARDOLLAR} \end{DOLLARDOLLAR}
# b. Convert \[ \] into \begin{SQUAREBRACKET} \end{SQUAREBRACKET}
# 9. Convert all picture environmentent (\begin{PICTUREENV} .. \end{PICTUREENV}
\PICTUREBLOCKenv
# For math-mode COARSE,WHOLE or NONE option -convert all \begin{MATH} ..
\end{MATH}
# into \MATHBLOCKmath{...} commands, where MATH/math is any valid math
environment
# 10. Add final token STOP to the very end. This is put in because the algorithm
works better if the last token is identical. This is removed again in
postprocessing.
#
# NB: step 6 and 7 is likely to convert some "_" inappropriately, e.g. in file
# names or labels but it does not matter because they are converted back in the
postprocessing step
# Returns: leading white space removed in step 1
sub preprocess {
for (@_) {
# change in \verb and similar commands - note that I introduce an extra space
here so that the
# already hashed variants do not trigger again
# transform \lstinline{...}
# s/\\lstinline(\[$brat0\])?(\{(?:.*?)\})/"\\DIFlstinline". $1 ."{". tohash(\
%verbhash,"$2") ."}"/esg;
# s/\\lstinline(\[$brat0\])?((\S).*?\2)/"\\DIFlstinline". $1 ."{". tohash(\
%verbhash,"$2") ."}"/esg;
s/\\lstinline((?:\[$brat_n\])?)(\{(?:.*?)\})/"\\DIFlstinline". $1 ."{".
tohash(\%verbhash,"$2") ."}"/esg;
s/\\lstinline((?:\[$brat_n\])?)(([^\s\w]).*?\3)/"\\DIFlstinline". $1 ."{".
tohash(\%verbhash,"$2") ."}"/esg;
s/\\(verb\*?|lstinline)([^\s\w])(.*?)\2/"\\DIF${1}{". tohash(\
%verbhash,"${2}${3}${2}") ."}"/esg;
# mark all first empty line (in block of several) with \PAR tokens
s/\n(\s*?)\n((?:\s*\n)*)/\n$1\\PAR\n$2/g ;
# Convert _n or _\cmd into \SUBSCRIPTNB{n} or \SUBSCRIPTNB{\cmd} and _{nnn}
into \SUBSCRIPT{nn}
1 while s/(?<!\\)_(\s*([^{\\\s]|\\\w+))/\\SUBSCRIPTNB{$1}/g ;
1 while s/(?<!\\)_(\s*{($pat_n)})/\\SUBSCRIPT$1/g ;
# Convert ^n into \SUPERSCRIPTNB{n} and ^{nnn} into \SUPERSCRIPT{nn}
1 while s/(?<!\\)\^(\s*([^{\\\s]|\\\w+))/\\SUPERSCRIPTNB{$1}/g ;
1 while s/(?<!\\)\^(\s*{($pat_n)})/\\SUPERSCRIPT$1/g ;
# Convert \sqrt{n} into \SQRT{n} and \sqrt nn into SQRTNB{nn}
1 while s/(?<!\\)\\sqrt(\s*([^{\\\s]|\\\w+))/\\SQRTNB{$1}/g ;
1 while s/(?<!\\)\\sqrt(\s*{($pat_n)})/\\SQRT$1/g ;
# Convert $$ $$ into \begin{DOLLARDOLLAR} \end{DOLLARDOLLAR}
s/\$\$(.*?)\$\$/\\begin{DOLLARDOLLAR}$1\\end{DOLLARDOLLAR}/sg;
# Convert \[ \] into \begin{SQUAREBRACKET} \end{SQUAREBRACKET}
s/(?<!\\)\\\[/\\begin{SQUAREBRACKET}/sg;
s/\\\]/\\end{SQUAREBRACKET}/sg;
# Convert all picture environmentent (\begin{PICTUREENV} .. \end{PICTUREENV}
\PICTUREBLOCKenv
s/\\begin\{($PICTUREENV)}(.*?)\\end\{\1}/\\PICTUREBLOCK$1\{$2\}/sg;
# For math-mode COARSE,WHOLE or NONE option -convert all \begin{MATH} ..
\end{MATH}
# into \MATHBLOCKMATH{...} commands, where MATH is any valid math
environment
# Also convert all array environments into ARRAYBLOCK environments
if ( $mathmarkup != FINE ) {
# DIFANCHORARRB and DIFANCHORARRE, DIFANCHORMATHB and DIFANCHORMATHE markers
are inserted here to encourage the matching algorithm
# to always match up the closing brace. Otherwise sometimes one ends up with
a situation where
# the closing brace is deleted and added at another point. The deleted
closing brace is then
# prevented by a %DIFDELCMD, leading to material leaking in or out of the
math environment.
# The anchors are removed in post-processing again. (note that they are
simple text to cause least amount of complications
# Admittedly, this is something of a hack and will not always work. If it
does not, then one needs to
# resort to WHOLE or FINE, or NONE math mode processing.
s/\\begin\{($ARRENV)}(.*?)\\end\{\1}/\\ARRAYBLOCK$1\
{$2\\DIFANCHORARRB \}\\DIFANCHORARRE /sg;
take_comments_and_enter_from_frac();
s/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)\}(.*?)\\end\
{\1\}/\\MATHBLOCK$1\{$2\\DIFANCHORMATHB \}\\DIFANCHORMATHE /sg;
}
# $expanded=linecomment($string)
#preface all lines with verbatim marker (usually DIFVRB)
sub linecomment {
my @verbatimlines=split("\n",$_[0]);
# the first line needs special treatment - we do want to retain optional
arguments as is but wrap the remainder also with VERBCOMMENT
### print STDERR "DEBUG: before verbatimlines[0] = ",$verbatimlines[0],"\n";
$verbatimlines[0]=~s/^((?:\s*\[$brat_n\])?\s*)([^\s\[].*)/ defined($2) ? ( "$1\%
$VERBCOMMENT$2" ) : ( $1 )/e;
### print STDERR "DEBUG: after verbatimlines[0] = ",$verbatimlines[0],"\n";
return(join("\n%$VERBCOMMENT",@verbatimlines)."\n");
}
# $simple=reverselinecomment($env $string)
# remove DIFVRB comments but leave changed lines marked
sub reverselinecomment {
my ($environment, $verbatimtext)=@_;
###print STDERR "OLD VERBATIMTEXT: |$verbatimtext|\n";
# remove markup added by latexdiff
# (this should occur only if the type of verbatim environment was changed)
# (note that this destroys some information in old file)
# in theory I could save it by moving it out of the verbatim environment
# but this requires more bookkeeping and is probably not necessary)
$verbatimtext =~ s/\\DIFaddbegin //g;
$verbatimtext =~ s/\\DIFaddend //g;
$verbatimtext =~ s/\\DIFdelbegin //g;
$verbatimtext =~ s/\\DIFdelend //g;
$verbatimtext =~ s/$DELCMDOPEN.*//g;
#hashstring=tohash(\%hash,$string)
# creates a hash value based on string and stores in %hash
sub tohash {
my ($hash,$string)=@_;
my (@arr,$val);
my ($sum,$i)=(0,1);
my ($hstr);
@arr=unpack('c*',$string);
while (1) {
foreach $val (@arr) {
$sum += $i*$val;
$i++;
}
$hstr= "$sum";
last unless (defined($hash->{$hstr}) && $string ne $hash->{$hstr});
# else found a duplicate HASH need to repeat for a higher hash value
}
$hash->{$hstr}=$string;
### print STDERR "Hash:$hstr: Content:$string:\n";
return($hstr);
}
#string=fromhash(\%hash,$fromstring)
# restores string value stored in hash
#string=fromhash(\%hash,$fromstring,$prependstring)
# additionally begins each line with prependstring
sub fromhash {
my ($hash,$hstr)=($_[0],$_[1]);
my $retstr=$hash->{$hstr};
if ( $#_ >= 2) {
$retstr =~ s/^/$_[2]/mg;
}
return $retstr;
}
# writedebugfile(string, label)
# if $debug set writes <string> to file latexdiff.debug.<label>
# otherwise do nothing
sub writedebugfile {
my ($string,$label)=@_;
if ( $debug ) {
open(RAWDIFF,">","latexdiff.debug." . $label);
print RAWDIFF $string;
close(RAWDIFF);
}
}
# postprocess($string, ..)
# carry out the following post-processing steps for all arguments:
# * Remove STOP token from the end
# * Replace \RIGHTBRACE by }
# * change citation commands within comments to protect from processing (using
marker CITEDIF)
# 1. Check all deleted blocks:
# a.where a deleted block contains a matching \begin and
# \end environment (these will be disabled by a %DIFDELCMD statements), for
selected environments enable
# these commands again (such that for example displayed math in a deleted
equation
# is properly within math mode. For math mode environments replace numbered
equation
# environments with their display only variety (so that equation numbers in
new file and
# diff file are identical). Where the correct type of math environment cannot
be determined
# use a place holder MATHMODE
# b.where one of the commands matching $COUNTERCMD is used as a DIFAUXCMD, add a
statement
# subtracting one from the respective counter to keep numbering consistent
with new file
# Replace all MATHMODE environment commands by the correct environment to
achieve matching
# pairs
# c. Convert MATHBLOCKmath commands to their uncounted numbers (e.g. convert
equation -> displaymath
# (environments defined in $MATHENV will be replaced by $MATHREPL, and
environments in $MATHARRENV
# will be replaced by $MATHARRREPL
# d. If in-line math mode contains array environment, enclose the whole
environment in \mbox'es
# d. place \cite commands in mbox'es (for UNDERLINE style)
#
# For added blocks:
# c. If in-line math mode contains array environment, enclose the whole
environment in \mbox'es
# d. place \cite commands in mbox'es (for UNDERLINE style)
#
# 2. If math-mode COARSE,WHOLE or NONE option set: Convert \MATHBLOCKmath{..}
commands back to environments
#
# Convert all PICTUREblock{..} commands back to the appropriate environments
# 3. Convert DIFadd, DIFdel, DIFaddbegin , ... into FL varieties
# within floats (currently recognised float environments: plate,table,figure
# plus starred varieties).
# 4. Remove empty %DIFDELCMD < lines
# 4. Convert \begin{SQUAREBRACKET} \end{SQUAREBRACKET} into \[ \]
# Convert \begin{DOLLARDOLLAR} \end{DOLLARDOLLAR} into $$ $$
# 5. Convert \SUPERSCRIPTNB{n} into ^n and \SUPERSCRIPT{nn} into ^{nnn}
# 6. Convert \SUBSCRIPTNB{n} into _n and \SUBCRIPT{nn} into _{nnn}
# 7. Expand hashes of verb and verbatim environments
# 8. Convert '\PERCENTAGE ' back into '\%' and '\DOLLAR ' into '\$'
# 9.. remove all \PAR tokens
# 10. package specific processing: endfloat: make sure \begin{figure} and
\end{figure} are always
# on a line by themselves, similarly for table environment
# 4, undo renaming of the \begin, \end,{,} in comments
# Change \QLEFTBRACE, \QRIGHTBRACE,\AMPERSAND to \{,\},\&
#
# Note have to manually synchronize substitution commands below and
# DIF.. command names in the header
sub postprocess {
my ($begin,$len,$cnt,$float,$delblock,$addblock);
# second level blocks
my ($begin2,$cnt2,$len2,$eqarrayblock,$mathblock);
my (@textparts,@newtextparts,@liststack,$listtype,$listlast);
my (@itemargs, $itemarg);
for (@_) {
# change $'s in comments to something harmless
1 while s/(%.*)\$/$1DOLLARDIF/mg ;
# Check all deleted blocks: where a deleted block contains a matching \begin
and
# \end environment (these will be disabled by a %DIFDELCMD statements),
enable
# these commands again (such that for example displayed math in a deleted
equation
# is properly within math mode). For math mode environments replace
numbered equation
# environments with their display only variety (so that equation numbers in
new file and
# diff file are identical)
while ( m/\\DIFdelbegin.*?\\DIFdelend/sg ) {
### while ( m/\\DIFdelbegin.*?\\DIFdelend/sg ) {
### print STDERR "DEBUG Match delblock \n||||$&||||\n at ",pos,"\n";
$cnt=0;
$len=length($&);
$begin=pos($_) - $len;
$delblock=$&;
### A much simpler method for math replacement might follow this strategy
(can recycle part of the commands below for following
### this strategy:
### 1. a Insert aux commands \begin{MATHMODE} or \end{MATHMODE} for all
deleted commands opening or closing displayed math mode
### b Insert aux commands \begin{MATHARRMODE} or \end{MATHARRMODE} for
all deleted commands opening or closing math array mode
### 2 Replace MATHMODE and MATHARRMODE by correct pairing if appropriate
partner math command is found in text
### 3 a Replace remaining \begin{MATHMODE}...\end{MATHMODE} pairs with
\begin{$MATHREPL}..\end{$MATHREPL}
### b Replace remaining \begin{MATHARRMODE}...\end{MATHARRMODE} pairs
with \begin{$MATHREPL}..\end{$MATHREPL}
### 4 Delete all aux command math mode pairs which have simply comments or
empty lines between them
### As written this won't actually work!
### pre-0.42 # same as above for special case \[.\] (latex abbreviation
for displaymath)
### pre-0.42 $delblock=~ s/(\%DIFDELCMD < \s*\\\[\s*?\n())(.*?
[^\n]?)\n?(\%DIFDELCMD < \s*\\\])/$1\\\[$AUXCMD\n$3\n\\\]$AUXCMD\n$4/sg;
### pre-0.42 $delblock=~ s/(?<!${AUXCMD}\n)(\%DIFDELCMD <
\s*\\\]\s*?\n())(.*?[^\n]?)\n?(?<!${AUXCMD}\n)(\%DIFDELCMD < \s*\\\[)/
$1\\\]$AUXCMD\n$3\n\\\[$AUXCMD\n$4/sg;
# equation array environment
###pre-0.3 $delblock=~ s/(\%DIFDELCMD < \s*\\begin\
{($MATHARRENV)\}\s*?\n)(.*?)(\%DIFDELCMD < \s*\\end\{\2\})/$1\\begin{$MATHARRREPL}
$AUXCMD\n$3\n\\end{$MATHARRREPL}$AUXCMD\n$4/sg;
###0.5 $delblock=~ s/(\%DIFDELCMD < \s*\\begin\{($MATHARRENV)\}\s*?(?:
$DELCMDCLOSE|\n))(.*?[^\n]?)\n?(\%DIFDELCMD < \s*\\end\{\2\})/\\begin{$MATHARRREPL}
$AUXCMD\n$1$3\n\\end{$MATHARRREPL}$AUXCMD\n$4/sg;
$delblock=~ s/(\%DIFDELCMD < \s*\\begin\{($MATHARRENV)\}.*?(?:
$DELCMDCLOSE|\n))(.*?[^\n]?)\n?(\%DIFDELCMD < \s*\\end\{\2\})/\\begin{$MATHARRREPL}
$AUXCMD\n$1$3\n\\end{$MATHARRREPL}$AUXCMD\n$4/sg;
### pre-0.42 obsolete version which did not work on eqnarray test
$delblock=~ s/(?<!${AUXCMD}\n)(\%DIFDELCMD < \s*\\end\{($MATHARRENV)\}\s*?\n)(.*?
[^\n]?)\n?(?<!${AUXCMD}\n)(\%DIFDELCMD < \s*\\begin\{\2\})/$1\\end{$MATHARRREPL}
$AUXCMD\n$3\n\\begin{$MATHARRREPL}$AUXCMD\n$4/sg;
$delblock=~ s/(?<!${AUXCMD}\n)(\%DIFDELCMD < \s*\\end\{($MATHARRENV)\}\s*?
(?:$DELCMDCLOSE|\n))(.*?[^\n]?)\n?(?<!${AUXCMD}\n)(\%DIFDELCMD < \s*\\begin\
{\2\})/\\end{MATHMODE}$AUXCMD\n$1$3\n\\begin{MATHMODE}$AUXCMD\n$4/sg;
substr($delblock,$begin2,$len2)=$eqarrayblock;
pos($delblock) = $begin2 + length($eqarrayblock);
}
} elsif ( $mathmarkup == COARSE || $mathmarkup == WHOLE ) {
# Convert MATHBLOCKmath commands to their uncounted numbers (e.g.
convert equation -> displaymath
# (environments defined in $MATHENV will be replaced by $MATHREPL, and
environments in $MATHARRENV
# will be replaced by $MATHARRREPL
$delblock=~ s/\\MATHBLOCK($MATHENV)\{($pat_n)\}/\\MATHBLOCK$MATHREPL\
{$2\}/sg;
$delblock=~ s/\\MATHBLOCK($MATHARRENV)\{($pat_n)\}/\\MATHBLOCK$MATHARRREPL\
{$2\}/sg;
}
# Reinstate completely deleted list environments. note that items within the
# environment will still be commented out. They will be restored later
$delblock=~ s/(\%DIFDELCMD < \s*\\begin\{($LISTENV)\}\s*?(?:\n|$DELCMDCLOSE))
(.*?)(\%DIFDELCMD < \s*\\end\{\2\})/{
}/esg;
# bb. disable active labels within deleted blocks (i.e. those not
commented out) (as these are not safe commands, this should normally only
# happen within deleted maths blocks
### $delblock=~ s/(?<!$DELCMDOPEN)(\\$LABELCMD(?:${extraspace})\{(?:
[^{}])*\}[\t ]*)\n?/${DELCMDOPEN}$1${DELCMDCLOSE}/smg ;
### previous line caused trouble as by issue #90 I might need to modify
this
$delblock=~ s/^([^%]*)(\\$LABELCMD(?:${extraspace})\{(?:[^{}])*\}[\t ]*)\n?/
$1${DELCMDOPEN}$2${DELCMDCLOSE}/smg ;
### print STDERR "<<<$delblock>>>\n" if $debug;
# Go through whole text, and by counting list environment commands, find out
when we are within a list environment.
# Within those restore deleted \item commands
@textparts=split /(?<!$DELCMDOPEN)(\\(?:begin|end)\{$LISTENV\})/ ;
@liststack=();
@newtextparts=map {
### print STDERR ":::::::: $_\n";
if ( ($listtype) = m/^\\begin\{($LISTENV)\}$/ ) {
print STDERR "DEBUG: postprocess \\begin{$listtype}\n" if $debug;
push @liststack,$listtype;
} elsif ( ($listtype) = m/^\\end\{($LISTENV)\}$/ ) {
print STDERR "DEBUG: postprocess \\end{$listtype}\n" if $debug;
if (scalar @liststack > 0) {
$listlast=pop(@liststack);
($listtype eq $listlast) or warn "Invalid nesting of list environments:
$listlast environment closed by \\end{$listtype}.";
} else {
warn "WARNING: Invalid nesting of list environments: \\end{$listtype}
encountered without matching \\begin{$listtype}.\n";
}
} else {
print STDERR "DEBUG: postprocess \@liststack=(",join(",",@liststack),")\n" if
$debug;
if (scalar @liststack > 0 ) {
# we are within a list environment and should replace all item commands
$_=restore_item_commands($_);
}
# else: we are outside a list environment and do not need to do anything
}
$_ } @textparts; # end of map command
# replace the main text with the modified version
$_= join("",@newtextparts);
# The next line is complicated. The negative look-ahead insertion makes sure
that no \end{$MATHENV} (or other mathematical
# environments) are between the \begin{$MATHENV} and \end{MATHMODE} commands.
This is necessary as the minimal matching
# is not globally minimal but only 'locally' (matching is beginning from the
left side of the string)
if ( $mathmarkup == FINE ) {
1 while s/\\begin\{((?:$MATHENV)|(?:$MATHARRENV)|SQUAREBRACKET)}((?:.(?!
(?:\\end\{(?:(?:$MATHENV)|(?:$MATHARRENV)|SQUAREBRACKET)}|\\begin\
{MATHMODE})))*?)\\end\{MATHMODE}/\\begin{$1}$2\\end{$1}/s;
1 while s/\\begin\{MATHMODE}((?:.(?!\\end\{MATHMODE}))*?)\\end\{((?:
$MATHENV)|(?:$MATHARRENV)|SQUAREBRACKET)}/\\begin{$2}$1\\end{$2}/s;
# convert remaining \begin{MATHMODE} \end{MATHMODE} (and not containing &
or \\ )into MATHREPL environments
s/\\begin\{MATHMODE\}((?:(.(?!(?<!\\)\&|\\\\))*)?)\\end\
{MATHMODE\}/\\begin{$MATHREPL}$1\\end{$MATHREPL}/sg;
# others into MATHARRREPL
s/\\begin\{MATHMODE\}(.*?)\\end\{MATHMODE\}/\\begin{$MATHARRREPL}
$1\\end{$MATHARRREPL}/sg;
# now look for AUXCMD math-mode pairs which have only comments (or empty
lines between them), and remove the added commands
s/\\begin\{((?:$MATHENV)|(?:$MATHARRENV)|SQUAREBRACKET)\}$AUXCMD\n((?:\s*%.
[^\n]*\n)*)\\end\{\1\}$AUXCMD\n/$2/sg;
} else {
# math modes OFF,WHOLE,COARSE: Convert \MATHBLOCKmath{..} commands back to
environments
s/\\MATHBLOCK($MATHENV|$MATHARRENV|SQUAREBRACKET)\
{($pat_n)\}/\\begin{$1}$2\\end{$1}/sg;
# convert ARRAYBLOCK.. commands back to environments
s/\\ARRAYBLOCK($ARRENV)\{($pat_n)\}/\\begin{$1}$2\\end{$1}/sg;
# get rid of the DIFANCHOR markers, first the delete comments, then
everywhere
s/%DIFDELCMD < \\DIFANCHOR(?:MATH|ARR)[BE] (?:\n%DIFDELCMD < )?%%%\n//g ;
s/\\DIFANCHOR(?:MATH|ARR)[BE] //g;
}
# Expand hashes of verb and verbatim environments (note negative look behind
assertion to not leak out of DIFDELCMD comments
s/${DELCMDOPEN}\\($VERBATIMENV)\{([-\d]*?)\}/"$
{DELCMDOPEN}\\begin{${1}}".fromhash(\%verbhash,$2,$DELCMDOPEN)."$
{DELCMDOPEN}\\end{${1}}"/esg;
# revert changes to verbatim environments for line diffs (and add code to mark
up changes)
s/(?<!$DELCMDOPEN)\\begin\{($VERBATIMLINEENV)\}(.*?)\\end\{\1\}/"".
reverselinecomment($1, $2) .""/esg;
# # we do the same for deleted environments but additionally reinstate the
framing commands
# s/$DELCMDOPEN\\begin\{($VERBATIMLINEENV)\}$extraspace(?:\[$brat0\])?
$DELCMDCLOSE(.*?)$DELCMDOPEN\\end\{\1\}$DELCMDCLOSE/"\\begin{$1}".
reverselinecomment($2) . "\\end{$1}"/esg;
## s/$DELCMDOPEN\\begin\{($VERBATIMLINEENV)\}($extraspace(?:\[$brat0\])?\s*)
(?:\n|$DELCMDOPEN)*$DELCMDCLOSE((?:\%$DELCOMMENT$VERBCOMMENT.*?\n)*)
($DELCMDOPEN\\end\{\1\}(?:\n|\s|$DELCMDOPEN)*$DELCMDCLOSE)/"SUBSTITUTION:
\\begin{$1}$2 INTERIOR: |$3| END: |$4|"/esg;
s/ # Deleted \begin command of verbatim environment (Captures $1: whole
deleted command, $2: environment, $3: optional arguments with white space
(\Q$DELCMDOPEN\E\\begin\{($VERBATIMLINEENV)\}(\Q$extraspace\E(?:\
[$brat_n\])?\s*)(?:\n|\Q$DELCMDOPEN\E)*\Q$DELCMDCLOSE\E)
# Interior of deleted verbatim environment should consist entirely of
delete DIFVRB comments, i.e. match only lines beginning with % DIF < DIFVRB
# Captures: $4: all lines combined
((?:\%\Q$DELCOMMENT$VERBCOMMENT\E[^\n]*?\n)*)
# Deleted \end command of verbatim environment. Note that the type is
forced to match the opening. Captures: $5: Whole deleted environment (previous way
this line was written: (\Q$DELCMDOPEN\E\\end\{\2\}
(?:\n|\s|\Q$DELCMDOPEN\E)*\Q$DELCMDCLOSE\E)
(\Q$DELCMDOPEN\E\\end\{\2\})
/ # Substitution part
$1 # Leave expression as is
. "$AUXCMD NEXT\n" # Mark the following line as an auxiliary command
. "" # reinstate the original environment without options
. reverselinecomment($2, "$3$4") # modify the body to change the
markup; reverselinecomment parses for options
. " $AUXCMD\n" # close the auxiliary environment
. $5 # and again leave the original deleted closing
environment as is
/esgx; # Modifiers of substitution command
# where changes have occurred in verbatim environment, change verbatim to
DIFverbatim to allow mark-up
# (I use the presence of optional paramater to verbatim environment as the
marker - normal verbatim
# environment does not take optional arguments)
s/(?<!$DELCMDOPEN)\\begin\{(verbatim[*]?)\}(\[$brat_n\].*?)\\end\
{\1\}/\\begin{DIF$1}$2\\end{DIF$1}/sg;
s/\\($VERBATIMENV)\{([-\d]*?)\}/"\\begin{${1}}".fromhash(\%verbhash,
$2)."\\end{${1}}"/esg;
# remove all \PAR tokens (taking care to properly keep commented out PAR's
# from introducing uncommented newlines - next line)
s/(%DIF < )([^\n]*?)\\PAR\n/$1$2\n$1\n/sg;
# convert PAR commands which are on a line by themselves
s/\n(\s*?)\\PAR\n/\n\n/sg;
# convert remaining PAR commands (which are preceded by non-white space
characters, usually "}" ($ADDCLOSE)
s/\\PAR\n/\n\n/sg;
# Convert '\PERCENTAGE ' back into '\%' (the final question mark catches a
special situation where due to a latter pre-processing step the ' ' becomes
separated
s/\\PERCENTAGE ?/\\%/g;
# Convert '\DOLLAR ' back into '\$'
s/\\DOLLAR /\\\$/g;
return;
}
}
# $out = restore_item_commands($listenviron)
# short helper function for post-process, which restores deleted \item commands in
its argument (as DIFAUXCMDs)
sub restore_item_commands {
my ($string)=@_ ;
my ($itemarg,@itemargs);
$string =~ s/(\%DIFDELCMD < \s*(\\$ITEMCMD$extraspace)((?:<$abrat0>)?$extraspace)
((?:\[$brat_n\])?)\s*((?:${cmdoptseq}\s*?)*)(?:\n|$DELCMDCLOSE))/
# if \item has an []argument, then mark up the argument as deleted)
if (length($4)>0) {
# use substr to exclude square brackets at end points
@itemargs=splitlatex(substr($4,1,length($4)-2));
$itemarg="[".join("",marktags("","",$DELOPEN,$DELCLOSE,$DELCMDOPEN,
$DELCMDCLOSE,$DELCOMMENT,\@itemargs))."]";
} else {
$itemarg="";
}
"$1$2$3$itemarg$AUXCMD\n"; ###.((length($5)>0) ? "%DIFDELCMD $5
$DELCMDCLOSE\n" : "")
/sge;
return($string);
}
# @auxlines=preprocess_preamble($oldpreamble,$newpreamble);
# pre-process preamble by looking for commands used in \maketitle (title, author,
date etc commands)
# the list of commands is defined in CONTEXT2CMD
# if found then use a bodydiff to mark up content, and replace the corresponding
commands
# in both preambles by marked up version to 'fool' the linediff (such that only
body is marked
# up.
# A special case are e.g. author commands being added (or removed)
# 1. If commands are added, then the entire content is marked up as new, but also
the lines are marked as new in the linediff
# 2. If commands are removed, then the linediff will mark the line as deleted.
The program returns
# with $auxlines a text to be appended at the end of the preamble, which shows
the respective fields as deleted
sub preprocess_preamble {
my ($oldpreambleref,$newpreambleref)=(\$_[0],\$_[1]) ;
my @auxlines=();
# Remember to use $$oldpreambleref to refer to oldpreamble
my ($titlecmd,$titlecmdpat);
my (@oldtitlecommands,@newtitlecommands );
my %oldhash = ();
my %newhash = ();
my ($line,$cmd,$optarg,$arg,$optargnew,$optargold,$optargdiff,$argold,$argnew,
$argdiff,$auxline);
my $warnmsgdetail = <<EOF ;
This should not occur for standard styles, but can occur for some specifiy
styles, document classes,
e.g. journal house styles.
Workaround: Use --replace-context2cmd option to specifically set those
commands, which are not repeated.
EOF
while ( @oldtitlecommands ) {
$line=shift @oldtitlecommands;
$cmd=shift @oldtitlecommands;
$optarg=shift @oldtitlecommands;
$arg=shift @oldtitlecommands;
if ( defined($oldhash{$cmd})) {
warn "WARNING: $cmd is used twice in preamble of old file. Reverting to pure
line diff mode for preamble.\n";
print STDERR $warnmsgdetail;
return;
}
$oldhash{$cmd}=[ $line, $optarg, $arg ];
}
while ( @newtitlecommands ) {
$line=shift @newtitlecommands;
$cmd=shift @newtitlecommands;
$optarg=shift @newtitlecommands;
$arg=shift @newtitlecommands;
if ( defined($newhash{$cmd})) {
warn "$cmd is used twice in preamble of new file. Reverting to pure line diff
mode for preamble.\n";
print STDERR $warnmsgdetail;
return;
}
$newhash{$cmd}=[ $line, $optarg, $arg ];
}
foreach $cmd ( keys %newhash ) {
if ( defined($newhash{$cmd}->[1])) {
$optargnew=$newhash{$cmd}->[1];
} else {
$optargnew="";
}
if ( defined($oldhash{$cmd}->[1])) {
$optargold=$oldhash{$cmd}->[1];
} else {
$optargold="";
}
if ( defined($oldhash{$cmd}->[2]) ) {
$argold=$oldhash{$cmd}->[2];
} else {
$argold="";
}
$argnew=$newhash{$cmd}->[2];
$argdiff="{" . join("",bodydiff($argold,$argnew)) ."}";
if ( length $optargnew ) {
$optargdiff="[".join("",bodydiff($optargold,$optargnew))."]" ;
$optargdiff =~ s/\\DIFaddbegin /\\DIFaddbeginFL /g;
$optargdiff =~ s/\\DIFaddend /\\DIFaddendFL /g;
$optargdiff =~ s/\\DIFadd\{/\\DIFaddFL{/g;
$optargdiff =~ s/\\DIFdelbegin /\\DIFdelbeginFL /g;
$optargdiff =~ s/\\DIFdelend /\\DIFdelendFL /g;
$optargdiff =~ s/\\DIFdel\{/\\DIFdelFL{/g;
} else {
$optargdiff="";
}
### print STDERR "DEBUG s/\\Q$newhash{$cmd}->[0]\\E/\\
$cmd$optargdiff$argdiff/s\n";
# Note: \Q and \E force literal interpretation of what it between them but
allow
# variable interpolation, such that e.g. \title matches just that and not
TAB-itle
$$newpreambleref=~s/\Q$newhash{$cmd}->[0]\E/\\$cmd$optargdiff$argdiff/s;
# replace this in old preamble if necessary
if ( defined($oldhash{$cmd}->[0])) {
$$oldpreambleref=~s/\Q$oldhash{$cmd}->[0]\E/\\$cmd$optargdiff$argdiff/s ;
}
### print STDERR "DEBUG NEW PRE ".$$newpreambleref."\n";
}
# @diffs=linediff(\@seq1, \@seq2)
# mark up lines like this
#%DIF mm-mmdnn
#%< old deleted line(s)
#%DIF -------
#%DIF mmann-nn
#new appended line %<
#%DIF -------
# Future extension: mark change explicitly
# Assumes: traverse_sequence traverses deletions before insertions in changed
sequences
# all line numbers relative to line 0 (first line of real file)
sub linediff {
my $seq1 = shift ;
my $seq2 = shift ;
my $block = [];
my $retseq = [];
my @begin=('','',''); # dummy initialisation
my $instring ;
# init_regex_arr_data(\@array,"TOKEN INIT")
# scans DATA file handel for line "%% TOKEN INIT" line
# then appends each line not beginning with % into array (as a quoted regex)
# This is used for command lists and configuration variables, but the processing is
slightly
# different:
# For lists, the regular expression is extended to include beginning (^) and end
($) markers, to require full-string matching
# For configuration variables (and all others), simply an unadorned list is copied
sub init_regex_arr_data {
my ($arr,$token)=@_;
my $copy=0;
my ($mode);
if ($token =~ m/COMMANDS/ ) {
$mode=0; # Reading command list
} else {
$mode=1; # Reading configuration variables
}
while (<DATA>) {
if ( m/^%%BEGIN $token\s*$/ ) {
$copy=1;
next;
} elsif ( m/^%%END $token\s*$/ ) {
last; }
chomp;
if ( $mode==0 ) {
# print STDERR "DEBUG init_regex_arr_data regex >$_<\n" if ($debug && $copy);
push (@$arr,qr/^$_$/) if ( $copy && !/^%/ ) ;
} elsif ($mode==1) {
push (@$arr,"$_") if ( $copy && !/^%/ ) ;
}
}
seek DATA,0,0; # rewind DATA handle to file begin
}
# init_regex_arr_ext(\@array,$arg)
# appends array with regular expressions.
# if arg is a file name, then read in list of regular expressions from that file
# (one expression per line)
# Otherwise treat arg as a comma separated list of regular expressions
sub init_regex_arr_ext {
my ($arr,$arg)=@_;
if ( -f $arg ) {
init_regex_arr_file($arr,$arg);
} else {
init_regex_arr_list($arr,$arg);
}
}
# init_regex_arr_file(\@array,$fname)
# appends array with regular expressions.
# Read in list of regular expressions from $fname
# (one expression per line)
sub init_regex_arr_file {
my ($arr,$fname)=@_;
open(FILE,"$fname") or die ("Couldn't open $fname: $!");
while (<FILE>) {
chomp;
next if /^\s*#/ || /^\s*%/ || /^\s*$/ ;
push (@$arr,qr/^$_$/);
}
close(FILE);
}
# init_regex_arr_list(\@array,$arg)
# appends array with regular expressions.
# read from comma separated list of regular expressions ($arg)
sub init_regex_arr_list {
my ($arr,$arg)=@_;
my $regex;
### print STDERR "DEBUG init_regex_arr_list arg >$arg<\n" if $debug;
foreach $regex (split(qr/(?<!\\),/,$arg)) {
$regex =~ s/\\,/,/g;
print STDERR "DEBUG init_regex_arr_list regex >$regex<\n" if $debug;
push (@$arr,qr/^$regex$/);
}
}
sub usage {
die <<"EOF";
Usage: $0 [options] old.tex new.tex > diff.tex
Compares two latex files and writes tex code to stdout, which has the same format
as new.tex but
has all changes relative to old.tex marked up or commented. Note that old.tex and
new.tex need to
be real files (not pipes or similar) as they are opened twice.
--type=markupstyle
-t markupstyle Add code to preamble for selected markup style
Available styles: UNDERLINE CTRADITIONAL TRADITIONAL CFONT
FONTSTRIKE INVISIBLE
CHANGEBAR CCHANGEBAR CULINECHBAR
CFONTCHBAR BOLD PDFCOMMENT
[ Default: UNDERLINE ]
--subtype=markstyle
-s markstyle Add code to preamble for selected style for bracketing
commands (e.g. to mark changes in margin)
Available styles: SAFE MARGIN DVIPSCOL COLOR ZLABEL
ONLYCHANGEDPAGE (LABEL)*
[ Default: SAFE ]
* LABEL subtype is deprecated
--floattype=markstyle
-f markstyle Add code to preamble for selected style which
replace standard marking and markup commands within floats
(e.g., marginal remarks cause an error within floats
so marginal marking can be disabled thus)
Available styles: FLOATSAFE IDENTICAL
[ Default: FLOATSAFE ]
--encoding=enc
-e enc Specify encoding of old.tex and new.tex. Typical encodings
are
ascii, utf8, latin1, latin9. A list of available encodings
can be
obtained by executing
perl -MEncode -e 'print join ("\\n",Encode-
>encodings( ":all" )) ;'
[Default encoding is utf8 unless the first few lines of the
preamble contain
an invocation "\\usepackage[..]{inputenc} in which case the
encoding chosen by this command is asssumed. Note that ASCII
(standard
latex) is a subset of utf8]
--preamble=file
-p file Insert file at end of preamble instead of auto-generating
preamble. The preamble must define the following commands
\\DIFaddbegin,\\DIFaddend,\\DIFadd{..},
\\DIFdelbegin,\\DIFdelend,\\DIFdel{..},
and varieties for use within floats
\\DIFaddbeginFL,\\DIFaddendFL,\\DIFaddFL{..},
\\DIFdelbeginFL,\\DIFdelendFL,\\DIFdelFL{..}
(If this option is set -t, -s, and -f options
are ignored.)
--exclude-safecmd=exclude-file
--exclude-safecmd="cmd1,cmd2,..."
-A exclude-file
--replace-safecmd=replace-file
--append-safecmd=append-file
--append-safecmd="cmd1,cmd2,..."
-a append-file Exclude from, replace or append to the list of regex
matching commands which are safe to use within the
scope of a \\DIFadd or \\DIFdel command. The file must
contain
one Perl-RegEx per line (Comment lines beginning with # or %
are
ignored). A literal comma within the comma-separated list
must be
escaped thus "\\,", Note that the RegEx needs to match the
whole of
the token, i.e., /^regex\$/ is implied and that the initial
"\\" of the command is not included. The --exclude-safecmd
and --append-safecmd options can be combined with the
--replace-safecmd
option and can be used repeatedly to add cumulatively to the
lists.
--exclude-textcmd=exclude-file
--exclude-textcmd="cmd1,cmd2,..."
-X exclude-file
--replace-textcmd=replace-file
--append-textcmd=append-file
--append-textcmd="cmd1,cmd2,..."
-x append-file Exclude from, replace or append to the list of regex
matching commands whose last argument is text. See
entry for --exclude-safecmd directly above for further
details.
--replace-context1cmd=replace-file
--append-context1cmd=append-file
--append-context1cmd="cmd1,cmd2,..."
Replace or append to the list of regex matching commands
whose last argument is text but which require a particular
context to work, e.g. \\caption will only work within a
figure
or table. These commands behave like text commands, except
when
they occur in a deleted section, when they are disabled, but
their
argument is shown as deleted text.
--replace-context2cmd=replace-file
--append-context2cmd=append-file
--append-context2cmd="cmd1,cmd2,..."
As corresponding commands for context1. The only difference
is that
context2 commands are completely disabled in deleted
sections, including
their arguments.
context2 commands are also the only commands in the
preamble, whose argument will
be processed in word-by-word mode (which only works, if they
occur no more than
once in the preamble).
--exclude-mboxsafecmd=exclude-file
--exclude-mboxsafecmd="cmd1,cmd2,..."
--append-mboxsafecmd=append-file
--append-mboxsafecmd="cmd1,cmd2,..."
Define safe commands, which additionally need to be
protected by encapsulating
in an \\mbox{..}. This is sometimes needed to get around
incompatibilities
between external packages and the ulem package, which is
used for highlighting
in the default style UNDERLINE as well as CULINECHBAR
CFONTSTRIKE
--config var1=val1,var2=val2,...
-c var1=val1,.. Set configuration variables.
-c configfile Available variables:
ARRENV (RegEx)
COUNTERCMD (RegEx)
FLOATENV (RegEx)
ITEMCMD (RegEx)
LISTENV (RegEx)
MATHARRENV (RegEx)
MATHARRREPL (String)
MATHENV (RegEx)
MATHREPL (String)
MINWORDSBLOCK (Integer)
PICTUREENV (RegEx)
SCALEDELGRAPHICS (Float)
VERBATIMENV (RegEx)
VERBATIMLINEENV (RegEx)
CUSTOMDIFCMD (RegEx)
This option can be repeated.
--add-to-config varenv1=pattern1,varenv2=pattern2
For configuration variables containing a regular expression
(essentially those ending
in ENV, and COUNTERCMD) this provides an alternative way to
modify the configuration
variables. Instead of setting the complete pattern, with
this option it is possible to add an
alternative pattern. varenv must be one of the variables
listed above that take a regular
expression as argument, and pattern is any regular
expression (which might need to be
protected from the shell by quotation). Several patterns can
be added at once by using semi-colons
to separate them, e.g. --add-to-config
"LISTENV=myitemize;myenumerate,COUNTERCMD=endnote"
--packages=pkg1,pkg2,..
Tell latexdiff that .tex file is processed with the packages
in list
loaded. This is normally not necessary if the .tex file
includes the
preamble, as the preamble is automatically scanned for
\\usepackage commands.
Use of the --packages option disables automatic scanning, so
if for any
reason package specific parsing needs to be switched off,
use --packages=none.
The following packages trigger special behaviour:
endfloat hyperref amsmath apacite siunitx cleveref
glossaries mhchem chemformula/chemmacros
[ Default: scan the preamble for \\usepackage commands to
determine
loaded packages.]
NB For all --show commands, no old.tex or new.tex file needs to be given, and no
differencing takes place.
--disable-citation-markup
--disable-auto-mbox Suppress citation markup and markup of other vulnerable
commands in styles
using ulem (UNDERLINE,FONTSTRIKE, CULINECHBAR)
(the two options are identical and are simply aliases)
--enable-citation-markup
--enforce-auto-mbox Protect citation commands and other vulnerable commands in
changed sections
with \\mbox command, i.e. use default behaviour for ulem
package for other packages
(the two options are identical and are simply aliases)
Miscelleneous options
--label=label
-L label Sets the labels used to describe the old and new files. The
first use
of this option sets the label describing the old file and
the second
use of the option sets the label for the new file.
[Default: use the filename and modification dates for the
label]
--visible-label Include old and new filenames (or labels set with --label
option) as
visible output
--help
-h Show this help text.
--verbose
-V Output various status information to stderr during
processing.
Default is to work silently.
Internal options:
These options are mostly for automated use by latexdiff-vc. They can be used
directly, but
the API should be considered less stable than for the other options.
=head1 NAME
=head1 SYNOPSIS
=head1 DESCRIPTION
The program treats the preamble differently from the main document.
Differences between the preambles are found using line-based
differencing (similarly to the Unix diff command, but ignoring white
spaces). A comment, "S<C<%DIF E<gt>>>" is appended to each added line, i.e. a
line present in C<new.tex> but not in C<old.tex>. Discarded lines
are deactivated by prepending "S<C<%DIF E<lt>>>". Changed blocks are preceded by
comment lines giving information about line numbers in the original files. Where
there are insignificant
differences, the resulting file C<diff.tex> will be similar to
C<new.tex>. At the end of the preamble, the definitions for I<latexdiff> markup
commands are inserted.
In differencing the main body of the text, I<latexdiff> attempts to
satisfy the following guidelines (in order of priority):
=over 3
=item 1
If both C<old.tex> and C<new.tex> are valid LaTeX, then the resulting
C<diff.tex> should also be valid LateX. (NB If a few plain TeX commands
are used within C<old.tex> or C<new.tex> then C<diff.tex> is not
guaranteed to work but usually will).
=item 2
=item 3
=item 4
=back
and not
or
even though all varieties are the same to LaTeX (but see
B<--allow-spaces> option which allows the second variety).
C<\newenvironment{DIFnomarkup}{}{}>
but the choice is yours. Any markup within the environment will be
removed, and generally everything within the environment will just be
taken from the new file.
=head1 OPTIONS
=head2 Preamble
The following options determine the visual markup style by adding the appropriate
command definitions to the preamble. See the end of this section for a description
of
available styles.
=over 4
=item B<--type=markupstyle> or
B<-t markupstyle>
Add code to preamble for selected markup style. This option defines
C<\DIFadd> and C<\DIFdel> commands.
Available styles:
[ Default: C<UNDERLINE> ]
=item B<--subtype=markstyle> or
B<-s markstyle>
[ Default: C<SAFE> ]
* Subtype C<LABEL> is deprecated
=item B<--floattype=markstyle> or
B<-f markstyle>
[ Default: C<FLOATSAFE> ]
=item B<--encoding=enc> or
B<-e enc>
=item B<--preamble=file> or
B<-p file>
=item B<--packages=pkg1,pkg2,..>
Tell latexdiff that .tex file is processed with the packages in list
loaded. This is normally not necessary if the .tex file includes the
preamble, as the preamble is automatically scanned for C<\usepackage> commands.
Use of the B<--packages> option disables automatic scanning, so if for any
reason package specific parsing needs to be switched off, use B<--packages=none>.
The following packages trigger special behaviour:
=over 8
=item C<amsmath>
=item C<endfloat>
=item C<hyperref>
=item C<apacite>
=item C<siunitx>
=item C<glossaries>
Define most of the glossaries commands as safe, protecting them with \mbox'es where
needed
=item C<mhchem>
Treat C<\ce> as a safe command, i.e. it will be highlighted (note that C<\cee> will
not be highlighted in equations as this leads to processing errors)
Treat C<\ch> as a safe command outside equations, i.e. it will be highlighted (note
that C<\ch> will not be highlighted in equations as this leads to processing
errors)
=back
=item B<--show-preamble>
=back
=head2 Configuration
=over 4
=item B<--exclude-safecmd=exclude-file> or
B<-A exclude-file> or B<--exclude-safecmd="cmd1,cmd2,...">
=item B<--replace-safecmd=replace-file>
=item B<--append-safecmd=append-file> or
B<-a append-file> or B<--append-safecmd="cmd1,cmd2,...">
=item B<--exclude-textcmd=exclude-file> or
B<-X exclude-file> or B<--exclude-textcmd="cmd1,cmd2,...">
=item B<--replace-textcmd=replace-file>
=item B<--append-textcmd=append-file> or
B<-x append-file> or B<--append-textcmd="cmd1,cmd2,...">
=item B<--replace-context1cmd=replace-file>
=item B<--append-context1cmd=append-file> or
=item B<--append-context1cmd="cmd1,cmd2,...">
=item B<--replace-context2cmd=replace-file>
=item B<--append-context2cmd=append-file> or
=item B<--append-context2cmd="cmd1,cmd2,...">
context2 commands are also the only commands in the preamble, whose argument will
be processed in
word-by-word mode (which only works, if they occur no more than once in the
preamble). The algorithm currently cannot cope with repeated context2 commands in
the preamble, as they occur e.g. for the C<\author> argument in some journal styles
(not in the standard styles, though
If such a repetition is detected, the whole preamble will be processed in line-by-
line mode. In such a case, use C<--replace-context2cmd> option to just select the
commands, which should be processed and are not used repeatedly in the preamble.
C<ARRENV> (RegEx)
C<COUNTERCMD> (RegEx)
C<CUSTODIFCMD> (RegEx)
C<FLOATENV> (RegEx)
C<ITEMCMD> (RegEx)
C<LISTENV> (RegEx)
C<MATHARRENV> (RegEx)
C<MATHARRREPL> (String)
C<MATHENV> (RegEx)
C<MATHREPL> (String)
C<MINWORDSBLOCK> (Integer)
C<PICTUREENV> (RegEx)
C<SCALEDELGRAPHICS> (Float)
=item B<--show-safecmd>
=item B<--show-textcmd>
Print list of RegEx matching and excluding commands with text argument.
=item B<--show-config>
=item B<--show-all>
=back
=over 4
=item B<--allow-spaces>
=item B<--math-markup=level>
C<off> or C<0>: suppress markup for math environments. Deleted equations will not
appear in diff file. This mode can be used if all the other modes
cause invalid latex code.
C<fine> or C<3>: Detect small change in equations and mark up at fine granularity.
This mode is most suitable, if only minor changes to equations are
expected, e.g. correction of typos.
=item B<--graphics-markup=level>
C<new-only> or C<1>: surround newly added or changed figures with a blue frame
[Default if graphicx package loaded]
C<both> or C<2>: highlight new figures with a blue frame and show deleted
figures at reduced
scale, and crossed out with a red diagonal cross. Use configuration
variable SCALEDELGRAPHICS to set size of deleted figures.
Note that changes to the optional parameters will make the figure appear as changed
to latexdiff, and this figure will thus be highlighted
=back
=head2 Miscellaneous
=over 4
=item B<--driver=type>
Choose driver for changebar package (only relevant for styles using
changebar: CCHANGEBAR CFONTCHBAR CULINECHBAR CHANGEBAR). Possible
drivers are listed in changebar manual, e.g. pdftex,dvips,dvitops
[Default: dvips]
=item B<--ignore-warnings>
=item B<--label=label> or
B<-L label>
Sets the labels used to describe the old and new files. The first use
of this option sets the label describing the old file and the second
use of the option sets the label for the new file, i.e. set both
labels like this C<-L labelold -L labelnew>.
[Default: use the filename and modification dates for the label]
=item B<--no-label>
Suppress inclusion of old and new file names as comment in output file
=item B<--visible-label>
Include old and new filenames (or labels set with C<--label> option) as
visible output.
=item B<--flatten>
=item B<--help> or
B<-h>
=item B<--version>
=back
These options are mostly for automated use by latexdiff-vc. They can be used
directly, but the API should be considered less stable than for the other options.
=over 4
=item B<--no-links>
The major type determine the markup of plain text and some selected latex commands
outside floats by defining the markup commands C<\DIFadd{...}> and
C<\DIFdel{...}> .
=over 10
=item C<UNDERLINE>
Added text is wavy-underlined and blue, discarded text is struck out and red
(Requires color and ulem packages). Overstriking does not work in displayed math
equations such that deleted parts of equation are underlined, not struck out (this
is a shortcoming inherent to the ulem package).
=item C<CTRADITIONAL>
Added text is blue and set in sans-serif, and a red footnote is created for each
discarded
piece of text. (Requires color package)
=item C<TRADITIONAL>
=item C<CFONT>
Added text is blue and set in sans-serif, and discarded text is red and very small
size.
=item C<FONTSTRIKE>
Added tex is set in sans-serif, discarded text small and struck out
=item C<CCHANGEBAR>
Added text is blue, and discarded text is red. Additionally, the changed text is
marked with a bar in the margin (Requires color and changebar packages).
=item C<CFONTCHBAR>
Like C<CFONT> but with additional changebars (Requires color and changebar
packages).
=item C<CULINECHBAR>
Like C<UNDERLINE> but with additional changebars (Requires color, ulem and
changebar packages).
=item C<CHANGEBAR>
No mark up of text, but mark margins with changebars (Requires changebar package).
=item C<INVISIBLE>
=item C<BOLD>
=item C<PDFCOMMENT>
The pdfcomment package is used to underline new text, and mark deletions with a PDF
comment. Note that this markup might appear differently or not at all based on the
pdf viewer used. The viewer with best support for pdf markup is probably acroread.
This style is only recommended if the number of differences is small.
=back
=head2 Subtypes
The subtype defines the commands that are inserted at the begin and end of added or
discarded blocks, irrespectively of whether these blocks contain text or commands
(Defined commands: C<\DIFaddbegin, \DIFaddend, \DIFdelbegin, \DIFdelend>)
=over 10
=item C<SAFE>
=item C<MARGIN>
Mark beginning and end of changed blocks with symbols in the margin nearby (using
the standard C<\marginpar> command - note that this sometimes moves somewhat
from the intended position.
=item C<COLOR>
An alternative way of marking added passages in blue, and deleted ones in red.
(It is recommeneded to use instead the main types to effect colored markup,
although in some cases coloring with dvipscol can be more complete, for example
with citation commands).
=item C<DVIPSCOL>
An alternative way of marking added passages in blue, and deleted ones in red. Note
that C<DVIPSCOL> only works with the dvips converter, e.g. not pdflatex.
(it is recommeneded to use instead the main types to effect colored markup,
although in some cases coloring with dvipscol can be more complete).
=item C<ZLABEL>
=item C<ONLYCHANGEDPAGE>
also highlights changed pages, without the need for post-processing, but might not
work reliably if
there is floating material (figures, tables).
=item C<LABEL>
is similar to C<ZLABEL>, but does not need the zref package and works less reliably
(deprecated).
=back
Some of the markup used in the main text might cause problems when used within
floats (e.g. figures or tables). For this reason alternative versions of all
markup commands are used within floats. The float type defines these alternative
commands.
=over 10
=item C<FLOATSAFE>
Use identical markup for text as in the main body, but set all commands marking the
begin and end of changed blocks to null-commands. You have to choose this float
type if your subtype is C<MARGIN> as C<\marginpar> does not work properly within
floats.
=item C<TRADITIONALSAFE>
Mark additions the same way as in the main text. Deleted environments are marked
by angular brackets \[ and \] and the deleted text is set in scriptscript size.
This float type should always be used with the C<TRADITIONAL> and C<CTRADITIONAL>
markup types as the \footnote command does not work properly in floating
environments.
=item C<IDENTICAL>
=back
=over 10
=item C<ARRENV>
=item C<COUNTERCMD>
C<|subsubsection|paragraph|subparagraph)> ]
=item C<CUSTOMDIFCMD>
This option is for advanced users and allows definition of special versions of
commands, which do not work as safe commands.
C<\newcommand{\DELblindtext}{{\color{red}\blindtext}}>
C<\newcommand{\ADDblindtext}{{\color{blue}\blindtext}}>
and then latexdiff should be invoked with the option C<-c CUSTOMDIFCMD=blindtext>.
[ Default: none ]
=item C<FLOATENV>
=item C<ITEMCMD>
[ Default: \C<item> ]
=item C<LISTENV>
Environments whose name matches the regular expression in C<LISTENV> are list
environments.
=item C<MATHENV>,C<MATHREPL>
If both \begin and \end for a math environment (environment name matching
C<MATHENV> or \[ and \])
are within the same deleted block, they are replaced by a \begin and \end commands
for C<MATHREPL>
rather than being commented out.
=item C<MATHARRENV>,C<MATHARRREPL>
=item C<MINWORDSBLOCK>
[ Default: 3 ]
=item C<PICTUREENV>
=item C<SCALEDELGRAPHICS>
[ Default: 0.5 ]
=item C<VERBATIMENV>
=item C<VERBATIMLINEENV>
=over 10
1. Use C<CFONT> type markup (option C<-t CFONT>): If this markup is chosen, then
changed citations are no longer marked up
with the wavy line (additions) or struck out (deletions), but are still highlighted
in the appropriate color, and deleted text is shown with a different font. Other
styles not using the C<ulem> package will also work.
For custom packages you can define the commands which need to be protected by
C<\mbox> with C<--append-mboxsafecmd> and C<--excludemboxsafecmd> options
(submit your lists of command as feature request at github page to set the default
behaviour of future versions, see section 6)
Try options C<--math-markup=whole>. If even that fails, you can turn off mark up
for equations with C<--math-markup=off>.
=item How can I just show the pages where changes had been made
=back
=head1 BUGS
=over 10
=back
Please submit bug reports using the issue tracker of the github repository page
I<https://github.com/ftilmann/latexdiff.git>,
or send them to I<tilmann -- AT -- gfz-potsdam.de>. Include the version number of
I<latexdiff>
(from comments at the top of the source or use B<--version>). If you come across
latex
files that are error-free and conform to the specifications set out
above, and whose differencing still does not result in error-free
latex, please send me those files, ideally edited to only contain the
offending passage as long as that still reproduces the problem. If your
file relies on non-standard class files, you must include those. I will not
look at examples where I have trouble to latex the original files.
L<latexrevise>, L<latexdiff-vc>
=head1 PORTABILITY
I<latexdiff> does not make use of external commands and thus should run
on any platform supporting Perl 5.6 or higher. If files with encodings
other than ASCII or UTF-8 are processed, Perl 5.8 or higher is required.
=head1 AUTHOR
Version 1.3.0
Copyright (C) 2004-2018 Frederik Tilmann
=cut
__END__
%%BEGIN SAFE COMMANDS
% Regex matching commands which can safely be in the
% argument of a \DIFadd or \DIFdel command (leave out the \)
arabic
dashbox
emph
fbox
framebox
hspace\*?
math.*
makebox
mbox
pageref
ref
symbol
raisebox
rule
text.*
shortstack
usebox
dag
ddag
copyright
pounds
S
P
oe
OE
ae
AE
aa
AA
o
O
l
L
frac
ss
sqrt
ldots
cdots
vdots
ddots
alpha
beta
gamma
delta
epsilon
varepsilon
zeta
eta
theta
vartheta
iota
kappa
lambda
mu
nu
xi
pi
varpi
rho
varrho
sigma
varsigma
tau
upsilon
phi
varphi
chi
psi
omega
Gamma
Delta
Theta
Lambda
Xi
Pi
Sigma
Upsilon
Phi
Psi
Omega
ps
mp
times
div
ast
star
circ
bullet
cdot
cap
cup
uplus
sqcap
vee
wedge
setminus
wr
diamond
(?:big)?triangle.*
lhd
rhd
unlhd
unrhd
oplus
ominus
otimes
oslash
odot
bigcirc
d?dagger
amalg
leq
prec
preceq
ll
(?:sq)?su[bp]set(?:eq)?
in
vdash
geq
succ(?:eq)?
gg
ni
dashv
equiv
sim(?:eq)?
asymp
approx
cong
neq
doteq
propto
models
perp
mid
parallel
bowtie
Join
smile
frown
.*arrow
(?:long)?mapsto
.*harpoon.*
leadsto
aleph
hbar
imath
jmath
ell
wp
Re
Im
mho
prime
emptyset
nabla
surd
top
bot
angle
forall
exists
neg
flat
natural
sharp
backslash
partial
infty
Box
Diamond
triangle
clubsuit
diamondsuit
heartsuit
spadesuit
sum
prod
coprod
int
oint
big(?:sq)?c[au]p
bigvee
bigwedge
bigodot
bigotimes
bigoplus
biguplus
(?:arc)?(?:cos|sin|tan|cot)h?
csc
arg
deg
det
dim
exp
gcd
hom
inf
ker
lg
lim
liminf
limsup
ln
log
max
min
Pr
sec
sup
[Hclbkdruvt]
[`'^"~=.]
_
AMPERSAND
(SUPER|SUB)SCRIPTNB
(SUPER|SUB)SCRIPT
SQRT
SQRTNB
PERCENTAGE
DOLLAR
%%END SAFE COMMANDS
%% FLOAT TYPES
% see:
% http://tex.stackexchange.com/questions/47351/can-i-redefine-a-command-to-
contain-itself
%DIF DIFCODE_UNDERLINE
moredelim=[il][\color{red}\sout]{\%DIF\ <\ },
moredelim=[il][\color{blue}\uwave]{\%DIF\ >\ }
%DIF END DIFCODE_UNDERLINE
%DIF DIFCODE_CTRADITIONAL
moredelim=[il][\color{red}\scriptsize]{\%DIF\ <\ },
moredelim=[il][\color{blue}\sffamily]{\%DIF\ >\ }
%DIF END DIFCODE_CTRADITIONAL
%DIF DIFCODE_TRADITIONAL
moredelim=[il][\color{white}\tiny]{\%DIF\ <\ },
moredelim=[il][\sffamily]{\%DIF\ >\ }
%DIF END DIFCODE_TRADITIONAL
%DIF DIFCODE_CFONT
moredelim=[il][\color{red}\scriptsize]{\%DIF\ <\ },
moredelim=[il][\color{blue}\sffamily]{\%DIF\ >\ }
%DIF END DIFCODE_CFONT
%DIF DIFCODE_FONTSTRIKE
moredelim=[il][\scriptsize \sout]{\%DIF\ <\ },
moredelim=[il][\sffamily]{\%DIF\ >\ }
%DIF END DIFCODE_FONTSTRIKE
%DIF DIFCODE_INVISIBLE
moredelim=[il][\color{white}\tiny]{\%DIF\ <\ },
moredelim=[il]{\%DIF\ >\ }
%DIF END DIFCODE_INVISIBLE
%DIF DIFCODE_CHANGEBAR
moredelim=[il][\color{white}\tiny]{\%DIF\ <\ },
moredelim=[il]{\%DIF\ >\ }
%DIF END DIFCODE_CHANGEBAR
%DIF DIFCODE_CCHANGEBAR
moredelim=[il][\color{red}]{\%DIF\ <\ },
moredelim=[il][\color{blue}]{\%DIF\ >\ }
%DIF END DIFCODE_CCHANGEBAR
%DIF DIFCODE_CULINECHBAR
moredelim=[il][\color{red}\sout]{\%DIF\ <\ },
moredelim=[il][\color{blue}\uwave]{\%DIF\ >\ }
%DIF END DIFCODE_CULINECHBAR
%DIF DIFCODE_CFONTCHBAR
moredelim=[il][\color{red}\scriptsize]{\%DIF\ <\ },
moredelim=[il][\color{blue}\sffamily]{\%DIF\ >\ }
%DIF END DIFCODE_CFONTCHBAR
%DIF DIFCODE_BOLD
% unfortunately \bfseries cannot be combined with ttfamily without extra packages
% also morecomment=[il] is broken as of v1.5b of listings at least
% workaround: plot in white with tiny font
% morecomment=[il]{\%DIF\ <\ },
moredelim=[il][\color{white}\tiny]{\%DIF\ <\ },
moredelim=[il][\sffamily\bfseries]{\%DIF\ >\ }
%DIF END DIFCODE_BOLD
%DIF DIFCODE_PDFCOMMENT
moredelim=[il][\color{white}\tiny]{\%DIF\ <\ },
moredelim=[il][\sffamily\bfseries]{\%DIF\ >\ }
%DIF END DIFCODE_PDFCOMMENT