gnuplot lua TikZ

test.png
A snapshot of the output from TikZ which originated from a gnuplot rendering of $y=x^3$

This guide describes how to install the gnuplot TikZ terminal in a Windows environment. This gnuplot terminal provides a bridge from gnuplot to TikZ, allowing for the preparation of complex mathematical figures or graphs, along with consistency of caption font typesetting and line widths.

test2.png
The same plot with a TikZ scale factor of 50%.


I was interested in pursuing this union of gnuplot and TikZ because gnuplot can provide the complicated, powerful graphical rendering, and TikZ can provide the consistent typesetting and parametrized styling.

Ironically, the thumbnails of the attached images demonstrate why such a process is important. Because the thumbnails auto-resize a raster image, things like font size and line thicknesses change. But view the images at 100% and you will see that this system prevents such a nuisance when attached to LaTeX documents or presentations.

For a better example, check out scaling demo - May 9, 2009 (pdf).

If you are wondering what that all means, check out http://www.texample.net/tikz/examples/gnuplot-tikz-terminal/ and http://peter.affenbande.org/gnuplot/ for starters.

Prerequisites

Installation

Moving and Modifying Files

lua.trm

  • browse to TIKZ_PATH and copy lua.trm to GNU_PATH\term\
    • open this file in the new location, and look around line 40 for
       #define GNUPLOT_LUA_DIR X11_DRIVER_DIR 
      
    • change it to something like
       #define GNUPLOT_LUA_DIR "c:\\Apps\\gnuplot_lua_terminal" 
      
      where the path is based on TIKZ_PATH. Note the use of double \\ so the path name is that of DOS-based directory structure, and it is not munged up by the use of the unix-based escape character.
    • comment out around line 436 where it says
       extern const char lua_ident[]; 
      
      make it so it reads
       /*  extern const char lua_ident[];*/ 
      
    • comment out around lines 448 and 449 where it says
       lua_pushstring(L, lua_ident); 
        lua_setfield(L, luaterm, "lua_ident"); 
      
      and make it so it reads
       /*  lua_pushstring(L, lua_ident); 
        lua_setfield(L, luaterm, "lua_ident");*/ 
      

terms.h

  • browse to GNU_PATH\src\ and open term.h
    • add the following line to the bottom of term.h:
      #include "lua.trm"
      

makefile.mgw

  • browse to GNU_PATH\config\ and copy makefile.mgw to GNU_PATH\src\. Open makefile.mgw in this new location
    • comment (#) lines 38 through 40 to turn off the GD library stuff. This requires the GD Graphics library package, and I couldn't get it to behave properly during compile.
    • comment (#) line 360 to turn off the help file stuff. This requires some windows app which I couldn't get to work properly during compile.
    • find the line
      TERMLIBS =
      
      It should be around line 153.
    • change this to something like
      CFLAGS += -I/c/Apps/Lua/5.1/include
      TERMLIBS = -llua51 -L/c/Apps/Lua/5.1/lib
      
      where the path depends on where you put Lua
    • Find the line
      DESTDIR = /c/Progra~1/Gnuplot4.2
      
      It should be around line 87
    • change this to whatever path you want, being mindful of the unix-style file path syntax
    • comment (#) the line that says
      cp wgnuplot.hlp $(DESTDIR)/wgnuplot.hlp
      
      This should be around line 449

gnuplot.lua

  • browse to TIKZ_PATH and open gnuplot.lua
  • comment (--) line 214 where it tries to use the busted-ass lua_ident property. Make it read something like
    "Lua version removed!",--string.match(term.lua_ident, "Lua [0-9\.]+"),
    

gnuplot-lua-tikz.sty

  • browse to TIKZ-PATH and copy gnuplot-lua-tikz.sty to wherever your working directory is for your latex project. Alternatively, you can generate this file using gnuplot, as described later.

Compiling and Installing

  • Open a MinGW shell by running msys.bat from MSYS_PATH\1.0 and navigate using this shell to GNU_PATH. Refer to some linux reference for syntax and directory structure overview.
    • Enter the following in the MinGW command prompt:
      make -C src -f makefile.mgw
      
      There should hopefully be no compile errors. If there are, undo the compile (described later) and try to resolve the issue
    • Enter the following in the MinGW command prompt:
      make -C src -f makefile.mgw install
      
      This will copy all relevant files over to the installation folder defined in makefile.mgw
    • Browse to the installation folder and make a shortcut to wgnuplot.exe if you like

Testing

  • Run wgnuplot.exe
  • switch the output directory to some convenient place using the ChDir button
  • Type the following command into gnuplot:
    set term lua createstyle
    
    This will generate the file gnuplot-lua-tikz.sty in the current directory. This is required to compile the LaTeX document. This needs only be done once, and this .sty file should be moved into whatever project uses these figures.
  • Type the following commands into gnuplot:
    set term lua fulldoc
    set output "test.tex"
    plot (x**3)
    set output
    
  • Open test.tex in your LaTeX editor to view, and compile using pdflatex. The file test.pdf should produce a quaint little graph showing $y=x^3$.

Backtracking and Troubleshooting

If you made a mistake and need to recompile, navigate to the GNUPLOT_PATH and enter the following in the MinGW command prompt:

make -C src -f makefile.mgw clean

If you have any more concerns, contact me and I can try to post solutions here.

Use

I find it helpful to make the following gnuplot file. I call it tikz-output.plt.

#this outputs a self-contained latex document.  remove fulldoc to only output the tikzpicture object
unset multiplot
set terminal lua fulldoc

Load this in gnuplot, then set the output filename by entering the following into the gnuplot console:

set output "filename.tex"

Once this is done, load the gnuplot project you are working on, or do whatever you plan to do. Finally, close the output file with

unset multiplot
set output

Note that you need to enter the unset multiplot line only if you were doing multiplot in the gnuplot project. It may be wise to just stick this at the end of your gnuplot project file to streamline things.

This will write out a TikZ style file, fully standalone, to the current gnuplot working directory. Take it from there in your favorite LaTeX editor to tweak up to your liking!

Revisions

This document is a quick-and-dirty job, and leaves lots of room for improvement, documentation, and development. This is based on my experiences trying to get the system working. Based on feedback, I will improve upon this.

Credits

I am deeply grateful to my brother, Travis, for guiding me through this perilous installation process. I would not have been able to get this working without his support and patience.

Of course, I'm also very grateful for all the people who make all this open source software. LEd, MikTeX, PGF/LaTeX, gnuplot, gnuplot TikZ terminal, msys, MinGW, and so on.


Links

An example showing outputted TikZ code and a figure