Package doconce :: Module latex
[hide private]
[frames] | no frames]

Module latex

source code

Functions [hide private]
 
latex_code(filestr, format) source code
 
latex_figure(m, includegraphics=True) source code
 
latex_movie(m) source code
 
latex_table(table) source code
 
latex_title(m) source code
 
latex_author(authors_and_institutions, auth2index, inst2index, index2inst, auth2email) source code
 
latex_ref_and_label(section_label2title, format, filestr) source code
 
latex_index_bib(filestr, index, citations, bibfile) source code
 
latex_exercise(exer) source code
 
define(FILENAME_EXTENSION, BLANKLINE, INLINE_TAGS_SUBST, CODE, LIST, ARGLIST, TABLE, EXERCISE, FIGURE_EXT, CROSS_REFS, INDEX_BIB, INTRO, OUTRO) source code
 
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.
source code
Variables [hide private]
  additional_packages = ''
  __package__ = 'doconce'
Function Details [hide private]

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}'
>>> # 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