Graphs in LaTeX using GNU Octave
The other day, I finished two assignments in my crappy ME 410 statics class that involved me making graphs and writing out equations, and it was required that it be done on a computer. For the first one I used OpenOffice and it was really ghetto; I was determined to do a “proper one” for the second assignment, and so I went out to learn LaTeX and how to generate graphs using GNU Octave, which is written to be (mostly) syntax-compatible with Matlab. I also wrote a really hacky script that helps in compiling LaTeX into the PDF file so you don’t have to run the same commands all the time.
What is LaTeX?
For those of you who haven’t heard of LaTeX, it’s a program that creates formatted PDF documents using a text file (like a web browser draws a page using HTML). Why go back to pre-Word days? Well, LaTeX is really good at doing text layout and generally making your document look good and consistent; it’s been in existence for almost 23 years. For example, you don’t have to worry about where to put your diagrams to make them fit or how to make a document look nice, it does that all for you. A majority of it was also written by Donald Knuth, one of the best computer scientists ever. The fact is, if you have to write mathematical equations in a document, LaTeX is the best and easiest way to do it. Period.
Generating graphs using Octave
For generating graphs, I used a feature of Octave that connects to GnuPlot, a plot rendering program. Here’s my Octave file (which is a good example file because it’s pretty simple):
gset output "the_file.eps"
gset term postscript eps
theta = linspace(0, 60, 200);
function r = Na(theta, L)
W = 800*1000;
w = 20;
w_one = 200*1000;
po180 = 3.14159 / 180;
r = (w_one * 1.5 + (w_one*L).*(L.*cos(theta * po180)) - W*(L-3.3)) / 2.3;
endfunction
xlabel("T")
ylabel("Na")
plot(theta, Na(theta, 2), "-;aaaaaaaaa;", theta, Na(theta, 4), "-;bbbbbbbbb;", theta, Na(theta, 5),
"-;cccccccccc;")
Run this file with the command octave the_file.m and you’ll get a file called ‘the_file.eps’
Most of the file is pretty self-explanatory, there’s some things to note here; the first lines tell Octave to write to a file labeled ‘the_file.eps’ (if you want color graphs, add the word ‘color’ after ‘postscript eps’). Also, label your graphs using simple one-word labels, don’t worry about how they look. Later, we’re going to use these labels and replace them with prettier ones. Also, the reason that we use ‘aaaaaaaaa’ as a label is a hack to get the spacing right; not very elegant.
One more thing for people who are stuck with errors in the equation, you have to use ‘.*’ when you multiply a scalar by a vector (in this example, theta is the vector). Basically, just try to rearrange your equation so the non-vector terms are all together and replace the ‘*’s next to the vectors with ‘.*’s
Integrating the graphs with LaTeX
Next, we write a LaTeX file with our document. There are many tools to help you do this such as Kile and I won’t get into a general LaTeX tutorial here because I’m lazy. Here however, is how to get your graphs into your document
\documentclass{article}
\usepackage{graphicx,psfrag,floatflt,hyperref}
****** Some text in your document ******
\begin{figure}[h]
\psfrag{Na}{$N_{a}$}
\psfrag{T}{$0 \leq \theta < 60^{\circ}$}
\psfrag{aaaaaaaaa}{\tiny{$N_{a}, L=2$}}
\psfrag{bbbbbbbbb}{\tiny{$N_{a}, L=4$}}
\psfrag{cccccccccc}{\tiny{$N_{a}, L=5$}}
\includegraphics[scale=0.93]{the_file.eps}
\caption { Plot of normal force at point A showing various lengths }
\end{figure}
We use a package called PSfrag that lets us replace labels in the graph with labels generated by LaTeX. This is important because it allows you to put in mathematical symbols into the graph, and it looks nicer too.
How do I get the final output?
To get the final output, you run a series of commands on the .tex file (latex the_file.tex; dvips -o the_file.ps the_file.tex; pstopdf the_file.ps), but this quickly becomes tiring after a few rounds, not to mention you’ll forget to regenerate your graphs when you change them and get confused. To solve this, I put together a very hacky Ruby script that basically just builds everything in the current directory. It also does the correct thing for BibTeX if you’re using it (and if you have to cite something more than once, you should be because it makes your life much easier).
Download the LaTeX build script here!
I wrote it in Linux but no reason it shouldn’t work everywhere. Run it as such: latex_build.rb /path/to/directory or leave the directory off for the current directory. If all goes well, you should end up with a PDF; if not, check the output of the script, it halts on errors if it runs into any (except BibTeX errors, long story).







Entries RSS
gebi said,
April 23, 2007 @ 12:47 pm
hello
some time ago I wrote a bash script called texfig:
http://texfig.sourceforge.net/
for generating graphs in xfig which uses latex for typesetting.
This is very handy. Just import your graph in xfig and label
it there. The problem with psfrag is :
*) you cannot use it with pdflatex.
*) having a picture with latex included is more flexible as you can import it
to any “word processor”
Brooke said,
June 4, 2007 @ 10:18 am
awsome article
Edita said,
May 7, 2008 @ 3:44 pm
Hallo!
I installed octave 2.9 I heard about it for the first time, so I just followed the instructions… well now that I read the comments, I’m a bit afraid that I will need pdflatex, but maybe not… so lets try to fix this: why do I get
I also like the article, its idea, everything… but unfortunately, octave the_file.m does not work for me
parse error near line 2 of file /home/edita/Desktop/the_file.m
syntax error
>>> gset output “the_file.eps”
?
thanks for the answer!
Edita
Ed said,
May 17, 2008 @ 12:09 am
For “windoz”:
comment lines 1 & 2 in octave file (.m)
and add:
print -deps thefile.eps
as the last line: