4

I'm in the situation where the enduser can define a variable name by himself.

For instance: a variable called "tbm_al" is correct.

In order to pprint variable as latex, I'm using sympy.latex and expecting to have something like "tbm" with "al" as indice, but bm is translated in boldsymbol.

enter image description here

Is there a way to have both "tbm" with indice "al" and neither t (bold) with indice al nor tbm_al as string ?

like:

\begin{equation*}\begin{equation}{tbm}_{al}\end{equation}\end{equation*}

user2652620
  • 464
  • 2
  • 17

2 Answers2

4

This autotranslation of bm is performed by the Sympy latex printer (sympy.printing.latex), specifically as bm is an entry in the variable modifiers dictionary modifier_dict declared in sympy.printing.latex. I see no way in the source to disable the use of the modifier dict upon call latex(expr, **settings); from what I can see, not settings are not used anywhere in the same context as the modifier_dict dictionary.

Have a look at e.g. the function translate(s) in the source:

def translate(s):

Check for a modifier ending the string. If present, convert the modifier to latex and translate the rest recursively.

...

From the source of this function, it's quite clear that the modifier dictionary will be checked (recursively) for all entries in the argument expression.


The remaining option would then be to manually modify the name modifiers (modifier_dict) in your own custom source copy of sympy.printing.latex (or, alternatively, in the original), by simply removing the dictionary entry for key bm. This is, of course, unless you want to make use of bm elsewhere.

See also:

dfrib
  • 70,367
  • 12
  • 127
  • 192
  • I used a decorator to decorate the latex function. – user2652620 Jan 12 '16 at 13:40
  • @user2652620 that seems like a valid workaround, good job. As a note: a "less intrusive" method would be to only modify parts of the dictionary, rather than wiping all of it. – dfrib Jan 12 '16 at 13:55
1

Thx to @dfri. I decided to clear modifier_dict during latex translate.

from sympy.printing.latex import modifier_dict
from sympy import latex
def cancel_sympy_translate(f):
    def wrapper(*args, **kwargs):
        saved_dict = dict(modifier_dict)
        modifier_dict.clear()
        result = f(*args, **kwargs)
        modifier_dict.update(saved_dict)
        return result
    return wrapper

latex = cancel_sympy_translate(latex)

t = Symbol("tbm_al")
print latex(t, mode="equation")

\begin{equation}tbm_{al}\end{equation}

with the "keep_translate_keys". (suggested by @dfri)

def cancel_sympy_translate(f, keep_translate_keys=None):

    keep_translate_keys = keep_translate_keys or []

    def remove_unwanted_keys(modif_dict):
        for k in modif_dict.keys():
            if k in keep_translate_keys:
                continue
            del modif_dict[k]

    def wrapper(*args, **kwargs):
        saved_dict = dict(modifier_dict)
        remove_unwanted_keys(modifier_dict)
        result = f(*args, **kwargs)
        modifier_dict.update(saved_dict)
        return result

    return wrapper

latex = cancel_sympy_translate(latex, keep_translate_keys=["bar"])
t = Symbol("tbm_abar")
print latex(t, mode="equation")

\begin{equation}tbm_{\bar{a}}\end{equation}

user2652620
  • 464
  • 2
  • 17