Skip to content

Commit

Permalink
Fix package parsing bug, add support to cleveref
Browse files Browse the repository at this point in the history
  • Loading branch information
ftilmann committed Mar 8, 2015
1 parent 31f2444 commit a8eb8d9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ latexdiff.aux
latexdiff.tex
latexrevise.aux
latexrevise.tex

latexdiff-1.0.5a
74 changes: 56 additions & 18 deletions latexdiff
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
# Version 1.0.5a
# - treat diacritics (\",\', etc) as safe commands
# - treat \_ and \& correctly as safe commands, even if used without spacing to the next word
# - Add a BOLD markup type that sets added text in bold face (Contribution by )
# - add append-mboxsafecmd list option to be able to specify
# - protect \SI command in \siunitx package
# - Add a BOLD markup type that sets added text in bold face (Contribution by Victor Zabalza via pull request )
# - add append-mboxsafecmd list option to be able to specify special safe commands which need to be surrounded by mbox to avoid breaking the latex
# - protect \SI command in \siunitx package and \cref,\Cref{range}{*}
# - bug fix: packages identified correctly even if \usepackage command options extend over several lines (previously \usepackage command needed to fully contained in one line
#
# Version 1.0.4
# - introduce list UNSAFEMATHCMD, which holds list of commands which cannot be marked up with \DIFadd or \DIFdel commands (only relevant for WHOLE and COARSE math markup modes)
Expand Down Expand Up @@ -720,8 +721,9 @@ if ( length $oldpreamble && length $newpreamble ) {
add_safe_commands($newpreamble);

# get a list of packages from preamble if not predefine
%packages=list_packages(@newpreamble) unless %packages;
### if ( %packages ) {print STDERR "DEBUG Packages: ",%packages,"\n" ;}
### %packages=list_packages(@newpreamble) unless %packages;
%packages=list_packages($newpreamble) unless %packages;
if ( %packages && $debug ) { my $key ; foreach $key (keys %packages) { print STDERR "DEBUG \\usepackage[",$packages{$key},"]{",$key,"}\n" ;} }

if (defined $packages{"hyperref"} ) {
# deleted lines should not generate or appear in link names:
Expand Down Expand Up @@ -804,6 +806,13 @@ if (defined $packages{"siunitx"} && ( $enablecitmark || ( $ulem && ! $disablec
init_regex_arr_ext(\@MBOXCMDLIST,'SI');
}

if (defined $packages{"cleveref"} && ( $enablecitmark || ( $ulem && ! $disablecitmark ))) {
# 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 at the same time as style using ulem.\n" if $verbose ;
init_regex_arr_ext(\@MBOXCMDLIST,'[Cc]ref(?:range)?\*?,labelcref,(?:lc)?name[cC]refs?');
}


my ( $citpat);

Expand Down Expand Up @@ -951,22 +960,43 @@ sub read_file_with_encoding {
return $output;
}

# %packages=list_packages(@preamble)
## %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
sub list_packages {
my (@preamble)=@_;
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 (defined($1)) {
$packages{$2}=$1;
} else {
$packages{$2}="";
}

# remove comments
$preamble=~s/(?<!\\)%.*$//mg ;

while ( $preamble =~ m/\\(?:documentclass|usepackage|RequirePackage)(?:\[($brat0)\])?\{(.*?)\}/gs ) {
if (defined($1)) {
$packages{$2}=$1;
} else {
$packages{$2}="";
}
}
return (%packages);
Expand Down Expand Up @@ -2704,7 +2734,7 @@ format as new.tex but has all changes relative to old.tex marked up or commented
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
endfloat hyperref amsmath apacite siunitx cleveref
[ Default: scan the preamble for \\usepackage commands to determine
loaded packages.]
Expand Down Expand Up @@ -3025,6 +3055,14 @@ errors).
Redefine the commands recognised as citation commands.
=item C<siunitx>
Treat \\SI as equivalent to citation commands (i.e. protect with \\mbox if markup style uses ulem package.
=item C<cleveref>
Treat \\cref,\\Cref, etc as equivalent to citation commands (i.e. protect with \\mbox if markup style uses ulem package.
=back
[ Default: scan the preamble for C<\\usepackage> commands to determine
Expand Down
13 changes: 11 additions & 2 deletions testsuite/units-new.tex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
\documentclass[review,number,sort&compress]{elsarticle}
\documentclass[review,number,sort&compress]{article}

\usepackage{units}
\usepackage[colorlinks,urlcolor=black]{hyperref}
%\usepackage[colorlinks,urlcolor=black]{hyperref}
\usepackage{siunitx}
\usepackage{cleveref}

\begin{document}
$\unit[1 \times 10^{-3}]{km}$.
Expand All @@ -16,4 +17,12 @@

This is the new text \SI{300}{\meter\per\second}

Some test of references in cleveref:
\begin{equation}
1+1=2
\label{simplicissimus}
\end{equation}

This is my reference \cref{simplicissimus}

\end{document}
7 changes: 5 additions & 2 deletions testsuite/units-old.tex
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
\documentclass[review,number,sort&compress]{elsarticle}
\documentclass[review,number,sort&compress]{article}

\usepackage{units}
\usepackage[colorlinks,urlcolor=black]{hyperref}
%\usepackage[colorlinks,urlcolor=black]{hyperref}
\usepackage{siunitx}
\usepackage{cleveref}

\begin{document}
$\unit[1.1 \times 10^{-3}]{km}$
Expand All @@ -17,4 +18,6 @@
This is the old text \SI{600}{\meter\per\second}




\end{document}

0 comments on commit a8eb8d9

Please sign in to comment.