Git Product home page Git Product logo

pythontex's Introduction

PythonTeX

Execute Python and other code in LaTeX documents, or typeset it with syntax highlighting

PythonTeX executes code in LaTeX documents and allows the output to be included in the original document. It supports Python as well as Bash, JavaScript, Julia, Octave, Perl, R, Raku (Perl 6), Ruby, Rust, and SageMath. PythonTeX also provides syntax highlighting for typeset code in LaTeX documents via the Pygments syntax highlighter.

See pythontex_quickstart.pdf to get started, and pythontex_gallery.pdf for examples of what is possible with PythonTeX. PythonTeX is included in TeX Live and MiKTeX and may be installed via the package manager. See pythontex.pdf for detailed installation instructions if you want to install the current development version, or just use the installation script for TeX Live and MiKTeX.

The depythontex utility creates a copy of a PythonTeX document in which all code has been replaced by its output. This plain LaTeX document is more suitable for journal submission, sharing, or conversion to other document formats. See pythontex_gallery.html and the accompanying conversion script for an example of a PythonTeX document that was converted to HTML via depythontex and Pandoc.

Example

  • LaTeX document doc.tex:

    \documentclass{article}
    
    \usepackage{pythontex}
    
    \newcommand{\pymultiply}[2]{\py{#1*#2}}
    
    \begin{document}
    
    \begin{pycode}
    print("Python says ``Hello!''")
    \end{pycode}
    
    $8 \times 256 = \pymultiply{8}{256}$
    
    \end{document}
  • Compiling under Windows:

    pdflatex -interaction=nonstopmode doc.tex
    pythontex doc.tex
    pdflatex -interaction=nonstopmode doc.tex
    
  • Compiling under other operating systems:

    pdflatex -interaction=nonstopmode doc.tex
    pythontex.py doc.tex
    pdflatex -interaction=nonstopmode doc.tex
    
  • Output:

    Python says “Hello!”
    8 × 256 = 2048
    

Notice that there is a three-step compile process. This is what makes possible commands like \pymultiply that use Python or other languages internally. You may want to configure your LaTeX editor with a shortcut for running pythontex or pythontex.py, or configure your LaTeX build system to run pythontex or pythontex.py.

Citing PythonTeX

If you use PythonTeX in your writing and research, please consider citing it in any resulting publications. The best and most recent paper is in Computational Science & Discovery (doi:10.1088/1749-4699/8/1/014010). You may also cite the paper in the 2013 SciPy proceedings.

License

LPPL for LaTeX code and BSD 3-Clause for Python code.

pythontex's People

Contributors

beastyblacksmith avatar gpoore avatar nathancarter avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.