Python matplotlib MathText with variables

I need to document calculations including

  1. the used equation and
  2. the variables replaced by the values used

Example: A=1, B=2



I am using Reportlab and create flowable elements with matplotlib. The following code based on the help and can achieve No.1 from the list above. Two questions arise:

  1. How can I insert variable values in my mathtext_demo?

  2. Is there a way to write the general equation and the one with substituted values and result using some dictionary/solv

    import matplotlib.pylab as plt  
    mathext_demos = (r'$(frac{5 - frac{1}{x}}{4})$')       
    def make_equation(code,fsize=20,imgheight=70):
        fig=plt.figure(figsize=(7, 0.1))
                   xy=(.4, 0.1), ha='center', fontsize=fsize)


  1. You can use variables in your string:
a = 5
b = 4
mathext_demos = (r'$(frac{%s - frac{1}{x}}{%s})$' %(str(a), str(b))) 

You can also use %f for float, but I think that is something you have to figure out what works for you.

  1. You can write a function with the arguments a and b for example en return the string. Is that what you meant with your second question?
def create_mathtext(a,b):
    c = a + b
    return r'${%s} = {%s} + {%s}$' % tuple(map(str, [c, a, b]))

make_equation( create_mathtext(1, 2))