{"id":1286,"date":"2021-01-20T02:32:48","date_gmt":"2021-01-20T01:32:48","guid":{"rendered":"http:\/\/hobbykeller.spdns.de\/?p=1286"},"modified":"2021-01-20T02:33:07","modified_gmt":"2021-01-20T01:33:07","slug":"import-geogebra-plots-to-latex","status":"publish","type":"post","link":"https:\/\/hobbykeller.spdns.de\/?p=1286","title":{"rendered":"Import Geogebra plots to LaTeX"},"content":{"rendered":"\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile is-vertically-aligned-center\" style=\"grid-template-columns:15% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"85\" height=\"77\" src=\"http:\/\/hobbykeller.spdns.de\/wp-content\/uploads\/2021\/01\/geogebra-1.png\" alt=\"\" class=\"wp-image-1326 size-full\"\/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\">A couple of years ago I recommended using <a href=\"http:\/\/hobbykeller.spdns.de\/?p=322\">gnuPlot<\/a> 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): <a rel=\"noreferrer noopener\" href=\"https:\/\/geogebra.org\" target=\"_blank\">GeoGebra<\/a>.<\/p>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8211; yes, there are even a few ways to skin that cat\u2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Local Geogebra Installation needed<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8211; and among these some more features is an option to export as TikZ.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So go to <a href=\"https:\/\/wiki.geogebra.org\/en\/Reference:GeoGebra_Installation#GeoGebra_Classic_6\" data-type=\"URL\" data-id=\"https:\/\/wiki.geogebra.org\/en\/Reference:GeoGebra_Installation#GeoGebra_Classic_6\">Geogebra&#8217;s <\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/wiki.geogebra.org\/en\/Reference:GeoGebra_Installation#GeoGebra_Classic_6\" data-type=\"URL\" data-id=\"https:\/\/wiki.geogebra.org\/en\/Reference:GeoGebra_Installation#GeoGebra_Classic_6\" target=\"_blank\">download<\/a><a href=\"https:\/\/wiki.geogebra.org\/en\/Reference:GeoGebra_Installation#GeoGebra_Classic_6\" data-type=\"URL\" data-id=\"https:\/\/wiki.geogebra.org\/en\/Reference:GeoGebra_Installation#GeoGebra_Classic_6\"> page<\/a> and get the install files for your OS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Don&#8217;t Say <em>Export<\/em> &#8211; Say <em>Download as<\/em><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The instructions in the Geogebra Wiki are apparently outdated and refer to a previous version of Geogebra Classic:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Just drag a selection rectangle around the portion of the construction you need to export (or resize the GeoGebra window), then select File -&gt; Export -&gt; Graphics View as<\/p><p><\/p><cite><strong><a href=\"https:\/\/wiki.geogebra.org\/en\/Export_to_LaTeX_(PGF,_PSTricks)_and_Asymptote\" data-type=\"URL\" data-id=\"https:\/\/wiki.geogebra.org\/en\/Export_to_LaTeX_(PGF,_PSTricks)_and_Asymptote\" target=\"_blank\" rel=\"noreferrer noopener\">Geogebra Classic Wiki<\/a><\/strong><\/cite><\/blockquote>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\" style=\"grid-template-columns:35% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"290\" src=\"http:\/\/hobbykeller.spdns.de\/wp-content\/uploads\/2021\/01\/geogebra2.resized-300x290.png\" alt=\"\" class=\"wp-image-1331 size-medium\" srcset=\"https:\/\/hobbykeller.spdns.de\/wp-content\/uploads\/2021\/01\/geogebra2.resized-300x290.png 300w, https:\/\/hobbykeller.spdns.de\/wp-content\/uploads\/2021\/01\/geogebra2.resized.png 315w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\">This won&#8217;t work because File -&gt; Export will force you to export your result as a PNG file &#8211; but not as TikZ code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The correct way is to Choose File -&gt; Download as -&gt; and then pick export as PGF \/ TikZ. I first tried PS Tricks but failed to integrate it correctly into my LaTeX code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The resulting PGF \/ TikZ file is a text file &#8211; just what we need to put it into our LaTeX code. <\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-essential-blocks-infobox\"><a style=\"display:none\"><div class=\"infobox-container\" style=\"flex-direction:column;background-image:none;background-color:#edf1f7;background-size:unset;cursor:default;margin:0px 0px 0px 0px ;padding:30px 70px 30px 70px ;border:0px solid #000000;border-radius:0px;box-shadow:0px 0px 0px 0px #000000\"><div class=\"infobox-image-wrapper\" style=\"height:150px;width:150px;background-size:cover;order:0;margin-top:10px;margin-right:0;margin-bottom:0;margin-left:0;display:none\" data-image-url=\"\"><\/div><span style=\"display:block;order:0;color:#fff;background:#3074ff;font-size:36px;padding-top:20px;padding-right:30px;padding-bottom:20px;padding-left:30px;line-height:1;border-radius:5px\"><span class=\"fas fa-info-circle infobox-icon\" data-icon=\"fas fa-info-circle\"><\/span><\/span><div class=\"infobox-number\" style=\"display:none;order:0;font-size:36px\"><\/div><h3 style=\"font-weight:normal;text-transform:none;color:#4a5059;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px\">Texmaker  users<\/h3><p style=\"font-size:16px;font-weight:normal;text-transform:none\">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.<\/p><\/div><\/a><div class=\"infobox-container\" style=\"flex-direction:column;background-image:none;background-color:#edf1f7;background-size:unset;cursor:default;margin:0px 0px 0px 0px ;padding:30px 70px 30px 70px ;border:0px solid #000000;border-radius:0px;box-shadow:0px 0px 0px 0px #000000\"><div class=\"infobox-image-wrapper\" style=\"height:150px;width:150px;background-size:cover;order:0;margin-top:10px;margin-right:0;margin-bottom:0;margin-left:0;display:none\" data-image-url=\"\"><\/div><span style=\"display:block;order:0;color:#fff;background:#3074ff;font-size:36px;padding-top:20px;padding-right:30px;padding-bottom:20px;padding-left:30px;line-height:1;border-radius:5px\"><span class=\"fas fa-info-circle infobox-icon\" data-icon=\"fas fa-info-circle\"><\/span><\/span><div class=\"infobox-number\" style=\"display:none;order:0;font-size:36px\"><\/div><h3 style=\"font-weight:normal;text-transform:none;color:#4a5059;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px\">Texmaker  users<\/h3><p style=\"font-size:16px;font-weight:normal;text-transform:none\">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.<\/p><div class=\"infobox-button\" style=\"display:none;align-self:center;margin-top:10px\"><a class=\"eb-infobox-link\" style=\"text-decoration:none;background:#3074ff;color:#ffffff;padding-top:12px;padding-right:28px;padding-bottom:12px;padding-left:28px\">Click Me<\/a><\/div><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Open export file in Texmaker<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Lets assume our export file geogebra.tikz looks as follows:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\\documentclass[10pt]{article}\n\\usepackage{pgfplots}\n\\pgfplotsset{compat=1.15}\n\\usepackage{mathrsfs}\n\\usetikzlibrary{arrows}\n\\pagestyle{empty}\n\\begin{document}\n\\definecolor{ududff}{rgb}{0.30196078431372547,0.30196078431372547,1}\n\\begin{tikzpicture}[line cap=round,line join=round,&gt;=triangle 45,x=1cm,y=1cm]\n\\begin{axis}[\nx=1cm,y=1cm,\naxis lines=middle,\nymajorgrids=true,\nxmajorgrids=true,\nxmin=-5.524545454545454,\nxmax=6.657272727272723,\nymin=-5.484380165289265,\nymax=6.730495867768597,\nxtick={-5,-4,...,6},\nytick={-5,-4,...,6},]\n\\clip(-5.524545454545454,-5.484380165289265) rectangle (6.657272727272723,6.730495867768597);\n\\draw [line width=2pt,domain=-5.524545454545454:6.657272727272723] plot(\\x,{(--6-2*\\x)\/4});\n\\begin{scriptsize}\n\\draw [fill=ududff] (-3,3) circle (2.5pt);\n\\draw[color=ududff] (-2.8633884297520673,3.3503305785123945) node {$A$};\n\\draw [fill=ududff] (1,1) circle (2.5pt);\n\\draw[color=ududff] (1.1366115702479314,1.3503305785123931) node {$B$};\n\\draw[color=black] (-5.375785123966942,4.110661157024793) node {$f$};\n\\end{scriptsize}\n\\end{axis}\n\\end{tikzpicture}\n\\end{document}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<div class=\"wp-block-essential-blocks-infobox\"><a style=\"display:none\"><div class=\"infobox-container\" style=\"flex-direction:column;background-image:none;background-color:#edf1f7;background-size:unset;cursor:default;margin:0px 0px 0px 0px ;padding:30px 70px 30px 70px ;border:0px solid #000000;border-radius:0px;box-shadow:0px 0px 0px 0px #000000\"><div class=\"infobox-image-wrapper\" style=\"height:150px;width:150px;background-size:cover;order:0;margin-top:10px;margin-right:0;margin-bottom:0;margin-left:0;display:none\" data-image-url=\"\"><\/div><span style=\"display:block;order:0;color:#fff;background:#3074ff;font-size:36px;padding-top:20px;padding-right:30px;padding-bottom:20px;padding-left:30px;line-height:1;border-radius:5px\"><span class=\"fas fa-exclamation-triangle infobox-icon\" data-icon=\"fas fa-exclamation-triangle\"><\/span><\/span><div class=\"infobox-number\" style=\"display:none;order:0;font-size:36px\"><\/div><h3 style=\"font-weight:normal;text-transform:none;color:#4a5059;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px\">Ubuntu 20.04 LTS users need to edit code<\/h3><p style=\"font-size:16px;font-weight:normal;text-transform:none\">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.<\/p><\/div><\/a><div class=\"infobox-container\" style=\"flex-direction:column;background-image:none;background-color:#edf1f7;background-size:unset;cursor:default;margin:0px 0px 0px 0px ;padding:30px 70px 30px 70px ;border:0px solid #000000;border-radius:0px;box-shadow:0px 0px 0px 0px #000000\"><div class=\"infobox-image-wrapper\" style=\"height:150px;width:150px;background-size:cover;order:0;margin-top:10px;margin-right:0;margin-bottom:0;margin-left:0;display:none\" data-image-url=\"\"><\/div><span style=\"display:block;order:0;color:#fff;background:#3074ff;font-size:36px;padding-top:20px;padding-right:30px;padding-bottom:20px;padding-left:30px;line-height:1;border-radius:5px\"><span class=\"fas fa-exclamation-triangle infobox-icon\" data-icon=\"fas fa-exclamation-triangle\"><\/span><\/span><div class=\"infobox-number\" style=\"display:none;order:0;font-size:36px\"><\/div><h3 style=\"font-weight:normal;text-transform:none;color:#4a5059;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px\">Ubuntu 20.04 LTS users need to edit code<\/h3><p style=\"font-size:16px;font-weight:normal;text-transform:none\">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.<\/p><div class=\"infobox-button\" style=\"display:none;align-self:center;margin-top:10px\"><a class=\"eb-infobox-link\" style=\"text-decoration:none;background:#3074ff;color:#ffffff;padding-top:12px;padding-right:28px;padding-bottom:12px;padding-left:28px\">Click Me<\/a><\/div><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Editing needed for default LaTeX in Ubuntu 20.04 LTS<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>! Argument of \\pgfmathfloatparse@@ has an extra }. &lt;inserted text&gt; \\par l.30 \\end{axis}\n! Paragraph ended before \\pgfmathfloatparse@@ was complete. &lt;to be read again&gt; \\par l.30 \\end{axis}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">A posting on Stack Overflow has the solution. The <code>circle<\/code> commands in lines 24 and 26 follow TeX Live 2020 syntax conventions, while Ubuntu&#8217;s <code>apt<\/code> 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:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\\draw [fill=ududff] (-3,3) circle (2.5pt);<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">to&#8230;<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>\\draw [fill=ududff] (-3,3) circle [radius=2.5pt];<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;s regex based Replace All feature:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>sed -i -E  's\/circle \\((\\d+\\.?\\d*[a-z]+)\\)\/circle [radius=$1]\/g' geogebra.tex\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Integrating plots into other TeX documents<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8220;master document&#8221;. In order to achieve this, you have to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>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<\/li><li>Copy any <code>\\definecolor<\/code> commands like the one in line 8 from your geogebra.tex file into the section following the <code>\\begin{document}<\/code> tag in your master document.<\/li><li>Copy the complete section starting from <code>\\begin{tikzpicture}<\/code> to <code>\\end{tikzpicture}<\/code> 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.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You should now be able to tex your master document with a neat sketch exported from Geogebra.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<span class=\"more-button\"><a href=\"https:\/\/hobbykeller.spdns.de\/?p=1286\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\">Import Geogebra plots to LaTeX<\/span><\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64,1],"tags":[284,285,198,282,283],"class_list":["post-1286","post","type-post","status-publish","format-standard","hentry","category-linux","category-uncategorized","tag-export","tag-graphics","tag-latex","tag-plotting","tag-tikz"],"_links":{"self":[{"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/posts\/1286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1286"}],"version-history":[{"count":25,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/posts\/1286\/revisions"}],"predecessor-version":[{"id":1363,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=\/wp\/v2\/posts\/1286\/revisions\/1363"}],"wp:attachment":[{"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hobbykeller.spdns.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}