0

Im trying to execute a python script from within LaTeX by means of using the \input command, however I get a strange error that I can't really understand. Im following the working example taken from this answer but it simply won't work.

Example 1: \input{|python3 -c 'print(1+2)'} gives the error ! LaTeX Error: File `|python3 -c 'print(1+2)'.tex' not found. Note there's a tilde there after just before the |

Example 2: \input{|python3 test.py} gives the error ! LaTeX Error: File `|python3 test.py' not found.

Note again there's a tilde there after just before the |

Im currently using PDFLaTeX with the --shell-escape flag, but I can't really make it work.

What am I doing wrong? Here is the full code:

\documentclass[10pt, export]{standalone}
\usepackage{tikz}
\usepackage{animate}
\usepackage{amsmath}
\usepackage[left=0cm, top=0cm, bottom=-2cm, right=0cm, paperwidth=12in, paperheight=6.75in]{geometry}
\usepackage{circuitikz}

\begin{document}

\input{|python3 -c 'print(1+2)'} \input{|python3 test.py}

\end{document}

Best regards and thanks in advance

UPDATE:

Trying David's answer produces this output:

! LaTeX Error: File `|python3 -c 'print(1+2)'.tex' not found.

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

Enter file name: ! Emergency stop. <read *>

l.10 1+2 = \input{|python3 -c 'print(1+2)'} ^^M ! ==> Fatal error occurred, no output PDF file produced!

These are the flags I'm using with PDFLatex under Kile:

--shell-escape -synctex=1 -interaction=nonstopmode %source

The same is output when compiled from the terminal as follows:

pdflatex --shell-escape -interaction=nonstopmode test.tex
Charlie
  • 1,135
  • 1
    I don't see a tilde ~ do you mean the left quote \`` ? Have you usedpdflatex --shell-escape` to allow python to run? – David Carlisle Sep 15 '23 at 06:06
  • Hello @DavidCarlisle, thanks for your answer. Yes, I meant the left quote (I can't reproduce it on my spanish keyboard :( ) As I described in my question Im using the --shell-escape both in kile as well as from the terminal (tried both). – Charlie Sep 15 '23 at 15:56
  • ^^M rather unexpected, have you transferred the file from windows? – David Carlisle Sep 16 '23 at 04:50
  • Sorry but I don't understand. I work under Debian Testing only. No Windows. – Charlie Sep 16 '23 at 14:37

2 Answers2

3

It works if you use TeX's primitive \input, which is saved in LaTeX as \@@input:

This is script.py:

print("Number four: %d." % 4)

This works:

\documentclass{article}

\makeatletter % to use @@input \def\runshell#1{% @@input"|#1"\relax } \makeatother

\begin{document} a\runshell{python3 -c 'print(1+2)'}b

a\runshell{python3 script.py}b

\end{document}

enter image description here

  • 1
    it works with latex input as well – David Carlisle Sep 15 '23 at 06:14
  • 1
    `\documentclass{article} \begin{document} a\input{|python3 -c 'print(1+2)'}b

    a\input{|python3 script.py}b \end{document}`

    – David Carlisle Sep 15 '23 at 08:15
  • 1
    Thanks for your answer. I tried your solution and it works, but I can't really understand why it won't work as in @DavidCarlisle's answer. I will be marking this as solved, until I get new updates. – Charlie Sep 15 '23 at 16:05
1

You need --shell-escape and remove the geometry setting which is forcing the text off the page;

\documentclass[10pt, export]{standalone}
\usepackage{tikz}
\usepackage{animate}
\usepackage{amsmath}
%\usepackage[left=0cm, top=0cm, bottom=-2cm, right=0cm, paperwidth=12in, paperheight=6.75in]{geometry}
\usepackage{circuitikz}

\begin{document}

1+2 = \input{|python3 -c 'print(1+2)'}

\end{document}

enter image description here

David Carlisle
  • 757,742
  • I tried your example but it won't work. The problem persists. Im currently using debian testing and Im starting to wonder if this could be the reason behind the problem. – Charlie Sep 15 '23 at 16:01
  • @Charlie this works, as does the version I added as a comment in the other answer, you could show the log you get from this answer – David Carlisle Sep 15 '23 at 16:34
  • @Charlie if you have an old latex you may need double quotes as used in the other answer so \input{"|......."} – David Carlisle Sep 15 '23 at 16:42
  • please see my updated answer. BTW: I have tried double quotes as well, but nothing changed :( thanks for the tip though. – Charlie Sep 15 '23 at 20:26