Using the Tufte-Latex class in Sweave
Asked Answered
S

4

18

I must confess that today is my first day using either Sweave or LaTeX. Yes, it's true. I realized today that my "before 40" bucket list was not going to resolve itself in the absence of my involvement. And like so many men of my generation, learning Sweave and LaTeX is on my bucket list.

So with that long preamble, I have a very nice first Sweave document:

\documentclass{article}
\begin{document}
\title{Look Mom! I'm doing \LaTeX}
\author{JD Long}
\maketitle
\section{Where I get both funky and fresh}
<<make some randomness>>=
  set.seed(12)
  t <- rnorm(100)
@
and an example plot
\begin{center}
<<fig=TRUE,echo=FALSE>>=
  plot(density(t))
@
\end{center}
This is a very simple example of how we might get started with Sweave. You know what comes next, right? That's right... Lorem Ipsum, ladies! 

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin sed sem est. Pellentesque massa magna, ullamcorper eget lacinia sit amet, dignissim ac leo. Fusce hendrerit elit vitae lacus mollis suscipit. 
\end{document}

Which produces this wonderful output:

enter image description here

So that much is great. But what I really would like to do is start bringing in wonderful LaTeX classes like the Tufte-LaTeX class which, from what I understand, basically turns my documents into brilliant, inspired works of art.

So how would I bring the Tufte-LaTeX business into my Sweave document and use it to make my documents more magical?

Swansdown answered 9/11, 2011 at 19:18 Comment(1)
Your Latin has several typos. You need to install a better spelling dictionary, this simply won't do. And an age 40 bucket list - did you tell your life insurance underwriter? (And I'm being generous by describing your Latin as having typos. Some of those had better be changed to 3rd person, or you are ... I can't bear to say it.)Adamok
K
12

I kind of like this as my standard header wrapped around your document:

\documentclass{tufte-handout}
\usepackage{amsmath}  % extended mathematics
\usepackage{booktabs} % book-quality tables
\usepackage{units}    % non-stacked fractions and better unit spacing
\usepackage{multicol} % multiple column layout facilities
\usepackage{lipsum}   % filler text
\usepackage{fancyvrb} % extended verbatim environments
  \fvset{fontsize=\normalsize}% default font size for fancy-verbatim environments
\usepackage{xspace}

I would note you should not put any markup in things like the title or author, as tufte-handout really doesn't do too well with it:

enter image description here

The Tufte classes are easily installed via TeXLive as the "tufte-latex" package, which includes the necessary material and some examples, if I recall correctly.

Kilburn answered 9/11, 2011 at 19:46 Comment(4)
Do I have to do anything to make the Tufte bits available to call as a doc class? Does it have to be installed or some such?Swansdown
@JDLong Just put the Latex style files in your local texmf directory (if you downloaded them from the web), or use the TeXLive package manager, as described on tex.SE.Parasol
Yeah, tufte can be installed via TeXLive and if you are not using TeXLive yet, you should. It provides a really nice and simple way to manage what packages are installed and provides a simple and (relatively) transparent mechanism to update those packages, too.Kilburn
@JamesHoward if you could add a little blurb about installing tufte via TeXLive to the text of your answer?Swansdown
B
10

I guess this is more like a LaTeX question than an Sweave question. I used the tufte-handout class a long while ago with the pgfSweave package, and you can find an example here (it is one of my homeworks for a data mining course).

Since you guys have written down how to do it with Sweave, I do not have anything to add here, except that I really dislike the default style of Sweave. I mean LaTeX the environments defined using fancyvrb. The default font style in R graphics is also something I do not like. They can just ruin the beauty of the Tufte class. The pgfSweave package using tikzDevice is a lot better in terms of styles. Some of frustrations with Sweave (e.g. style) made me decide to rewrite a new engine knitr; it is available here.

Bandolier answered 9/11, 2011 at 20:12 Comment(1)
Now you can check out the graphics manual of knitr at yihui.github.com/knitr/demo/graphics which uses the tufte-handout class. Both source code and output are there. You will lose a lot if you use Sweave :)Bandolier
C
5

I've used this template for making notes:

\documentclass{tufte-book}
\usepackage{graphicx}
\usepackage{lipsum}
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio}
% Prints a trailing space in a smart way.
\usepackage{xspace}


\usepackage{hyperref}
\usepackage{amsmath}

\newcommand{\tthdump}[1]{#1}
\usepackage{makeidx}
\makeindex

\title{My title}

\begin{document}
\setkeys{Gin}{width=1.1\marginparwidth} %% Sweave

 \section{Where I get both funky and fresh}
<<make some randomness>>=
  set.seed(12)
  t <- rnorm(100)
@
and an example plot
\begin{center}
<<fig=TRUE,echo=FALSE>>=
  plot(density(t))
@
\end{center}

%% a margin figure
<<a, fig=FALSE, echo=FALSE>>=
plot(density(t))
@ 
\begin{marginfigure}
<<fig=TRUE, echo=FALSE>>=
<<a>>
@   
\end{marginfigure}

This is a very simple example of how we might get started with Sweave. You know what comes next, right? That's right... Lorem Ipsum, ladies! 
\lipsum

\end{document}
Curkell answered 9/11, 2011 at 19:34 Comment(0)
R
4

As with many thing involving programming of some sort, I suggest separation of issues as a first strategy.

In other words, noodle with the Tufte class (that I haven't used in 22+ years of LaTeX ;-) til you are happy, then move what you want to use back to the Sweaving. Sweave does a number of funny things behind the back that can be confusing. [ And John just did that first part for you. ]

At lastly: if everything else fails do what I do and ask for (La)TeX help on tex.stackexchange.com ;-)

Rosenkranz answered 9/11, 2011 at 19:34 Comment(1)
I'd prefer to run into a magic talking unicorn who can impart the wisdom through me riding on its back to Narnia. Is that too much to ask?Swansdown

© 2022 - 2024 — McMap. All rights reserved.