# Can we import Python file in latex?

eg. Let say we have a python file songbook-makeindex.py which required input as .xsd file extension and give output as .sbx file extension, which needs to be run inside the latex file.

\documentclass{article}

{here some required packages}

\begin{document}

songbook-makeindex.py english_auth.sxd > english_auth.sbx

songbook-makeindex.py english_title.sxd > english_title.sbx

\end{document}

• I don't understand what you want to do, can you clarify? – Torbjørn T. Apr 27 '17 at 14:59
• You are probably looking for something related to write18, which you can google. – Benjamin McKay Apr 27 '17 at 15:09
• Look at the pythontex package, see e.g. tex.stackexchange.com/questions/65290/embedding-python-in-tikz/… – Torbjørn T. Apr 27 '17 at 15:09
• they are simply running the python code only but i want to import python file and then run on within the latex file. @Torbjørn T – Biki Teron Apr 27 '17 at 15:28
• What does that mean? Import and then run on within latex? But if you want to generate files automatically and include the output in your document, you should probably use a makefile and/or latexmk – DG' Apr 28 '17 at 14:24

It could be a single file for all? I mean LaTeX code plus python code plus python output all mixed in one file with the .Rnw extension.

If so, you must convert it to .tex with R and knitr and then compile the LaTeX file as usual, or munch better, left to Rstudio to take care of the whole process.

Example:

% For .tex output use:
% Rscript -e "library(knitr); knit('filename.Rnw')"
\documentclass[a5paper]{article}
\setlength\parindent{0pt}
\begin{document}

This is not just verbatim text, but  executable python code:
<<test1, echo=T, eval=FALSE, engine='python'>>=
x = 'hello, python world!'
print(x)
print(x.split(' '))
@

And this the phython ouput of the above code from \textsc{this} file:
<<test2, echo=F, comment="", engine='python'>>=
<<test1>>
@

All inside only a \LaTeX\ (R noweb) file.

\end{document}

• Thanking you for reply, but actually i don't need to write the python code inside the latex file only i need to call the python file only @Fran – Biki Teron Apr 30 '17 at 7:33
• @BikiTeron I am not a python expert, but I guess that the python code inside LaTeX file instead of print "hello" is able also of run external scripts, with some like os.system("myscript.py 1"). However, I am not tested that. – Fran Apr 30 '17 at 11:58
• – Fran Apr 30 '17 at 11:59

I think that should be possible using the sagetex package; its documentation is available here on CTAN. The package allows you to run a computer algebra system called Sage. This means Sage needs to be installed locally on your computer or, even easier, you can get a free Cocal account, formerly known as SagemathCloud. The sagetex package is an easy way of getting the power of Python plus all the built in mathematics of Sage. I uploaded a python file to Cocal that calculates the beginning of the Recaman sequence. The code itself isn't important, the important thing is that the results of running the program were contained ni a string called output. The sagesilent block allows Sage code to run but it doesn't show anything in the LaTeX code. In this case, I just loaded the Python program and ran it. In the body of the LaTeX document, the \sagestr command tells Sage to grab the string output and put it into the document. Calculations can be done using the \sage command. You can search this site for sagetex to see more about its use. Note the picture below shows the Python document and LaTeX document are in the same directory.

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}