简体   繁体   中英

LaTex in matplotlib

I am trying to connect LaTex in matplotlib and I can't do it right.

I did based on this information. Installed MiKTeX, dvipng and Ghostscript.

    import matplotlib
    matplotlib.use('agg') 
    import matplotlib.pyplot as plt
    from sympy import latex
    ...

    # sympy.latex() returned \left[\begin{matrix}2 & 4 & 6\\4 & 6 & 8\\8 & 6 & 4\end{matrix}\right]
    latex_to_img(r"$\left[\begin{matrix}2 & 4 & 6\\4 & 6 & 8\\8 & 6 & 4\end{matrix}\right]$", FILE_NAME_MATRIX)

    ...
    def latex_to_img(str_latex, file_name):

        plt.rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
        plt.rc('text', usetex=True)

        fig = plt.figure()
        ax = fig.add_axes([0,0,1,1])
        ax.set_axis_off()
        t = ax.text(0.5, 0.5, str_latex, 
                horizontalalignment='center', 
                verticalalignment='center',
                fontsize=32, 
                color='black')
        ax.figure.canvas.draw()
        bbox = t.get_window_extent()
        fig.set_size_inches(bbox.width/80, bbox.height/80) # dpi=80
        save(file_name, config.FILE_EXPANSION)

I get such a list:

RuntimeError: latex was not able to process the following string: b'$\\\\left[\\\\begin{matrix}2 & 4 & 6\\\\\\\\4 & 6 & 8\\\\\\\\8 & 6 & 4\\\\end{matrix}\\\\right]$'

Here is the full report generated by latex: This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019/W32TeX) (preloaded format=latex) restricted \\write18 enabled. entering extended mode (c:/Users/myname/.matplotlib/tex.cache/7b0c91ed78ceb07701f3d2b71c0c8048.te x LaTeX2e <2018-12-01> (c:/texlive/2019/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (c:/texlive/2019/texmf-dist/tex/latex/base/size10.clo)) (c:/texlive/2019/texmf-dist/tex/latex/type1cm/type1cm.sty) (c:/texlive/2019/texmf-dist/tex/latex/psnfss/helvet.sty (c:/texlive/2019/texmf-dist/tex/latex/graphics/keyval.sty)) (c:/texlive/2019/texmf-dist/tex/latex/base/textcomp.sty (c:/texlive/2019/texmf-dist/tex/latex/base/ts1enc.def (c:/texlive/2019/texmf-dist/tex/latex/base/ts1enc.dfu))) (c:/texlive/2019/texmf-dist/tex/latex/base/inputenc.sty) (c:/texlive/2019/texmf-dist/tex/latex/geometry/geometry.sty (c:/texlive/2019/texmf-dist/tex/generic/oberdiek/ifpdf.sty) (c:/texlive/2019/texmf-dist/tex/generic/oberdiek/ifvtex.sty) (c:/texlive/2019/texmf-dist/tex/generic/ifxetex/ifxetex.sty)

Package geometry Warning: Over-specification in h'-direction. width' (5058.9pt) is ignored.

Package geometry Warning: Over-specification in v'-direction. height' (5058.9pt) is ignored.

) (./7b0c91ed78ceb07701f3d2b71c0c8048.au x) (c:/texlive/2019/texmf-dist/tex/latex/base/ts1cmr.fd) geometry driver: auto-detecting geometry detected driver: dvips (c:/texlive/2019/texmf-dist/tex/latex/psnfss/ot1phv.fd) ! Misplaced alignment tab character &. l.14 ...000000}{\\sffamily $\\left[\\begin{matrix}2 & 4 & 6\\4 & 6 & 8\\8 & 6 &... No pages of output. Transcript written on 7b0c91ed78ceb07701f3d2b71c0c8048.lo g.

Add these two lines

matplotlib.rcParams['text.usetex']=True
matplotlib.rcParams['text.latex.unicode']=True

The problem was in the ampersand (&). Found a solution here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM