Site Overlay

Import Geogebra plots to LaTeX

A couple of years ago I recommended using gnuPlot for producing high quality plots of functions and time series. There has been a lot going on in data visualization since then and while I gnuplot still remains my favourite plotter, there has been an interesting addition for fast and frugal function plotting in the field of high school mathematics (and also geometry): GeoGebra.

The question is: Is there a way to import Geogebra output into a LaTeX document. Not as a PNG of course but asLaTeX compliant code(like TikZ or PSTricks). The answer is – yes, there are even a few ways to skin that cat…

Local Geogebra Installation needed

First thing you need to know: You cannot export to LaTeX code with the online version. You need a local installation of a so called Geogebra Classic on your machine. Geogebra Classic is kind of Geogebra online plus some more features – and among these some more features is an option to export as TikZ.

So go to Geogebra’s download page and get the install files for your OS.

Don’t Say Export – Say Download as

The instructions in the Geogebra Wiki are apparently outdated and refer to a previous version of Geogebra Classic:

Just drag a selection rectangle around the portion of the construction you need to export (or resize the GeoGebra window), then select File -> Export -> Graphics View as

Geogebra Classic Wiki

This won’t work because File -> Export will force you to export your result as a PNG file – but not as TikZ code.

The correct way is to Choose File -> Download as -> and then pick export as PGF / TikZ. I first tried PS Tricks but failed to integrate it correctly into my LaTeX code.

The resulting PGF / TikZ file is a text file – just what we need to put it into our LaTeX code.

Texmaker users

Geogebra suggests TikZ export files to be saved with a .txt extension. Those who use Texmaker might find it expedient to change the file extension into .tex before saving. This facilitates the opening and editing and compilation in Texmaker.

Texmaker users

Geogebra suggests TikZ export files to be saved with a .txt extension. Those who use Texmaker might find it expedient to change the file extension into .tex before saving. This facilitates the opening and editing and compilation in Texmaker.

Open export file in Texmaker

Lets assume our export file geogebra.tikz looks as follows:

As you can see, PGF/TikZ has produced a standalone LaTeX code that you can directly compile to a DVI, PS, PDF or whatever you desire as a target system.

Ubuntu 20.04 LTS users need to edit code

Although the above code is in line with latest TikZ coding standards, LaTeX will produce an error. This is because the default Ubuntu software sources install a TeX Live 2019 installation which has outdated coding standards for TikZ. Read below what to do.

Ubuntu 20.04 LTS users need to edit code

Although the above code is in line with latest TikZ coding standards, LaTeX will produce an error. This is because the default Ubuntu software sources install a TeX Live 2019 installation which has outdated coding standards for TikZ. Read below what to do.

Editing needed for default LaTeX in Ubuntu 20.04 LTS

In case you are running a default LaTeX installation from the Ubuntu Software Sources, your LaTeX run will probably stall with a cryptic error message like this one:

A posting on Stack Overflow has the solution. The circle commands in lines 24 and 26 follow TeX Live 2020 syntax conventions, while Ubuntu’s apt installs Tex Live 2019. Therefore we have to edit our Geogebra export file and replace each line with slightly modified circle command. Line 24 is changed from:

to…

Line 26 has to be changed the same way. You can use a regex-based command to change each occurence of the circle command from the current TeX Live 2020 syntax to the deprecated 2019 syntax in the geogebra.tex file or use Texmaker’s regex based Replace All feature:

Once you have rolled back the circle commands to the deprecated TeX Live 2019 syntax that your Ubuntu LaTeX installation uses, you can produce a DVI, PS or PDF from that code. The result should be a single neat page with the plot you exported.

Integrating plots into other TeX documents

Normally though, you do not want to produce a standalone document but you want your graphics to appear in another TeX document (e.g. a sheet with solutions to homework assignments), which I will call the “master document”. In order to achieve this, you have to:

  • Copy lines 2-5 from geogebra.tex file into the preamble of your master document. These lines contain package imports and settings TikZ needs for plotting
  • Copy any \definecolor commands like the one in line 8 from your geogebra.tex file into the section following the \begin{document} tag in your master document.
  • Copy the complete section starting from \begin{tikzpicture} to \end{tikzpicture} to the desired place in your master document. If you want, you can put the document into a figure environment and scale it as needed.

You should now be able to tex your master document with a neat sketch exported from Geogebra.