GeneralHow to embed python in latex?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
luofeiyu
Posts: 3
Joined: Wed Aug 23, 2017 8:36 am

How to embed python in latex?

Post by luofeiyu »

For the article here http://www.texample.net/weblog/2008/oct ... hon-latex/.

Let’s start with a basic “hello world” example:

%& -shell-escape
\documentclass{article}
\usepackage{python}
\begin{document}

Say hello Python:

\begin{python}%
print r"Hello \LaTeX!"
\end{python}%
\end{document}

Compiling the above code with shell escape enabled gives the following output:
Say Hello world!
Hello Latex


How to compile it?
vim test.latex

%& -shell-escape
\documentclass{article}
\usepackage{python}
\begin{document}

Say hello Python:

\begin{python}%
print r"Hello \LaTeX!"
\end{python}%
\end{document}

Which shell command to compile the test.latex file?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How to embed python in latex?

Post by Stefan Kottwitz »

Hi,

compile it by

pdflatex -shell-escape test.latex

By the way, the file name extension is usually .tex.

Stefan
LaTeX.org admin
luofeiyu
Posts: 3
Joined: Wed Aug 23, 2017 8:36 am

How to embed python in latex?

Post by luofeiyu »

pdflatex -shell-escape test.latex
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2015/dev/Debian) (preloaded format=pdflatex)
\write18 enabled.
entering extended mode
(./test.latex
LaTeX2e <2014/05/01>
Babel <3.9l> and hyphenation patterns for 2 languages loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texlive/texmf-dist/tex/latex/python/python.sty) (./test.aux)

! LaTeX Error: Environment /usr/bin/python undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...

l.8 \begin{/usr/bin/python}
%
?


! LaTeX Error: Environment /usr/bin/python undefined.
How to fix then?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

How to embed python in latex?

Post by Stefan Kottwitz »

That line is wrong:

\begin{/usr/bin/python}

It has to be:

\begin{python}

The same with \end.

Stefan
LaTeX.org admin
Post Reply