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}
R
Sweave
A useful script to run Sweave and Latex (as many times as needed):
It's by Gregor Gorjanc: Sweave.shBEWARE: 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
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)
}
@
\usepackage{figlatex}
\usepackage{epsfig}
\DeclareGraphicsExtensions{.fig,.pdf}
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!
<
system("for fig in *.eps; do mv `basename $fig eps`{eps,fig} ; done")
system("fig2pdftex `ls *.fig`")
system("rm -f _null.ps")
@
See figtest.Rnw for a brief example; figtest.pdf as typeset.
One complication is that long LaTeX commands go off the page when R creates it and get lost; to remedy this, define a shorter LaTeX command earlier in your document that typesets what you want and call it in the plot. See the example.
Also, here's a version of Sweave.sh that cleans up the pdftex and pdftex_t file created by the fig2ps script.
Emacs/ESS
I use Emacs/ESS and have set up the following customizations in my ~/.emacs file to use the Sweave.sh file mentioned above to compile the current Rnw file and also to use evince to view it. To use it, you'll need the Sweave.sh in your ~/bin directory, named as just Sweave. It sets up key-bindings for \C-s\C-c to run Sweave (I think Sweave/Compile) and \C-s\C-v to view the resulting pdf in evince (I think Sweave/View).
I find that this makes my Emacs/ESS Sweave workflow much more productive.
Also below are customizations to always use pdflatex on a latex file and to use evince to view the resulting pdf.
; use Sweave script as compile function for Rnw files and bind to \C-s\C-c (defun ess-swv-SweaveSh () "Use Sweave script to weave an Rnw file and create a pdf." (interactive) (let ((compilation-buffer-name-function (function (lambda(ign)(concat "*" (buffer-file-name) "*"))))) (compile (concat "Sweave -ld " (buffer-file-name))) ) ) (define-key noweb-minor-mode-map "\C-s\C-c" 'ess-swv-SweaveSh) ; view the PDF file created by Sweave script (defun ess-swv-SweaveView () "View the PDF file created by Sweave." (interactive) (let ((buf (buffer-name)) (namestem (file-name-sans-extension (buffer-file-name)))) (call-process-shell-command (concat "evince \"" namestem ".pdf\"") nil 0) )) (define-key noweb-minor-mode-map "\C-s\C-v" 'ess-swv-SweaveView) ;; Use PDF mode by default (setq-default TeX-PDF-mode t) ; change default TeX viewer to evince (defun pdfevince () (add-to-list 'TeX-output-view-style (quote ("^pdf$" "." "evince %o %(outpage)"))) ) (add-hook 'LaTeX-mode-hook 'pdfevince t)
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!