4

What is the best way to insert a diagram generated by TeX e.g. via TikZ or similar, in a Google Colab notebook? Tried inserting code for TikZ but that was not recognised. Any suggestions would be appreciated. Thanks.

  • Maybe converting to SVG would be an option? See for example https://tex.stackexchange.com/questions/51757/how-can-i-use-tikz-to-make-standalone-svg-graphics – Marijn Aug 19 '20 at 08:06
  • Can you import pdf or images with Colab? – vi pa Aug 19 '20 at 08:08
  • Hi and welcome. What's a *Google Colab notebook*? – AndréC Aug 19 '20 at 08:24
  • Well, Colab does let you install stuff with pip (https://colab.research.google.com/notebooks/snippets/importing_libraries.ipynb), and there is an option for using TikZ directly in a Notebook (https://github.com/mkrphys/ipython-tikzmagic), but it didn't work for me, could be that a complete tex installation is not available in Colab. Perhaps https://stackoverflow.com/a/30334730 for SVG, or use the button in the interface to add a PNG. – Torbjørn T. Aug 19 '20 at 14:11

2 Answers2

7

Had the same question, figured out the rest. Colab supports apt in addition to pip installs, so it was a matter of figuring out which additional packages were needed to work with the ipython-tikzmagic package Torbjorn recommended.

I found also that I have to specify a "-f svg" option in order for it to detect the image generated by tikz. I suspect that a small code change to ipython-tikzmagic would fix this, or I chose a latex install that behaves slightly differently than the one ipython-tikzmagic assumes.

Here's an example colab notebook.

[Edit: Adding notebook commands here per commenter's request. Cell contents separated by whitespace. This worked as of 12/1/2020, but likely will at some point break in the future due to packages evolving.]

!apt update

!apt install imagemagick

!apt install pdf2svg

!apt install texlive texlive-latex-extra

!pip install git+git://github.com/mkrphys/ipython-tikzmagic.git

%load_ext tikzmagic

%%tikz -S test_output.tikz -f svg \draw (0,0) rectangle (1,1); \filldraw (0.5,0.5) circle (.1);

https://colab.research.google.com/drive/1GNczHdylcfYDFC1HpcloDRJxnmKpCuDO?usp=sharing

Would love to see ipython-tikzmagic to work within markdown.

  • Nice! The answer would be even better if you were to say in the text exactly what you needed to install and how, and where to specify -f svg (in case you delete that example notebook, or the link dies). – Torbjørn T. Oct 09 '20 at 14:31
  • !apt install imagemagick !apt install pdf2svg !apt install texlive texlive-latex-extra !pip install git+git://github.com/mkrphys/ipython-tikzmagic.git %load_ext tikzmagic %%tikz -S test_output.tikz -f svg \draw (0,0) rectangle (1,1); \filldraw (0.5,0.5) circle (.1); – msm1089 Nov 01 '20 at 19:48
  • This doesn't work for me, when I click on "run all" I get something like
    No image generated.
    ---------------------------------------------------------------------------
    FileNotFoundError                         Traceback (most recent call last)
    <ipython-input-22-14695ad3262e> in <module>()
    ----> 1 get_ipython().run_cell_magic('tikz', '-S test_output.tikz -f svg', '\\draw (0,0) rectangle (1,1);\n\\filldraw (0.5,0.5) circle (.1);')
    
    ]
    FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpxxov9ug9/tikz.svg'```
    
    – Rainb Nov 30 '20 at 11:34
  • @rainb, I reran it and found the same. Earlier in the notebook, pdf2svg wasn't being installed. I added an !apt update at the beginning and this resolved the pdf2svg install. Notebook is updated. – jrjbertram Dec 01 '20 at 12:57
1

Thanks for all the answers. For future reference I ran into the issue that the PIP installation of tikzmagic on google colab failed (@time 2023-04-2). Found a fix on the git repo . To install with PIP 'recommend:

!pip install git+https://github.com/mkrphys/ipython-tikzmagic.git
  • 1
    This would be more helpful as an edit to the other answer, you can click "improve this answer" under the existing answer to suggest an edit yourself. – Dai Bowen Apr 02 '23 at 17:28