1

I have rendered this latex expression using matplotlib, but it has wrapped the text and therefore given a multiline output.

I want the output to look like this instead: enter image description here

I set wrap = False , but it still does this

t = plt.text(0.5, 0.5, expr, fontsize=320, fontweight='bold', wrap=False, color='white',  horizontalalignment='center',verticalalignment='center')

I am not sure why it is wrapping it to 3 lines still.

For reference, this is latex expression being rendered.

$\equiv\ \frac{x^{3}}{3} + \frac{x^{2}}{2} \operatorname{asin}{\left (x \right )} + \frac{x^{2}}{2} + \frac{x}{4} \sqrt{- x^{2} + 1} + \begin{cases} 2 i \sqrt{x - 1} - 2 \log{\left (\sqrt{x} \right )} + \log{\left (x \right )} + 2 i \operatorname{asin}{\left (\frac{1}{\sqrt{x}} \right )} & \text{for}\: \left|{x}\right| > 1 \\2 \sqrt{- x + 1} + \log{\left (x \right )} - 2 \log{\left (\sqrt{- x + 1} + 1 \right )} & \text{otherwise} \end{cases} - \frac{1}{4} \operatorname{asin}{\left (x \right )}$

How would i get the desired result?

Lewis Kelsey
  • 4,129
  • 1
  • 32
  • 42

1 Answers1

0

I removed the backslash behind equiv in the latex formula. Then, using the code from this github, to which I linked already in the previous question I get the desired output.

import matplotlib.pyplot as plt
import numpy as np
plt.rc('text', usetex=True)
plt.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'

def plot_equation(eq, fontsize=50, outfile=None, padding=0.1):
    """
    Function taken from
    https://gist.github.com/ahwillia/c7e54f875913ebc3de3852e9f51ccc69
    Plot an equation as a matplotlib figure.
    Parameters
    ----------
    eq : string
        The equation that you wish to plot. Should be plottable with
        latex. If `$` is included, they will be stripped.
    fontsize : number
        The fontsize passed to plt.text()
    outfile : string
        Name of the file to save the figure to.
    padding : float
        Amount of padding around the equation in inches.
    Returns
    -------
    ax : matplotlib axis
        The axis with your equation.
    """
    # clean equation string
    eq = eq.strip('$').replace(' ', '')

    # set up figure
    f = plt.figure()
    ax = plt.axes([0,0,1,1])    
    r = f.canvas.get_renderer()

    # display equation
    t = ax.text(0.5, 0.5, '${}$'.format(eq), fontsize=fontsize,
        horizontalalignment='center',verticalalignment='center')

    # resize figure to fit equation
    bb = t.get_window_extent(renderer=r)
    w,h = bb.width/f.dpi,np.ceil(bb.height/f.dpi)
    f.set_size_inches((padding+w,padding+h))

    # set axis limits so equation is centered
    plt.xlim([0,1])
    plt.ylim([0,1])
    ax.grid(False)
    ax.set_axis_off()

    if outfile is not None:
        plt.savefig(outfile)

    return ax

if __name__ == "__main__":
    plot_equation('x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}',outfile=__file__[:-3]+"1.png",padding=0.1)

    eq =r" \equiv \frac{x^{3}}{3} + \frac{x^{2}}{2} \operatorname{asin}{\left (x \right )} + \frac{x^{2}}{2} + \frac{x}{4} \sqrt{- x^{2} + 1} + \begin{cases} 2 i \sqrt{x - 1} - 2 \log{\left (\sqrt{x} \right )} + \log{\left (x \right )} + 2 i \operatorname{asin}{\left (\frac{1}{\sqrt{x}} \right )} & \text{for}\: \left|{x}\right| > 1 \\2 \sqrt{- x + 1} + \log{\left (x \right )} - 2 \log{\left (\sqrt{- x + 1} + 1 \right )} & \text{otherwise} \end{cases} - \frac{1}{4} \operatorname{asin}{\left (x \right )}"
    plot_equation( eq ,outfile=__file__[:-3]+"2.png",padding=0.1, fontsize=10)

enter image description here

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712