1

I am searching for a good tool for graphs and charts and found Graphics Layout Engine. Am I able to include a code written in GLE within a latex document? The aim is not including it as a file in the document but as a script within the latex script, is it possible?

Thank you

User505
  • 287
  • 3
    Of course you are if you're willing to write an interpreter in TeX that reads that format (I never heard of one as of now). Note however that there are packages like pgfplots and TikZ around, which also enable you to produce high quality plots and images. – Skillmon Apr 01 '18 at 17:37
  • 1
    that page says that it will generate pdf, which you can then include into latex – David Carlisle Apr 01 '18 at 17:45
  • 1
    It really depends on what tool you can use and how far you want to go. If you use MATLAB, you can create your figures and print them as eps or other vector or raster graphics formats. If you use python, you can do the same with the matplotlib library. If you want to go deep in the code to fully control the output, you may use pgfplots of TikZ as proposed by @Skillmon ... – BambOo Apr 01 '18 at 17:59
  • 2
    In addition to @BambOo: Python's matplotlib can export to pgf-code which can be included directly into your LaTeX document with the pgf (or TikZ) package. This way every font aspect should match the rest of your document (but it might really slow down your compilation process). – Skillmon Apr 01 '18 at 18:11
  • Judging by the comments and the suggested duplicate, some clarification might be in order. Do you want to place the code itself in the PDF, or do you want to write GLE code in the .tex file, and get a diagram in the PDF? (I understood you to mean the latter.) – Torbjørn T. Apr 02 '18 at 06:17
  • @TorbjørnT. You are right, yes I meant the latter. – User505 Apr 02 '18 at 06:19
  • 1
    Right, so the suggested duplicate doesn't fit. (I've voted to reopen.) I don't know whether such a thing exists, in the meantime you can of course use David's suggestion. – Torbjørn T. Apr 02 '18 at 06:24
  • Please clarify the question by editing it. – Johannes_B Apr 02 '18 at 07:32
  • It is not a stackexchange post, but other people have the same questions here. The thing is that you have so much possibilities that it is really depending on your actual experience. If you are already "fluent" in GLE coding, please use it. If not, using the already existing packages for latex would be a better idea. What experience do you currently have with such programs @User505 ? – BambOo Apr 02 '18 at 10:18
  • @BambOo Thank you for your comment, actually I am middle level in using latex but not aware that much on GLE or the package (pgfplots) that Latex offers since I'm trying to create a chart and I read the documentation but seriously it wasn't easy to understand for me, I found the this example in GLE and it is easy to edit that is why I want to see if there is such a way enables me to adapt the GLE code in the Latex code. – User505 Apr 02 '18 at 17:07
  • @User505, I personally started to use pgfplots and tikz a year ago, both are very flexible and you can get a lot of help between the community of users and the documentation, which I think should guide your choice, in any case, good luck ! – BambOo Apr 02 '18 at 19:59
  • Unrelated: the pgfplots manual you linked to is quite old (version 1.3, from 2010), you can find the manual for the current version (1.15, from 2017) at for example CTAN: https://ctan.org/pkg/pgfplots Not saying the new one is easier to understand, but it does also have a lot of examples. – Torbjørn T. Apr 03 '18 at 10:33

1 Answers1

1

You can insert the GLE code in your document, write it to a temporary file, call GLE on this file, and include the output as an image. Writing the contents of an environment to a file can be done with \VerbatimOut from the fancyvrb package, see Write environment body verbatim to a file. Calling an external program can be done with \immediate\write18 (see for example https://stackoverflow.com/questions/3252957/how-to-execute-shell-script-from-latex), which requires the --shell-escape flag when compiling the document.

A counter called glenum is used to prevent caching by creating different filenames in case you want to compile more than one figure.

MWE:

\documentclass{article}
\usepackage{graphicx}
\usepackage{fancyvrb}
\newcounter{glenum}
\setcounter{glenum}{0}
\newenvironment{GLEinclude}[1]
  {\xdef\mywd{#1}\VerbatimOut{\jobname.\theglenum.gle}}
  {\endVerbatimOut%
  \immediate\write18{gle -output \jobname.\theglenum.gle.pdf \jobname.\theglenum.gle}%
  \includegraphics[width=\mywd]{{\jobname.\theglenum.gle}.pdf}%
  \stepcounter{glenum}%
  }

\begin{document}
Here is a tree fractal compiled with GLE:

\begin{GLEinclude}{5cm}
size 32 22

a = 3; b = 4; c = sqrt(a^2+b^2)

sub pythagorean n
   local red   = (34+n*92)/(n+1)
   local green = (139+n*64)/(n+1)
   local blue  = (34+n*51)/(n+1)
   box c c fill rgb255(red,green,blue)
   if n = 0 then return
   begin translate c c
      begin rotate todeg(-acos(a/c))
         begin scale a/c a/c
            begin translate -c 0
               pythagorean n-1
            end translate
         end scale
      end rotate
   end translate
   begin translate 0 c
      begin rotate todeg(acos(b/c))
         begin scale b/c b/c
            pythagorean n-1
         end scale
      end rotate
   end translate
end sub

begin translate 16 0
   pythagorean 10
end translate
\end{GLEinclude}

Also known as a \textbf{Pythagorean tree}.
\end{document}

Result:

enter image description here

Marijn
  • 37,699