0

I am considering learning LaTex, and my choice will depend on how I might be able to use it with Python. I am aware of PythonTeX, which lets you use Python code within a LaTeX document.

But is there a way to write the code in a separate .py file, and the somehow import the functions and variable to a LaTeX document to use the results? These results can be int, float or matplotlib plots.

It would be practical to keep the text and the Python code separate but use the results without manually copy/pasting them into the LaTeX document.

  • Have you seen this issue? https://github.com/gpoore/pythontex/issues/89 There is an workaround to add folder with your .py file to PATH. Its cumbersome, but possible. I am affraid that this is for some time as good as it gets ... – Tomáš Kruliš Jun 16 at 8:04
  • 1
    This seems a bit too generic. Can you try and explain some examples of what you mean? – egreg Jun 16 at 15:56
2

I'm not entirely sure how to answer that so let me start from the very start.

Fundamentally, Latex is a language that allows you two define, format, and write text documents. You don't have objects and functions like in most programming languages. In the end, it is compiled to a static document. Therefore, I'm not sure why you are interested in a python integration.

You could write a Python program that generates a static tex file, which is then compiled to a pdf (if you use pdflatex). But that is cumbersome and more work than writing the latex file in the first place.

There might be an edge case where dynamic programming elements are useful, but tex does support some basic programming capabilities. SO, I don't really see how it could be useful.

Besides the question of usefulness, I understand that some people use Python to generate data/plots for their documents. Most people I know, generate plots/data with python and then import the plots or csv files in latex. There is no copy/pasting involved.

I've seen that PythonTex is designed for cases where you would like to add pyhton code to your document, which is fine. However, as it is in most programming languages, I wouldn't recommend having one large file containing all the code. It's just bad practice. But, if you use separate tex file anyway, there is no benefit over having separate python and tex files. You can automate the compilation process with MAKE or CMAKE (or any other building environment of your choice).

To answer your question, I don't think there is a Python integration in Latex besides PythonTex. I don't think that you could use an \import or \include command within the pycode environment. (I haven't tested it though). SO I don't see how you can do it from a latex side. Having said that, you might be able to import from a file with Python code within a latex document. (see here).

If I can give you a personal advice: I would recommend learning Latex, if you write a large document (book, thesis, ..) or publications (conferences or journals). Latex looks generally better and is more stable than MS Word/Libreoffice/Openoffice. YES, it is awkward to learn and has a steep learning curve. But it works well. I, personally, use it for most of my documents in personal and professional life.

I use MAKE files to compile my documents (which includes: running simulations written in C, executing R files, generate tables and plots, and finally compile tex). It works well.

| improve this answer | |
  • Just to ask on side of Windows users, what alternatives there would be in wondows (which to my knowledge does not have make or other similar tools)? – Tomáš Kruliš Jun 16 at 7:19
  • Yes, Sorry, I didn't think of Windows. Hmm ... Have a look here link. There might be alternatives for Windows – Stefan Jun 16 at 15:18
  • @TomášKruliš latexmk is the TeX version of make and comes with most distributions. – Teepeemm Jun 16 at 16:16
  • @TeePeemm True but you couldn't run python or other scripts that generate stuff for you with it. – Stefan Jun 17 at 20:42
0

The sagetex package gives you access to a computer algebra system, Sage, as well as Python programming. It will let you use string data from an external Python file as seen in my answer to Can we import Python file in latex?. I am unable to get it to pass variables or functions, however, from an external file. The ability to pass a string of LaTeX code is quite useful. I take that approach (not using an external file) in many problems. See my approach to plotting the Cantor function where the output string is created using Python and Sage and inserted via \sagestr. This gives you LaTeX quality plots for your data. A similar idea of creating a Trig table here using Python/Sage to create the string quickly and easily gets the heavy lifting done.

With respect to matplotlib see here; its not done using an external file though. It's easy enough to use Python within LaTeX, for example, blurring an image. In an integrated environment though you can combine Sage calculations plus Python programming to increase the accuracy of a tikz plot. Since you're getting a CAS, sagetex gives you even more tools for mathematical programming.

The Cocalc platform "supports running Python, SageMath, R Statistical Software, Julia, and more in the same project as your LaTeX document.", It also supports pythontex and sagetex. There's also a lot of support for Python even if you aren't going to use LaTeX.

You can do a lot with Python raw strings from an external Python file. When working with Python is easy from within LaTeX, however, I think working with Python using external files would be a bit cumbersome.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.