Skip to content

Instantly share code, notes, and snippets.

@user202729
Last active January 12, 2026 12:00
Show Gist options
  • Select an option

  • Save user202729/34c1243cf3f612332dad60760054c3ed to your computer and use it in GitHub Desktop.

Select an option

Save user202729/34c1243cf3f612332dad60760054c3ed to your computer and use it in GitHub Desktop.
sympytex on overleaf

Use sympytex on overleaf

This is not officially supported, but you can made it to work...

  • first, see the test.tex file below (because of an annoying GitHub gist "bug", all files following a large raw file are shown as "cannot display file this big...", thus the "a-" prefix)

  • download sympy.zip attached below in the gist, and upload it to your overleaf.

    If you don't trust downloading random files from random sources, you can make the file yourself -- otherwise just ignore this section. This file was prepared by

    # <install python2.7 somehow>
    python2 -m ensurepip --upgrade --user
    
    cd /tmp/
    virtualenv  -p /bin/python2.7 zzt  # create a Python 2.7 virtual environment
    cd zzt
    . bin/activate
    pip2 install sympy  # install sympy and mpmath
    pip2 install matplotlib  # matplotlib backend for sympy for plotting
    
    mkdir /tmp/new; cd /tmp/new  # change to some new empty directory
    cp  -r /tmp/zzt/lib/python2.7/site-packages/*  .
    rm -r pip setuptools pkg_resources wheel
    
    # a weird hack to allow importing mpl_toolkits
    touch mpl_toolkits/__init__.py
    
    # minimizing the resulting file
    find . -name __pycache__ -exec rm  -r {} \;
    find . -name '*.pyc' -exec rm  -r {} \;
    find . -name 'tests' -exec rm  -r {} \;
    
    # then create sympy.zip in /tmp/
    rm /tmp/sympy.zip
    #zip -r /tmp/sympy.zip *
    7z a -tzip -mx=9 /tmp/sympy.zip *  # generates smaller zip file, needed if close to 50MB limit of overleaf

    (by the way Overleaf properly sandbox the environment, so even if there's malicious code it won't be able to do anything such as delete your TeX file or leak it to the Internet)

  • copy the initial lines of test.tex to your file

    \usepackage{shellesc}
    
    \ShellEscape{unzip sympy.zip </dev/null  >/dev/null 2>&1}  % </dev/null, it will only attempt to unzip once then ignore in following attempts
    
    \ShellEscape{
    PYTHONPATH=$(kpsewhich -var-value TEXMFDIST)/scripts/sympytexpackage/ python2 \jobname.sympy  >tmpoutput.txt 2>&1 #$}
    
    \usepackage{sympytex}

    We need unzip instead of just add the zip file to PYTHONPATH to allow importing .so file for numpy.

  • add the .latexmkrc file to your project

  • In Overleaf, at the moment if you select "TeX Live version" to be "2022" then it will show the error message python2: command not found, which I'm not sure why. Just use version 2021 as a workaround.

  • then you can use sympytex as usual.

    If you want to plot remember to select AGG backend (see the test file below), otherwise it will break because tkinter is not installed.

Furthermore, it cannot detect changes, so the first compilation after "Recompile from scratch" will not work...?

system("unzip sympy.zip </dev/null >/dev/null 2>&1");
# write </dev/null so it will only attempt to unzip once then ignore in following attempts
$texmfdist = `kpsewhich -var-value TEXMFDIST`;
chomp($texmfdist); # to remove trailing newline
sub sympytexsub {
return system("PYTHONPATH='$texmfdist'/scripts/sympytexpackage/ python2 '$_[0]'.sympy >tmpoutput.txt 2>&1");
}
add_cus_dep('sympy', 'sout', 0, 'sympytexsub');
\documentclass{article}
\usepackage{shellesc}
\usepackage{sympytex}
% for debugging purpose
\usepackage{fvextra}
\fvset{breaklines=true, breakanywhere=true}
\newcommand\verbatimInputIfExists[1]{\IfFileExists{#1}{\VerbatimInput{#1}}{}}
\AtEndDocument{%
% we force raise a message like this to make latexmk generate the .sout file
% the string "No file" is needed to make latexmk recognize it
\IfFileExists{\jobname.sout}{}{\typeout{No file \jobname.sout.}}%
}
\begin{document}
$5^4=\sympy{5**4}$
\begin{sympysilent}
from sympy import *
var("x")
import matplotlib
matplotlib.use("AGG")
\end{sympysilent}
\sympyplot[angle=30, width=5cm]{plot(sin(x), 0, pi), axes=False, chocolate=True}
Debug:
\verbatimInputIfExists{tmpoutput.txt}
\end{document}
This file has been truncated, but you can view the full file.
Copy link

ghost commented Mar 7, 2023

Hi, I've tried the script and couldn't figure out how to get past the error saying 'python2 not found', because i'm not sure how virtual environments work. Is there any chance you could make an overleaf example and share it? Thanks

@user202729
Copy link
Author

user202729 commented Mar 7, 2023

@jackw1111

Hi, I've tried the script and couldn't figure out how to get past the error saying 'python2 not found', because i'm not sure how virtual environments work. Is there any chance you could make an overleaf example and share it? Thanks

Fixed, see the latest change.

(advertisement: I have another package that allows you to use Python on Overleaf https://github.com/user202729/pythonimmediate-tex -- which has some advantages (e.g. faster compilation time, Python 3 support) and disadvantages so depends on what you need you may consider that)

@asierrayk
Copy link

Hi, thanks for the guide. Everything is working for me except the plot.
For example, executing:
\sympyplot[angle=30, width=5cm]{plot(sin(x), 0, pi), axes=False, chocolate=True}

gives me the following error:

ValueError: The TextBackend supports only one graph per Plot

@user202729
Copy link
Author

user202729 commented May 13, 2023

Looks difficult. Fundamentally I need to install matplotlib to allow plotting https://docs.sympy.org/latest/modules/plotting.html, but matplotlib depends on some other back end https://matplotlib.org/stable/users/explain/backends.html and I haven't really managed to get any of them working.

pgf seems to be the easiest to get work, but

File "/compile/subprocess32.py", line 1630, in
_communicate_with_poll↪→
ready = poller.poll(self._remaining_time(endtime))
error: (1, 'Operation not permitted')

Edit: never mind, I got AGG backend to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment