gnuplot has been used in combination with LaTex for many years . Since the days of GnuTex, gnuplot has supported a graphical terminal called latex, which produces LaTex code. The code generated can directly be included in LaTex documents. With the help of a programming language such as Perl, it is possible to generate dynamic and very professional-looking reports . Using gnuplot in combination with LaTex has many advantages over other systems:
In this section, we look at a simple application that creates a database-driven LaTex document. Applications like this can easily be implemented and used for reporting purposes because they offer the advantages of stability and speed. Let's look at our prototype application. We use the following Makefile to generate a document named final.ps: x : config.plot psql -c 'SELECT * FROM income ORDER BY year' -t -A -F ' ' \ mygnuplot > file.data gnuplot -background white config.plot maketex.pl cat head.tex graph.tex end.tex > final.tex rm -f *aux *log *dvi *ps latex final.tex latex final.tex latex final.tex dvips final.dvi -o final.ps First we query the database and store the result of the query in file.data . Then we start gnuplot with the configuration of the graphics we want to generate. In the next step, we start maketex.pl , a short Perl script (we look at this in more depth later). maketex.pl creates a file called head.tex , which contains the header of the TEX file we will generate and a table containing the data retrieved from the database. Then we take all TEX files ( head.tex created by maketex.pl , graph.tex created by gnuplot, and the static file end.tex ) and make one big file called final.tex out of it. After removing some rubbish, we compile the TEX file to make a DVI file out of it. Note If you want to generate a table of contents, you have to compile the TEX file three times to receive the correct result. Finally, dvips converts the DVI file to a Postscript file. The following, config.plot , is the file containing the gnuplot code: set terminal latex set output "graph.tex" set nokey set grid set time set format y "$%g$" set title "Income table" set xlabel "year" set ylabel "income" set yrange[0 : ] plot 'file.data' using 1:2 with lines linewidth 2, \ 'file.data' using 1:3 with lines linewidth 2 The first line sets the terminal to latex . This makes sure that the output of gnuplot will contain LaTex code that we can use. The next line tells gnuplot to redirect the output to graph.tex . gnuplot uses LaTex's picture environment to display the graph. The result generated by gnuplot can sometimes be quite long and a little bit complicated, so we have decided not to include the LaTex code, which has been generated in this section. After defining some additional parameters, we define the format of the y-axis' label. If we don't, the label will collide with the graph. In the next step, we define the text the labels should contain and define the y range. With the help of the plot command, we display the result. After running gnuplot, the LaTex code for the graph is stored in graph.tex. The following is the code for maketex.pl: #!/usr/bin/perl # datafile ... $datafile="file.data"; # opening tex file open(TEX,"> head.tex") or die "cannot open head.tex\ n"; # generating header ... print "Trying to generate tex header ...\ n"; print TEX "\ \ documentclass{ article} \ n". "\ \ begin{ document} \ n"; # printing tex code to file print TEX "\ \ title{ Income statistics} \ n"; print TEX "\ \ maketitle\ n"; print TEX "\ \ begin{ abstract} \ n"; print TEX "The past few years show a constant increase of male and female ". "incomes. Especially in 2001 women's income has increased about ". "26 percent compared to the year 2000. .... bla ... bla ...\ n"; print TEX "\ \ end{ abstract} \ n"; print TEX "\ \ strut \ n\ \ newline\ n"; print TEX "\ \ begin{ center} \ n"; # generating a table ... print TEX "\ \ begin{ tabular} { rrrr} \ \ hline\ n"; print TEX "Year & Male & Female \ \ \ \ \ \ hline \ \ hline \ n"; # inserting every line into the table ... open(DATA, "< $datafile") or die "cannot open $datafile\ n"; while(<DATA>) { ($year, $male, $female)=split(/\ s/, $_); print TEX "$year & $male & $female \ \ \ \ \ \ hline \ n"; } print TEX "\ \ end{ tabular} \ n\ n"; print TEX "\ \ end{ center} \ n"; print TEX "\ \ pagebreak \ n"; close(DATA); close(TEX); # ending script ... print "tex-header generated successfully.\ n"; In the beginning, we define the file containing the input data (in our case, file.data ). Note Every backslash used in the LaTex code has to be defined by using \\ in Perl, because one backslash is used for masquerading the second backslash. It is better to pass the name of this file to the script as a parameter, but we have decided to do some hard-coding to make the code easier to understand. Then the file header.tex is opened. We will use this file to store the LaTex code that we will produce. If Perl is not able to create the file, we quit the script. Now we start generating the LaTex code and generating the required tex header ( \documentclass , and so forth). After defining the title, we start the abstract and add some text to it. This text will be displayed shortly after the title of the document. Then we define a table, which should be displayed in the middle of the page. We open $datafile for reading and processing, line by line. Every line is split into three components ( year , male , female ) and inserted into the table. This is done with just two lines of code: ($year, $male, $female)=split(/\ s/, $_); print TEX "$year & $male & $female \ \ \ \ \ \ hline \ n"; \hline makes sure that a horizontal line is drawn after every line. If all lines have been processed successfully, the table ends, and we tell LaTex to start a new page using \newpage . Now that head.tex is ready, we can look at the following file, end.tex: \ end{ document} The file contains just one line, which has to be added to the end of the LaTex document. Now that all TEX files have been generated by the Makefile , the Postscript file is generated. The document will be two pages long. Figures 20.8 and 20.9 show the most important parts of the two pages. Figure 20.8. The first page of the LaTex document.
Figure 20.9. The second page of the LaTex document.
On the first page you can see the title, the date, and the text of the abstract that we have added to our document in maketex.pl. After the text, we can see the table containing the data that we have extracted from our PostgreSQL database. The second page shows the graph generated by gnuplot. The line defining the women's income looks different than the men's line, because gnuplot makes sure that the two lines can easily be distinguished. This example demonstrates how simple, database-driven LaTex documents can be generated with the help of gnuplot and PostgreSQL. You can add applications such as this to a cron job (cron is a tool for starting certain tasks on Unix automatically and periodically) and generate professional-looking reports automatically. No matter how many pages your report contains, LaTex, gnuplot, Perl, and PostgreSQL are capable of handling it easily and reliably. |