84

I recently upgraded my laptop to Snow Leopard, updated TeX to Version 3.1415926 (TeX Live 2011/MacPorts 2011_5), and installed Python 2.7.3. After all these installs, I ran macport selfupdate and macport upgrade outdated. However, now when I try to use TeX in matplotlib, I receive the following:

LaTeX was not able to process the following string:'lp'
Here is the full report generated by LaTeX: 

This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011/MacPorts 2011_5)
 restricted \write18 enabled.  
entering extended mode (./64a53cc27244d5ee10969789771e33fa.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, cz
ech, slovak, dutch, ukenglish, usenglishmax, basque, french, german-x-2009-06-1
9, ngerman-x-2009-06-19, german, ngerman, swissgerman, italian, polish, portugu
ese, spanish, catalan, galician, ukenglish, loaded.
(/opt/local/share/texmf-texlive-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/opt/local/share/texmf-texlive-dist/tex/latex/base/size10.clo))

! LaTeX Error: File `type1cm.sty' not found.

Type X to quit or <RETURN> to proceed, or enter new name. (Default extension: sty)

     
l.3 \renewcommand
             {\rmdefault}{pnc}^^M
No pages of output.

Similar to this previous question, I tried setting the path in my python code via:

os.environ['PATH'] = os.environ['PATH'] + ':/opt/local/bin/latex'

since which latex yielded /opt/local/bin/latex. However, that didn't work, with the same error message. I also tried the path to tex, as well as the example from the previous question. No change.

I then tried to force possibly missing packages via:

matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]

however, that also did not work.

The only way I can get my plots to work is to say rc('text', usetex=False), which is not ideal. Any help would be much appreciated.

1
  • 7
    this fixed for me sudo apt-get install dvipng Commented Jul 6, 2016 at 23:41

7 Answers 7

113

On an Ubunutu 14.04 machine the combination of answers from above worked. I sudo apt-get install the dvipng,texlive-latex-extra, and texlive-fonts-recommended packages and that did the trick:

$ sudo apt-get install dvipng texlive-latex-extra texlive-fonts-recommended

Edit: As of Matplotlib 3.2.1, you now also need the package cm-super (see https://github.com/matplotlib/matplotlib/issues/16911)

$ sudo apt-get install dvipng texlive-latex-extra texlive-fonts-recommended cm-super
Sign up to request clarification or add additional context in comments.

6 Comments

Works on Ubuntu16.04 as well.
Also works on Ubuntu 16.04.1 in the Windows Subsystem for Linux.
I can generate plots, but they have serif fonts.
Update 09/2021: This works on Ubuntu 21.04 as well.
Also works for 22.04 =)
|
57

The error message says you're missing the type1cm package. It seems that MacPorts includes it as part of texlive-latex-extra.

8 Comments

Still applicable on my Ubuntu 13.10 machine. I also needed the dvipng package.
On ubuntu 14.04.1 I needed to install both texlive-latex-extra and texlive-fonts-recommended
Same on ubuntu 15.10 ... However, matplotlib continued using cached versions of plots generated before installing these packages. Before installing these packages, matplotlib would generate plots with no text at all. It took me a little bit to realize the cached versions were being used, but removing the dvi's and pdfs in ~/.cache/matplotlib/tex.cache/ did the trick.
On Ubuntu 16.04, texlive-latex-extra texlive-fonts-recommended and dvipng were needed.
Any idea what to add on a windows machine? tried to add type1cm and it says package not found on miktex. thanks
|
47

I had to install the cm-super package in a ubuntu derivate (jupyter/minimal-notebook which derives from Ubuntu 18.04) $ sudo apt-get install cm-super

5 Comments

Indeed, cm-super was needed on Ubuntu 18.04 along with dvipng texlive-latex-extra texlive-fonts-recommended.
I also needed cm-super on (LM) Ubuntu 18.04
Also did the trick for me. I had previously sudo apt install texlive (which includes texlive-fonts-recommended). Had to install also: cm-super and texlive-latex-extra (with apt-get, for whatever reasons it didn't work with apt).
Installation of cm-super also did the trick for me in Ubuntu 19.10, but with two caveats: (i) now the plot takes way too longer to be rendered, (ii) my font styles has been affected (which I don't want it to happen)
@kmario23 I'm not sure on why it should take longer to render, but for the fonts I assume that it might make sense. Could you check what fonts are used to render the plot without latex (default, or rc('text', usetex=True)), and what fonts are used once you rc('text', usetex=True)?
3

Along with dvipng texlive-latex-extra texlive-fonts-recommended, cm-super was also need in Ubuntu 20.04.

Comments

0

On Manjaro (Arch), I only needed to install texlive-latexextra, which contains the type1cm.sty file.

pacman -S texlive-latexextra

The packages texlive-bin and texlive-core were already installed on my computer and would likely also be required.

Comments

0

In Manjaro, you may also need texlive-fontrecommended.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

This is an Ubuntu directed discussion, but could not find a Windows based solution so far - here goes for anyone wondering how to setup LaTeX for matplotlib in Windows:

  1. Install TeX locally, I went with MikTeX, for reasons given in 6.

  2. Ensure through the installer that the PATH variable in your system is set correctly, e.g. includes the path to the LaTeX executables. A warning will be displayed if that is not the case.

  3. Open MikTex Console, update all packages and copy the path that is given under the 'Settings' -> 'Directories' -> 'Link to target directory'. It might look something like this:
    'C:\Users\a_user\AppData\Local\Programs\MiKTeX\miktex\bin\x64'

  4. In 'Settings' of your system, go to 'Edit the system environment variables', add the path you copied to the 'path' variable under system variables: The window should look like this.

  5. In your python file/jupyter notebook add the following code to ensure tex typesetting:

    import matplotlib.pyplot as plt
    from matplotlib import rcParams
    
    plt.rcParams.update({
        "text.usetex": True,
        "font.family": "palatino"
    })
    
  6. When matplotlib is used to plot, MikTeX will be used as local TeX dependency. Dialog boxes should open, where MikTeX will suggest to install a required package; after installing the last one the plot should be visible with TeX set text.

If you run into this error after all steps:

'RuntimeError: Failed to process string with tex because latex could not be found'

It is likely that the path variable that python 'sees' does not include the path to your LaTeX executable. You can check this by running:

import os
print(os.getenv("PATH"))

Making sure that you path is included can be achieved by:

import os
os.environ["PATH"] += os.pathsep + 'absolute/path/to/your/executables'

Though this will only 'hold' for the current session. In my experience restarting the computer and running print(os.getenv("PATH")) displays the changes that were made recently.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.