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
4
  • 1
    I don't see a tilde ~ do you mean the left quote ` ? Have you used pdflatex --shell-escape to allow python to run? Sep 15, 2023 at 6: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, 2023 at 15:56
  • ^^M rather unexpected, have you transferred the file from windows? Sep 16, 2023 at 4:50
  • Sorry but I don't understand. I work under Debian Testing only. No Windows.
    – Charlie
    Sep 16, 2023 at 14:37

2 Answers 2

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

3
  • 1
    it works with latex input as well Sep 15, 2023 at 6:14
  • 1
    \documentclass{article} \begin{document} a\input{|python3 -c 'print(1+2)'}b a\input{|python3 script.py}b \end{document} Sep 15, 2023 at 8: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, 2023 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

4
  • 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, 2023 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 Sep 15, 2023 at 16:34
  • @Charlie if you have an old latex you may need double quotes as used in the other answer so \input{"|......."} Sep 15, 2023 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, 2023 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .