Skip to content

Commit

Permalink
Merge pull request #92 from matze-dd/Issue-85
Browse files Browse the repository at this point in the history
Issue 85
  • Loading branch information
matze-dd authored Oct 27, 2020
2 parents 022e7df + 5376d57 commit 6e0b008
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 3 deletions.
7 changes: 6 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ Work in progress
- LaTeX macros / environments
- builtins: added \\bibitem, \\bibliographystyle, \\begin{thebibliography}
(issue [#80](../../issues/80))
- yalafi.shell
- added option --simple-equations for simple replacements of displayed
equations (issue [#85](../../issues/85))
- yalafi core
- added 'ru' for option --lang (issue [#84](../../issues/84))
- added option --seqs for simple replacements of displayed equations
(issue [#85](../../issues/85))
- fixed problem in tests/test_packages/test\_latex\_builtins.py
(issue [#86](../../issues/86))
- README.md: minor edits
- README.md: updated

Version 1.1.6 (2020/10/19)
--------------------------
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ Default option values are set at the Python script beginning.
comma-separated list.
This is useful for check of foreign-language text, if marked accordingly.
Internally used for detection of file inclusions on --include.
- `--simple-equations`<br>
Replace a displayed equation only with a single placeholder from collection
'Paramaters.math\_repl\_display' in file yalafi/parameters; append trailing
interpunction, if present.
- `--disable rules`<br>
Comma-separated list of ignored LT rules, is passed as --disable to LT
(default: 'WHITESPACE\_RULE').
Expand Down Expand Up @@ -1071,6 +1075,10 @@ are placed at the right side of the alignment character '\&'.
LaTeX does not enforce that, but it is the style found in examples of the
documentation for package amsmath.

**Remark.**
For a simplification, see option --simple-equations in section
[Example application](#example-application).

With the default entry
```
EquEnv(self, 'align'),
Expand Down Expand Up @@ -1219,7 +1227,7 @@ The LaTeX filter can be integrated in shell scripts, compare the examples in
```
python -m yalafi [--nums file] [--repl file] [--defs file] [--dcls class]
[--pack modules] [--extr macros] [--lang xy] [--ienc enc]
[--unkn] [latexfile]
[--seqs] [--unkn] [latexfile]
```
Without positional argument `latexfile`, standard input is read.

Expand All @@ -1243,6 +1251,9 @@ Without positional argument `latexfile`, standard input is read.
proof titles, and for handling of macros like '\"\='.
- `--ienc enc`<br>
As option --encoding in section [Example application](#example-application).
- `--seqs`<br>
As option --simple-equations in section
[Example application](#example-application).
- `--unkn`<br>
As option --list-unknown in section
[Example application](#example-application).
Expand All @@ -1265,6 +1276,7 @@ Invocation of `python -m yalafi ...` differs as follows from
see [the example below](#equation-html-report).
- Added options --dcls and --pack allow modification of predefined LaTeX
macros and environments at Python level.
- Added option --seqs.
- Option --defs expects a file containing macro definitions as LaTeX code.
- Option --ienc is also effective for file from --defs.
- Option --char (position tracking for single characters) is always activated.
Expand Down
19 changes: 19 additions & 0 deletions tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,22 @@ def test_9():
plain, pos = utils.get_txt_pos(toks)
assert plain_9 == plain

# simplified equation parsing
#
latex_10 = r"""
\usepackage{amsmath}
\begin{align}
a &= b.
\end{align}
"""
plain_10 = r"""
W-W-W.
"""
def test_10():
parms = parameters.Parameters()
parms.math_displayed_simple = True
p = parser.Parser(parms)
toks = p.parse(latex_10)
plain, pos = utils.get_txt_pos(toks)
assert plain_10 == plain

11 changes: 10 additions & 1 deletion yalafi/mathparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(self, parser):

def expand_display_math(self, buf, tok, env):
buf.next()
start = tok.pos
start_simple = start = tok.pos
first_section = True
next_repl = True
out = [defs.ActionToken(start),
Expand Down Expand Up @@ -93,6 +93,15 @@ def expand_display_math(self, buf, tok, env):
else:
out = [defs.ActionToken(out[-1].pos)]
else:
if self.parser.parms.math_displayed_simple:
txt = self.parser.get_text_direct(out).strip()
out = [defs.ActionToken(start_simple),
defs.SpaceToken(start_simple, ' ', pos_fix=True),
defs.TextToken(start_simple,
self.parser.parms.math_repl_display[0], pos_fix=True)]
if txt and txt[-1] in self.parser.parms.math_punctuation:
out.append(defs.TextToken(out[-1].pos, txt[-1],
pos_fix=True))
out.append(defs.ActionToken(out[-1].pos))
return out

Expand Down
4 changes: 4 additions & 0 deletions yalafi/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ def init_collections(self):
#
def init_math_collections(self):

# simple replacement for displayed equations?
#
self.math_displayed_simple = False

# things to be ignored in math mode
# - some entries are redundant, if macros are known from text mode
# and expand to 'nothing'
Expand Down
1 change: 1 addition & 0 deletions yalafi/shell/proofreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def run_proofreader_options(tex, language, disable, enable,
t2t_options = tex2txt.Options(char=True, repl=cmdline.replace,
defs=cmdline.define, lang=language[:2],
extr=cmdline.extract, unkn=cmdline.list_unknown,
seqs=cmdline.simple_equations,
dcls=cmdline.documentclass, pack=cmdline.packages)

if cmdline.plain_input:
Expand Down
1 change: 1 addition & 0 deletions yalafi/shell/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
parser.add_argument('--skip')
parser.add_argument('--plain-input', action='store_true')
parser.add_argument('--list-unknown', action='store_true')
parser.add_argument('--simple-equations', action='store_true')
parser.add_argument('--language', default=default_option_language)
parser.add_argument('--encoding', default=default_option_encoding)
parser.add_argument('--replace')
Expand Down
6 changes: 6 additions & 0 deletions yalafi/tex2txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def read(file):
extr = ['\\' + s for s in opts.extr.split(',')]
else:
extr = []
if opts.seqs:
parms.math_displayed_simple = True
p = parser.Parser(parms, packages, read_macros=read)
toks = p.parse(latex, extract=extr)
txt, pos = utils.get_txt_pos(toks)
Expand Down Expand Up @@ -199,6 +201,7 @@ def __init__(self,
pack=None, # import modules for \usepackage
extr=None, # or string: comma-separated macro list
lang=None, # or set to language code
seqs=False, # True: simple replacements for displayed equations
unkn=False): # True: print unknowns
self.ienc = ienc
self.repl = repl
Expand All @@ -211,6 +214,7 @@ def __init__(self,
self.pack = pack
self.extr = extr
self.lang = lang
self.seqs = seqs
self.unkn = unkn

# function to be called for stand-alone script
Expand All @@ -227,6 +231,7 @@ def main():
parser.add_argument('--extr')
parser.add_argument('--lang')
parser.add_argument('--ienc')
parser.add_argument('--seqs', action='store_true')
parser.add_argument('--unkn', action='store_true')
cmdline = parser.parse_args()

Expand All @@ -242,6 +247,7 @@ def main():
pack=cmdline.pack,
extr=cmdline.extr,
lang=cmdline.lang,
seqs=cmdline.seqs,
unkn=cmdline.unkn)

if cmdline.file:
Expand Down

0 comments on commit 6e0b008

Please sign in to comment.