Skip to content

Commit

Permalink
replace contributed function take_comments_and_enter_from_frac() with…
Browse files Browse the repository at this point in the history
… much simpler, more robust implementation. Fixes #184
  • Loading branch information
ftilmann committed Mar 1, 2020
1 parent 9f243d6 commit e9d2fbc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 49 deletions.
64 changes: 17 additions & 47 deletions latexdiff
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
# Bug fixes:
# - remove some uninitialised variable $2 warnings in string substitution in flatten function in case included file is not found
# - add minimal postprocessing to diff processing of preamble commands (replace \RIGHTBRACE by \} )
# - pre-processing: replacee (contributed) routine take_comments_and_enter_from_frac() with take_comments_and_newline_from_frac(), which does the same thing
# (remove whitespace characters and comments between the argument of \frac commands) in an easier and more robust way. In addition, it
# will replace commands like \frac12 with \frac{1}{2} as pre-processing step. Fixes issue #184
#
# Version 1.3.0 (7 October 2018):
# - treat options to \documentclass as potential package names (some packages allow implicit loading of or imply selected packages
Expand Down Expand Up @@ -2491,52 +2494,19 @@ sub marktags {
}

#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 {}***********

###**********take the \n and % between {} and {} of the frac***************
while( m/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)\}(.*?)\\frac\{(.*?)\\end\{\1\}/s ) {
# if there isn't any more //frac before the first //end in the pattern $2 then there should be an \\end{...} in $2
###print STDERR "Match the following in take_comments and_enter_from_frac(2):\n****$&****\n" if $debug;
if( $2 !~ m/\\end\{$1\}/s ) {
# from now on CURRFRAC is the frac we are looking at
s/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)\}(.*?)\\frac\{(.*?)\\end\{\1\}/\\begin\{$1\}$2CURRFRAC\{$3\\end\{$1\}/s;
while( m/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)\}(.*?)CURRFRAC\{(.*?)\\end\{\1\}/s ) {
if( m/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)\}(.*?)CURRFRAC\{($pat_n)\}([\s]*(%[^\n]*?)*[\r\n|\r|\n])+[\s]*\{(.*?)\\end\{\1}/s ) {
s/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)\}(.*?)CURRFRAC\{($pat_n)\}([\s]*(%[^\n]*?)*[\r\n|\r|\n])+[\s]*\{(.*?)\\end\{\1\}/\\begin\{$1\}$2CURRFRAC\{$3\}\{$6\\end\{$1\}/s;
}
else { # there is no comment or \n between the two brackets {}{}
# change CURRFRAC to FRACSTART so we can change them all back to //frac{ when we finish
s/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)}(.*?)CURRFRAC\{(.*?)\\end\{\1}/\\begin{$1}$2FRACSTART\{$3\\end{$1}/s;
}
}
}
else{
###there are no more frac 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 ){}
s/FRACSTART/\\frac/g;
###***************take the \n and % between {} and {} of the frac*********************
sub take_comments_and_newline_from_frac() {
# some special magic for common usage of frac, which does not conform to the latexdiff requirements but can be made to fit
# note that this is a rare exception to the general rule that the new tex can be reconstructed from the diff file

# \frac12 -> \frac{1}{2}
s/\\frac(\d)(\w)/\\frac\{$1\}\{$2\}/g;

# \frac1{2b} -> \frac{1}{2b}
s/\\frac(\d)/\\frac\{$1\}/g;

# delete space and comment characters between \frac arguments
# s/\\frac(?:\s*?%[^\n]*?)*?(\{$pat_n\})\s*(\{$pat_n\})/\\frac$1$2/g;
s/\\frac(?:\s|%[^\n]*?)*(\{$pat_n\})(?:\s|%[^\n]*?)*(\{$pat_n\})/\\frac$1$2/g;
}

# preprocess($string, ..)
Expand Down Expand Up @@ -2640,7 +2610,7 @@ sub preprocess {
s/\\begin\{($ARRENV)}(.*?)\\end\{\1}/\\ARRAYBLOCK$1\{$2\\DIFANCHORARRB \}\\DIFANCHORARRE /sg;
### print STDERR "vvvvvvvvvvvvvvvvvv\n$_\nvvvvvvvvvvvvvvvv\n" if $debug;

take_comments_and_enter_from_frac();
take_comments_and_newline_from_frac();

### print STDERR "^^^^^^^^^^^^^^^^^^\n$_\n^^^^^^^^^^^^^^^^^\n" if $debug;
s/\\begin\{($MATHENV|$MATHARRENV|SQUAREBRACKET)\}(.*?)\\end\{\1\}/\\MATHBLOCK$1\{$2\\DIFANCHORMATHB \}\\DIFANCHORMATHE /sg;
Expand Down
28 changes: 28 additions & 0 deletions testsuite/frac_w_space_comments_new.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\section{section}
change $\frac1a$

\begin{subequations} % eq 1
\begin{gather}
\begin{align}
a &= 1\\
b &= 1
\end{align}
\end{gather}
\end{subequations}
\begin{equation} % eq 2
a + b= 2
\end{equation}
\begin{equation} % eq 3
\frac {a} {b} = 1
\end{equation}
\begin{equation} % eq 4
\frac %Here is a fraction
{c} % Numerator
{d} % Denominator
= 1
\end{equation}
\end{document}

24 changes: 24 additions & 0 deletions testsuite/frac_w_space_comments_old.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\section{section}
change
\begin{subequations} % eq 1
\begin{gather}
\begin{align}
a &= 1\\
b &= 1
\end{align}
\end{gather}
\end{subequations}
\begin{equation} % eq 2
a + b= 2
\end{equation}
\begin{equation} % eq 3
\frac{a}{b} = 1
\end{equation}
\begin{equation} % eq 4
\frac{c}{d} = 1
\end{equation}
\end{document}

4 changes: 2 additions & 2 deletions testsuite/verify
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/tcsh
set refversion=1.3.0
set refversion=1.3.1a
#set refversion=1.1.0
# for comparison with 0.5:
#set options="-V -t UNDERLINE -s SAFE -f FLOATSAFE --disable-citation-markup"
Expand All @@ -20,7 +20,7 @@ set testroots=( test rapine_et_al island_obs2004 texdiffsample gershwin12 "gersh
delequ latin9 pollack move-equation nomarkup subscript doubledollar intertext \
DIFDELCMDBUG eqnarray eqnarray2 subscriptm delequ2 schneider gennady umesh underwood endfloat endfloat2 outerrise \
delequ3 delequ4 "simplefrac --allow-spaces" "master --flatten" "subfile --flatten" titlediffTest2 exampleDiff bornd2 \
"rolla --math-markup=2" mini "complex-maths --math-markup=1" margalit circonflex mwe-comment "apacite-test --flatten" quoteddollarunderscore units complicated-math move-equation2 lists "figures --graphics-markup=both" alignat "safecmd --append-safecmd=remark" verbatim verbatim2-is143 embedded-math-array "verbatim-input --flatten" anchordemo 'customdiffcmdtest --add-to-config "CUSTOMDIFCMD=blindtext;mypar"' description \
"rolla --math-markup=2" mini "complex-maths --math-markup=1" margalit circonflex mwe-comment "apacite-test --flatten" quoteddollarunderscore units complicated-math move-equation2 lists "figures --graphics-markup=both" alignat "safecmd --append-safecmd=remark" verbatim verbatim2-is143 embedded-math-array "verbatim-input --flatten" anchordemo 'customdiffcmdtest --add-to-config "CUSTOMDIFCMD=blindtext;mypar"' description frac_w_space_comments \
)

#set testroots= ( test subscript )
Expand Down

0 comments on commit e9d2fbc

Please sign in to comment.