doconce.latex

doconce.latex.define(FILENAME_EXTENSION, BLANKLINE, INLINE_TAGS_SUBST, CODE, LIST, ARGLIST, TABLE, EXERCISE, FIGURE_EXT, CROSS_REFS, INDEX_BIB, INTRO, OUTRO)
doconce.latex.fix_latex_command_regex(pattern, application='match')
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}'
>>> # match of mbox, not \mbox, and wrong output
>>> 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
doconce.latex.latex_author(authors_and_institutions, auth2index, inst2index, index2inst, auth2email)
doconce.latex.latex_code(filestr, format)
doconce.latex.latex_exercise(exer)
doconce.latex.latex_figure(m, includegraphics=True)
doconce.latex.latex_index_bib(filestr, index, citations, bibfile)
doconce.latex.latex_movie(m)
doconce.latex.latex_ref_and_label(section_label2title, format, filestr)
doconce.latex.latex_table(table)
doconce.latex.latex_title(m)

Previous topic

doconce.html

Next topic

doconce.pdflatex

This Page