Module latexpp.macro_subst_helper — helper for macro replacements

Module that provides a helper for writing fixes that perform macro substitutions with custom replacement strings.

class latexpp.macro_subst_helper.MacroSubstHelper(macros={}, environments={}, argspecfldname='argspec', args_parser_class=<class 'pylatexenc.macrospec._pyltxenc2_argparsers._base.MacroStandardArgsParser'>, context={})

Helper class that provides common functionality for fixes that replace certain macro invocations by a custom replacement string.

TODO: Document me. ….

eval_subst(c, n, *, node_contents_latex, argoffset=0, context={}, arg_filters=None)

Return the replacement string for the given node n, where the macro/environment config (argspec/repl) is provided in c (return value of get_node_cfg()).

You need to specify as node_contents_latex a callable that will be used to transform child nodes (argument nodes) to LaTeX code. If you’re calling this from a fix class (latexpp.fixes.BaseFix subclass) then you should most probably specify node_contents_latex=self.preprocess_contents_latex here.

If argoffset is nonzero, then the first argoffset arguments are skipped and the arguments argoffset+1, argoffset+2, … are exposed to the replacement string as %(1)s, %(2)s, ….

You can specify a dictionary context of additional key/value replacement strings in the formatting of the repl string. For instance, if context={'delimsize': r'\big'}, then %(delimsize)s in the replacement string repl is expanded to \big. This is all in addition to the argument placeholders %(1)s etc., to the environment body %(body)s, and to %(macroname)s/%(environmentname)s.

If arg_filters is not None and set to a dictionary, then additional placeholders of the type %(1.xyz) are also recognized, where xyz are keys in the arg_filters dictionary. The values in the arg_filters dictionary are functions that take the keyword arguments argspec, node, arg_index (0-based index), arg_number (placeholder number), and arg_contents (string representing the contents of the argument, as would be used if no filter were invoked), and return the replacement string should this placeholder and filter be used.

get_environment_cfg(environmentname)

Return the config associated with the environment named environmentname, or None.

You can use this function to test whether a given environment can be handled by us, testing the return value for non-None and using the return value directly as the relevant config for eval_subst().

get_macro_cfg(macroname)

Return the config associated with the macro named macroname, or None.

You can use this function to test whether a given macro can be handled by us, testing the return value for non-None and using the return value directly as the relevant config for eval_subst().

get_node_cfg(n)

Return the config associated with the macro/environment of the given node n, or None.

You can use this function to test whether a given node can be handled by us, testing the return value for non-None and using the return value directly as the relevant config for eval_subst().

get_specs()

Return the specs that we need to declare to the latex walker