# Computing Hints and Resources

Hints for LaTeX, R, Sweave, Emacs/ESS, and Arc.

## LaTeX

Set Margins

A one-inch margin all around:

\usepackage[margin=1in]{geometry}

A single page, useful for when viewing on the computer:

\usepackage[margin=0.1in,papersize={6.7in,100in}]{geometry}

Double Spacing (and variants)

Replace the 2 with the desired spacing, such as 1.5 for one-and-a-half spacing.

\usepackage{setspace}
\setstretch{2}

Subfloats

To use subfloats in your paper:

\usepackage[format=hang, justification=raggedright]{subfig}

Then include your graphics like this. Spaces matter in this package, so put % at the end of every line.

\begin{figure}%
\subfloat[Caption for first figure.]{%
\includegraphics{fig1a}%
\label{labela}%
}%
\quad %this puts a little space between the figures
% \\ % use this instead to start a new row of figures
\subfloat[Caption for second figure.]{%
\includegraphics{fig1b}%
\label{labelb}%
}%
\caption{Caption for both figures.}%
\label{wholelabel}%
\end{figure}

## Sweave

A useful script to run Sweave and Latex (as many times as needed):

It's by Gregor Gorjanc: Sweave.sh

BEWARE: By default, this script cleans up after itself, deleting all the extra files LaTeX and Sweave creates. If your file is called myfile.Rnw, this will include files called myfile.R and myfile.tex. So a word to the wise: make sure the basename of your Rnw file is unique; then nothing unexpected will be deleted.

One-click Sweaving in TeXShop on the Mac

Put the above Sweave.sh script in your ~/Library/TeXShop/bin/ folder. Then add the following files to your ~/Library/TeXShop/engines/ folder: Sweave.engine and SweaveNoClean.engine. To Sweave with one click, restart TeXShop after adding these files, open the Sweave document (with Rnw extension) and in the dropdown menu above the document window, change it from LaTeX to Sweave or SweaveNoClean.

BEWARE: The "Sweave" option wll clean up after itself, deleting all the extra files LaTeX and Sweave creates. If your file is called myfile.Rnw, this will include files called myfile.R and myfile.tex. So a word to the wise: make sure the basename of your Rnw file is unique; then nothing unexpected will be written over and then deleted.

The SweaveNoClean option does not clean up after itself. This makes sure you don't delete anything unexpected; though it could still write over a file called myfile.tex if you Sweave a myfile.Rnw. This also doesn't delete any graphics that have been created, in case you want to have them separate from your full typeset document.

Added 9/15/09: you'll also have to set the permissions on Sweave.sh and the two engine files to allow execution.

Added 3/31/11: To have LaTeX find the Sweave.sty file automatically, put a link to it in your texmf tree. From the command line (commands from codesnippets.com:

mkdir -p ~/Library/texmf/tex/latex
cd ~/Library/texmf/tex/latex
ln -s /Library/Frameworks/R.framework/Resources/share/texmf Sweave

Here's a couple links to pages describing how other people have done this and may have some extra tips: Jarad Niemi, Stack Overflow

Using Sweave to make graphics for subfigures

Unfortunately, if you use Sweave, you can't put a % after the includegraphics. Instead put this in a .sty file and include it with usepackage This redefines the figure environment to ignore end of line characters. It also automatically centers the contents.

% ----- Make Figure Environment that Ignores end-of-lines
\begingroup%
\catcode\^^M=\active%
\gdef\Moff{\catcode\^^M=\active\def^^M{\ignorespaces}}%
\endgroup

\let\origfigure=\figure
\let\endorigfigure=\endfigure
\renewenvironment{figure}%
{%
\begingroup%
\Moff
\begin{origfigure}\centering%
}%
{%
\end{origfigure}%
\endgroup%
}%

Using LaTeX in your R plots automatically

This hack NO LONGER WORKS. Something changed in R 2.5.0. It's here to help me remember what I did when I try to fix it.

It's for pdftex, so it assumes you're not making eps files and outputs the necessary stuff in pdf format. (It wouldn't be hard to change to do the opposite, should you wish.)

All of this needs to go in your Sweave file. First tell R to use the xfig command when a ps file is made instead of postscript. This will change it for ALL plots you make, so if you're going to want a plain ps output later, you need something different.

<>=
postscript<-function(file="_null.ps", width=6,height=6, horizontal=FALSE,paper="special") {
xfig(file=file, width=width, height=height, horizontal=horizontal)
}
@

You need these packages to have LaTeX know what to do with the fig graphics. The epsfig one is outdated but certainly available on your system, but the default installation of transfig (that converts fig into pdf/latex) uses it. The figlatex tells latex what to do with the fig file format. A copy is here: figlatex.sty; it's part of the non-standard latex-make package.

\usepackage{figlatex}
\usepackage{epsfig}
\DeclareGraphicsExtensions{.fig,.pdf}

Then you make your plot in the usual way, using latex code in your labels, if you wish, for example:

plot(x,y,xlab="$\\int_a^b f(x)\\,dx$",ylab="$\\sigma^2$")

And at the end of your Sweave file, you need to convert all the figs into pdf/latex form using the fig2pdftex script, which is a hack of the fig2ps script that doesn't reassemble everything and by default converts all text to LaTeX. BE CAREFUL: because Sweave doesn't know about fig format, it outputs all the fig files as eps, so this script RENAMES ALL EPS FILES AS FIG FILES!

<>=
)

## Arc on Mac OS X (under X11)

I haven't tested this directly, as I got it working in a more circuitous manner. I think these directions will simplify matters quite a bit.

Arc will run on Mac OS X under the X11 software. You need to install X11 and xlispstat first. X11 is on your OS X disk; it can also be downloaded from Apple. xlispstat you can find at the UM site. I had trouble compiling it, there was an error in lowess.c; this version makes the error go away so that it compiles, but may introduce problems down the road. Also, the three commands you run to compile it are slightly different on the default Mac setup; they are "./configure", "make", and "sudo make install". The make install needs extra permission to put the files in the right places, sudo will ask for your password to get the necessary permissions.

Download this zipped file: ArcMac.zip with the necessary files; the shar file you can get from the UM site but there's a couple things in it that don't work on OSX, so this should save you the trouble. To unzip it just double click on it in the Finder, this will create a folder called Arc.

This Arc folder can go anywhere on your system and it will work; I put it in ~/Arc, that is, in your home folder. To do that, drop the directory onto the Home folder that usually shows on the left side of your Finder.

We now need to tell Arc where you've put it. There are two files to change, arcinit1.lsp and savews.lsp (both in the Arc directory). Open each using TextEdit (one at a time). To do that, click on it in the Finder, then go to File>Open With>Other, and select Text Edit. Where it says /Users/Aaron/, change Aaron to your username. If you put the Arc directory somewhere else, change it to that. If you put the Arc directory somewhere else, you'll also have to change arc.sh; it has two lines with "~/Arc", change those to the correct place.

Now we initialize Arc. Open X11 and go the Arc directory (just "cd Arc") if you put it in your home folder. Then type "xlispstat savews.lsp".

Finally, to put Arc in a directory where you can start it easily type "sudo cp ~/Arc/arc.sh /usr/local/bin/arc". You'll have to type in your password.

Now Arc should run from anywhere just by typing "arc" at the command line of the X11 window.

If it doesn't, you probably need to set your path to include /usr/local/bin. I forget how to do this but your local computer guru could probably tell you without thinking about it.

If you try this, let me know if it works or not. Thanks!