Printing Code from Python in Latex via "minted"

Click For Summary

Discussion Overview

The discussion revolves around the use of the "minted" package in LaTeX for importing Python code. Participants explore issues related to configuration and alternatives for displaying code effectively in LaTeX documents.

Discussion Character

  • Technical explanation, Debate/contested, Experimental/applied

Main Points Raised

  • One participant encounters an error related to the "minted" package requiring the -shell-escape flag when trying to include Python code in LaTeX.
  • Another participant suggests an alternative method for including Python code in LaTeX, referencing a resource for optimal code inclusion.
  • The original poster expresses dissatisfaction with the alternative method, noting that it resulted in monochromatic code and reiterates their preference for the "minted" package.
  • A later reply indicates that the original poster successfully resolved the issue by running the pdflatex command with the -shell-escape flag from the Terminal.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best method for including Python code in LaTeX, as one participant prefers the "minted" package while another offers an alternative approach. The discussion includes both successful resolution of the initial error and ongoing preferences for code presentation.

Contextual Notes

The discussion includes a specific technical requirement (the -shell-escape flag) that may not be universally applicable, depending on the user's environment and LaTeX setup.

member 428835
Hi PF!

I am trying to import code written in Python into Latex via the "minted" package. However, I am getting the error:

Package minted error: you must invoke latex with the -shell-escape flag.

I tried googling help but I can't figure it out. I'm using Python 2.7 on mac OSX sierra. Any help is greatly appreciated!
 
Physics news on Phys.org
If I understand you correctly, you want to show your Python code in \LaTeX as shown below?

I am not familiar with the minted package you mention, but if you are open to other suggestions I'll tell you what I do: When I have to include e.q. Python code in \LaTeX I use the code found here: https://www.quora.com/What-is-the-optimal-way-to-include-Python-code-in-a-LaTeX-document

If you want other colors for let's say the print function, you can easily customize it to fit your needs at the start of the code. Let me know if you need any help using the code.
main-qimg-3fa6adccc1dc059422e28cadfe7ed471.png
 
Last edited by a moderator:
Thanks for the suggestion. I tried it out, but is was getting very monochromatic code. I'm quite sold on minted if I could just get the darn thing to work.
 
Looks like it's working fine. I just had to run it from Terminal as
$ pdflatex -shell-escape hw2
Thanks for your insight!
 
  • Like
Likes   Reactions: vanhees71 and Baarken

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
15
Views
11K
Replies
7
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K