<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 使用 LaTeX 渲染文本 > 原文:[Text rendering With LaTeX](http://matplotlib.org/users/usetex.html) > 譯者:[飛龍](https://github.com/) > 協議:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/) Matplotlib 可以選擇使用 LaTeX 來管理所有文本布局。 此選項可用于以下后端: + Agg + PS + PDF LaTeX 選項通過在`rc`設置中設置`text.usetex:True`來激活。 使用 matplotlib 的 LaTeX 支持的文本處理會慢于 matplotlib 的非常強大的 mathtext,但是更靈活,因為可以使用不同的 LaTeX 包(字體包,數學包等)。 結果會十分驚人,特別是當你在圖形中使用和主文檔相同的字體。 Matplotlib 的 LaTeX 支持需要可用的 LaTeX 安裝版本,dvipng(可能包括在你的 LaTeX 安裝中)和 Ghostscript(建議使用 GPL Ghostscript 8.60 或更高版本)。 這些外部依賴的可執行文件必須都位于你的`PATH`中。 有幾個選項需要提及,可以使用`rc`設置更改它們。 這里是一個`matplotlibrc`示例文件: ``` font.family : serif font.serif : Times, Palatino, New Century Schoolbook, Bookman, Computer Modern Roman font.sans-serif : Helvetica, Avant Garde, Computer Modern Sans serif font.cursive : Zapf Chancery font.monospace : Courier, Computer Modern Typewriter text.usetex : true ``` 每個系列中的第一個有效字體是要加載的字體。 如果未指定字體,則默認使用 Computer Modern 字體。 所有其他字體是 Adobe 字體。 Times 和 Palatino 每個都有自己附帶的數學字體,而其他 Adobe 襯線字體使用 Computer Modern 數學字體。 有關更多詳細信息,請參閱 [PSNFSS](http://www.ctan.org/tex-archive/macros/latex/required/psnfss/psnfss2e.pdf) 文檔。 要使用 LaTeX 并選擇 Helvetica 作為默認字體,但不編輯`matplotlibrc`,使用: ```py from matplotlib import rc rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) ## for Palatino and other serif fonts use: #rc('font',**{'family':'serif','serif':['Palatino']}) rc('text', usetex=True) ``` 這里是標準的示例,`tex_demo.py`: ```py """ Demo of TeX rendering. You can use TeX to render all of your matplotlib text if the rc parameter text.usetex is set. This works currently on the agg and ps backends, and requires that you have tex and the other dependencies described at http://matplotlib.org/users/usetex.html properly installed on your system. The first time you run a script you will see a lot of output from tex and associated tools. The next time, the run may be silent, as a lot of the information is cached in ~/.tex.cache """ import numpy as np import matplotlib.pyplot as plt # Example data t = np.arange(0.0, 1.0 + 0.01, 0.01) s = np.cos(4 * np.pi * t) + 2 plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.plot(t, s) plt.xlabel(r'\textbf{time} (s)') plt.ylabel(r'\textit{voltage} (mV)',fontsize=16) plt.title(r"\TeX\ is Number " r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!", fontsize=16, color='gray') # Make room for the ridiculously large title. plt.subplots_adjust(top=0.8) plt.savefig('tex_demo') plt.show() ``` ![](http://matplotlib.org/_images/tex_demo.png) 要注意數學顯示模式(`$$ e=mc^2 $$`)是不支持的,但是添加命令`\displaystyle`之后會產生相同結果,就像`tex_demo.py`中那樣。 > 注意 > 一些字符在 TeX 中需要特別轉義,例如: > ``` > # $ % & ~ _ ^ \ { } \( \) \[ \] > ``` > 所以,這些字符會根據`rcParam text.usetex`標志位而表現不同。 ## 在 TeX 中使用 Unicode 也可以在 LaTeX 文本管理器中使用 unicode 字符串,這里是從`tex_unicode_demo.py`中獲取的示例: ```py # -*- coding: utf-8 -*- """ This demo is tex_demo.py modified to have unicode. See that file for more information. """ from __future__ import unicode_literals import numpy as np import matplotlib matplotlib.rcParams['text.usetex'] = True matplotlib.rcParams['text.latex.unicode'] = True import matplotlib.pyplot as plt plt.figure(1, figsize=(6, 4)) ax = plt.axes([0.1, 0.1, 0.8, 0.7]) t = np.arange(0.0, 1.0 + 0.01, 0.01) s = np.cos(2*2*np.pi*t) + 2 plt.plot(t, s) plt.xlabel(r'\textbf{time (s)}') plt.ylabel('\\textit{Velocity (\u00B0/sec)}', fontsize=16) plt.title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty' r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r') plt.grid(True) plt.show() ``` ![](http://matplotlib.org/_images/tex_unicode_demo1.png) ## Postscript 選項 為了生成可以嵌入到新 LaTeX 文檔中的 postscript 封裝文件,matplotlib 的默認行為是提取輸出,這會刪除 LaTeX 使用的一些 postscript 操作符,這些操作符在 eps 文件中是非法的。 此步驟產生的結果對于一些用戶可能是不可接受的,因為文本被粗略地光柵化并且被轉換為位圖,而不像標準 Postscript 那樣是可縮放的,并且文本是不可搜索的。 一種解決方法是在你的`rc`設置中將`ps.distiller.res`設置為較高的值(可能為 6000),這將產生較大的文件,但可能看起來更好并能夠合理縮放。 更好的解決方法需要 Poppler 或 Xpdf,可以通過將`ps.usedistiller rc`設置更改為`xpdf`來激活。 此替代方案產生 postscript 而不光柵化文本,因此它能夠正確縮放,可以在 Adobe Illustrator 中編輯,并搜索`pdf`文檔中的文本。 ## 可能的問題 + 在 Windows 上,可能需要修改[`PATH`](http://matplotlib.org/faq/environment_variables_faq.html#envvar-PATH)環境變量來包含 latex,dvipng 和 ghostscript 可執行文件的目錄。詳細信息請參閱[環境變量](http://matplotlib.org/faq/environment_variables_faq.html#environment-variables)和[在 Windows 中設置環境變量](http://matplotlib.org/faq/environment_variables_faq.html#setting-windows-environment-variables)。 + 使用 MiKTeX 與 Computer Modern 字體,如果你得到奇怪的 \*Agg 和 PNG 結果,訪問`MiKTeX/Options`并更新你的格式文件。 + 字體在屏幕上看起來糟糕。你可能正在運行 Mac OS,在 mac 上的老版本 dvipng 運行著一些有趣的事情。在你的`matplotlibrc`文件中設置`text.dvipnghack:True`。 + 在 Ubuntu 和 Gentoo 上,texlive 基本安裝不附帶 type1cm 包。你可能需要安裝一些額外的包,來獲得所有與其它 LaTeX 分發版捆綁的特性。 + matplotlib 已經取得了一些進展,所以可以直接使用`dvi`文件進行文本布局。這允許 LaTeX 用于具有`pdf`和`svg`后端的文本布局,以及 \*Agg 和 PS 后端。在將來,LaTeX 安裝可能是唯一的外部依賴。 ## 故障排除 + 嘗試刪除`.matplotlib/tex.cache`目錄。 如果你不知道`.matplotlib`在哪里,請參見 [matplotlib 配置和緩存目錄位置](http://matplotlib.org/faq/troubleshooting_faq.html#locating-matplotlib-config-dir)。 + 確保 LaTeX,dvipng 和 ghostscript 都正常工作,并存在于你的[`PATH`](http://matplotlib.org/faq/environment_variables_faq.html#envvar-PATH)中。 + 確保你想要做的事情在 LaTeX 文檔中可實現,你的 LaTeX 語法是有效的,并且你正在使用原始字符串,如果必要,以避免意外的轉義序列。 + 郵件列表上報告的大多數問題已通過升級 [Ghostscript](https://ghostscript.com/) 來清除。 如果可能的話,請嘗試升級到最新版本,然后向列表報告問題。 + `text.latex.preamble rc`設置不受官方支持。 此選項提供了大量的靈活性和導致問題的許多方法。 請在向郵件列表報告問題之前禁用此選項。 + 如果仍需要幫助,請參閱[獲取幫助](http://matplotlib.org/faq/troubleshooting_faq.html#reporting-problems)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看