# Thesis Diagrams

So, I’m writing my thesis at the moment, and invested some good procrastinating efforts to make sure my diagrams are consistent with my LaTeX thesis template. What I’ve learnt could be useful, so here it is;

## Graphs

I do almost all of my data analysis using the fantastic (and free) R statistical package with RStudio.  So, the question is how to get print quality R figures into my thesis so that they are (a) pretty, (b) adaptable and (c) match the notations, style and size of my other LaTeX elements?

The few solutions I’ve tried to marry my nice R figures with LaTeX have been a real pain. Exporting as postscript or pdf is tough (although the R function cairo_pdf(family=”CMU Serif”) was quite a nice solution), and Sweave wasn’t to my taste. I found salvation in the awesome tikzDevice package from these nice people.

To get it working, I needed a nice new version of R (v.2.15.1) and the tikzDevice package found here (I got v.0.6.3).  Then, once loaded, you use tikzDevice just like any other graphics device in R: open the device, draw your diagram, close the device. Like so:


x<-seq(-pi,pi,,50)
y<-sin(x)

tikz("my-figure.tex", width=4, height=4)

plot(x,y, xlab="phase $\\phi_1$", ylab="amplitude $\\alpha$", main="Example Plot")

dev.off()



A few important points to note here:

• The width and height parameters for the graphic in the tikz() command are in inches by default. I find 4″x4″ is good for A4. Others use 3.5″
• I’ve used LaTex notation in the plot labels so remember to open and close in-line maths with $…$. you can use any latex notation you wish.
• In R, \ is an escape character, to actually get a \ you need to use \\. So any calls to LaTex macros or symbols should be \\macro as opposed to \macro.

This outputs a tex file named my-fugure.tex

All we need to now is use the tikz package in LaTeX and import the figure like any other:

\documentclass[11pt]{article}

\usepackage{tikz}

\begin{document}
\begin{figure}
\begin{center}
\input{my-figure}
\end{center}
\caption{An example figure using tkz. Notice this figure text is the same size and style as the graphic. The figure has inherited these from the document. }
\end{figure}
\end{document}

Producing an image like this, (I’m aware of the irony of harping on about image quality then posting this as a jpeg)

So far, it’s worked for any and all R graphics I’ve thrown at it, and I’m very happy with the results! They are vector based, print quality and adapt to any changes I make to the latex template. Editing them is as simple as opening the R file, changing the appropriate bits and re-running. The one problem I ran into was the use of scientific notation in the axis. A problem swiftly overcome with the following snippet of my own making:

plot(x,y,xaxt='n) #turn off the axis numbering in plot
aX <- axTicks(1)  #find out what numbers lie along axis
labelsX<-paste(aX/1e7,"$\\times 10^7$",sep="") #rescale and reformat the labels in latex style.
axis(1, at = aX, label = labelsX) #put them back


## Optics Diagrams

Drawing consistent optics diagrams or coordinate systems in latex? I’ve found using the pst-optexp package very, very useful. The most powerful part is the raytracing of your beam when you’re done, and the automatic alignment of optical elements because of the nodes. I’m not going to go into it in-depth here, head on over to ctan and have a play.  (ive also found it’s mummy package, pstricks, awesome for other diagrams). All I shall do is post an example, and quickly mention two pitfalls to avoid: The thing I like about this example diagram is the beam was expanded and collimated with the lens L1 and L2 automatically by the package. All I did was put the two lenses at each-other’s focal points. The labels are all automatically placed, too.

Pitfalls to avoid:

• If you use pdfLatex to compile, you need to run it in extended mode by adding the switch -shell-escape to your command call.
• You’ll also need to \usepackage{auto-pst-pdf} to make it work.

Phew. Hope that’s all helpful to someone!

Tom