fix_latex_command_regex(pattern,
application='match')
| source code
|
Given a pattern for a regular expression match or substitution, the
function checks for problematic patterns commonly encountered when
working with LaTeX texts, namely commands starting with a backslash.
For a pattern to be matched or substituted, and extra backslash is
always needed (either a special regex construction like ``\w`` leads to
wrong match, or ``\c`` leads to wrong substitution since ``\`` just
escapes ``c`` so only the ``c`` is replaced, leaving an undesired
backslash). For the replacement pattern in a substitutions, specified by
the ``application='replacement'`` argument, a backslash before any of the
characters ``abfgnrtv`` must be preceeded by an additional backslash.
The application variable equals 'match' if `pattern` is used for a
match and 'replacement' if `pattern` defines a replacement regex in a
``re.sub`` command.
Caveats: let `pattern` just contain LaTeX commands, not combination of
commands and other regular expressions (``\s``, ``\d``, etc.) as the
latter will end up with an extra undesired backslash.
Here are examples on failures.
>>> re.sub(r'egin\{equation\}', r'\[', r'egin{equation}')
'\begin{equation}'
>>>
>>> re.sub(r'\mbox\{(.+?)\}', r'box{\g<1>}', r'\mbox{not}')
'\box{not}'
Here are examples on using this function.
>>> from doconce.latex import fix_latex_command_regex as fix
>>> pattern = fix(r'egin\{equation\}', application='match')
>>> re.sub(pattern, r'\[', r'egin{equation}')
'\['
>>> pattern = fix(r'\mbox\{(.+?)\}', application='match')
>>> replacement = fix(r'box{\g<1>}', application='replacement')
>>> re.sub(pattern, replacement, r'\mbox{not}')
'\fbox{not}'
Avoid mixing LaTeX commands and ordinary regular expression commands,
e.g.,
>>> pattern = fix(r'\mbox\{(\d+)\}', application='match')
>>> pattern
'\\mbox\{(\\d+)\}'
>>> re.sub(pattern, replacement, r'\mbox{987}')
'\mbox{987}' # no substitution, no match
|