TeX won't recognize Pygments for use in minted

So a little bit of background: I'm fairly new to LaTeX, I've been writing up homework in it for 3 years and that's it. I'm writing my undergrad thesis with it because it's a departmental requirement, and for part of my thesis, I wrote a Python program. My advisor would like me to include the full code of the program as an appendix to the paper. Since I personally have a ton of trouble reading code that isn't highlighted, I'm attempting to use the minted package to import the code from the .py file directly and add highlighting.

The first roadblock that I hit with this was that I was doing my thesis in ShareLaTeX originally, so in order to use Pygments, which as I understand it from my research is the Python package that allows minted to highlight the code, I had to switch to an offline application, since ShareLaTeX doesn't support that. I spent the last couple hours wrestling with installing and configuring TeXStudio so that I could get my thesis to actually build at all, and at last I have most of it working--except for importing the .py file!

What I've found is that when I comment out the line that imports the .py file, \small{\inputminted[linenos]{python}{../../../example-file.py}}, in my document, everything runs smoothly, however when I leave the line in, it will start to build the document, sometimes put the warnings from the rest of the document in the log, and then stall out and never finish. By "never finish" I mean that it keeps trying to build it forever and never throws any errors.

I tried importing the exact same file to a blank document, and ended up getting the exact original error message that made me eventually conclude I needed to switch to an offline editor in the first place, saying that I'm missing Pygments. This is after installing Pygments for Python by the way.

Here is the test file that is throwing the error:

\documentclass{report}
\usepackage{minted}

\begin{document}

\small{\inputminted[linenos]{python}{example-file.py}}

\end{document}


And here's a screenshot of the error message:

How can I get my Python file to import?

• Can you please specify what error are you getting? – Phelype Oleinik Mar 7 '18 at 11:08
• using \small like that will make your entire document smal, Size commands do not take an argument, the syntax is {\small stuff} not \small{stuff} – David Carlisle Mar 7 '18 at 15:59
• Why do you say sharelatex does not support this? I do not have an account, but their documentation has examples sharelatex.com/learn/Code_Highlighting_with_minted – David Carlisle Mar 7 '18 at 16:01

1 Answer

This error is because minted runs pygments on the background, and to do that it requires that the compiler has access to the shell.

The error message is quite clear:

Package minted Error: You must invoke LaTeX with the -shell-escape flag.

So you'll have to run, for example:

pdflatex -shell-escape my_document.tex

• you mean in the command prompt? – Jules Randolph Mar 7 '18 at 11:29
• Yes, in the command prompt. Depending on the IDE you are using, you can configure it to use this -shell-escape flag. – Phelype Oleinik Mar 7 '18 at 11:48