malohm malohm - 3 months ago 16
LaTeX Question

Decrease spacing between equal signs using latex "eqnarray"-command in matplotlib?

I want to align the equal signs in matplotlib. Thus, I'm using the eqnarray environment in matplotlib:

import matplotlib.pyplot as plt
from matplotlib import rc

rc('text', usetex=True)
rc('font', size = 7)

fig = plt.figure(figsize=(3,2))
ax = fig.add_subplot(111)

ax.text(0.5,0.5 ,r'\begin{eqnarray*}' +\
r'M &=& 0.95' + '\\\\' +\
r'\xi &=& 0.5' + '\\\\' +\
r'\mu &=& 0.1' + '\\\\' +\
r'a/b &=& 0' + '\\\\' +\
r'\delta_{99}/L &=& 0' +\
r'\end{eqnarray*}',
verticalalignment='center',
horizontalalignment='center')

plt.savefig('output.pdf')
plt.show()


The result looks like this:
enter image description here

How can I decrease the spacing in the vicinity of the equal signs?

Answer

You need to load the amsmath package in order to use 'align'. Problems with the white space in 'eqnarray' are discussed here: https://github.com/matplotlib/matplotlib/issues/4954. At least in matplotlib 1.2.1 the problem is not resolved I guess.

This should give the same result:

#!/usr/bin/python
import matplotlib.pyplot as plt

preamble = {
    'text.usetex' : True,
    'font.size'   : 7,
    'text.latex.preamble': [
        r'\usepackage{amsmath}',
        ],
    }
plt.rcParams.update(preamble)

fig = plt.figure(figsize=(3.,2.))
ax  = fig.add_subplot(111)

ax.text(0.5,0.5 ,r'\begin{align*}' +\
             r'M              &= 0.95 \\' +\
             r'\xi            &= 0.5  \\' +\
             r'\mu            &= 0.1  \\' +\
             r'a/b            &= 0    \\' +\
             r'\delta_{99}/L  &= 0      ' +\
             r'\end{align*}',
             verticalalignment='center',
             horizontalalignment='center')



plt.savefig('output.pdf')
plt.show()

enter image description here

Comments