Aug 222010

I’m really surprised that most beamer slides I’ve ever seen have Figure/Table captions like this:

Figure: blabla blabla

or

Table: blabla blabla

which should have been Figure 1 or Table 2.

Why are the caption numbers missing? This is because beamer does not produce these numbers by default. To enable numbered captions, you have to put this in the preamble of your LaTeX document:

\setbeamertemplate{caption}[numbered]

I’m a picky LaTeX users… I cannot stand captions without numbers.

Mar 242010
Motivated by the excellent R package pgfSweave, I begin to notice the font families in my graphs when writing Sweave documents. The default font family for PDF graphs is Helvetica, which is, in most cases (I think), inconsistent with the LaTeX font styles. Some common font families are listed in ?postscript, and we can take a look at them by:

for (f in c("AvantGarde", "Bookman", "Courier", "Helvetica",
    "Helvetica-Narrow", "NewCenturySchoolbook", "Palatino", "Times")) {
    pdf.options(family = f)
    pdf(paste(f, ".pdf", sep = ""))
    set.seed(123)
    plot(rnorm(25), pch = 1:25, xlab = "xlab family", ylab = "ylab font",
        main = paste("Font Families in R (PDF):", f))
    text(13, 0, "Text in the Middle")
    mtext(sprintf("pdf.options(family = \"%s\")", f), side = 4)
    dev.off()
}

Here is a merged PDF containing the above single PDF files:

R-PDF-font-families.pdf (29K)

It seems that "Bookman", "NewCenturySchoolbook", "Palatino" and "Times" can be better choices when using Sweave because they are serif fonts, which are usually more consistent with LaTeX PDF.

Feb 182010

For a long time I’ve been wondering why we are not able to use Enter in the LyX Scrap environment which was set up by Gregor Gorjanc for Sweave. Two weeks ago, I (finally!) could not help asking Gregor about this issue, as I’m using “LyX + Sweave” more and more in my daily work. He explained it here: LyX-Sweave: mandatory use of control+enter in code chunks

After digging into the LyX customization manual for a while, I found a solution which allows us to press the Enter key just as we normally do when typing in a LyX document. The key is to use Environment instead of paragraph as LatexType for the style definition of Scrap. Besides, I used the LatexName as wrapsweave, as a LatexName is required by LyX. The definition for wrapsweave is simple: just two empty lines by \par. (If you define it as \newenvironment{wrapsweave}{}{}, you will run into troubles sometimes; especially when you use indent for paragraphs.)

As we know, LaTeX environment cannot be centered in LyX (only paragraphs can), so I defined a special environment ScrapCenter when I want to insert graphics via Sweave and make them center-aligned.

Nov 112009

Since animation 1.0-9, we will be able to create a PDF document with an animation embedded in it; the function is saveLatex(), and its usage is similar to saveMovie() and saveSWF(): you pass an R expression for creating animations to this function, and this expression will be evaluated in the function; the image frames get recorded by a graphics device. In the end, a LaTeX document is written in a directory, and we can get a PDF document by running pdflatex on the document.

In fact, the key point is the LaTeX package named animate, which can be used to insert image frames into a PDF document to generate an animation. The interface of animations created by this package is quite similar to the HTML animation page by the R package animation, moreover, it also uses JavaScript (in PDF) to animate the image frames.

Oct 122008

I‘d like to thank Prof Michael Friendly for telling me this: just create a hyperlink to the file with the run protocol and then you can open a file directly from the hyperlink. For example, in LaTeX with hyperref package, you may use \href{run:path/to/some.file}{some link} to open this some.file in your PDF. This is a useful hack that I have been looking for over a long time.

Jun 042008

I have spent three days on these two plugins. The first one “animation” is written by myself after a quick view of the instructions on writing plugins for Dokuwiki, as there is no such kind of plugins so far. What I want to say is “Regular expression is really a devil”! It has almost taken me a whole day to understand how to use a correct pattern to match the parameters in the tags…

Then I modified the “math” plugin so that it is much easier and more familiar for us to type LaTeX formulae in the wiki pages: just use $...$ or $$...$$. People who are familiar with LaTeX surely know what they mean.

By the way, Philippe Grosjean has kindly provided me the necessary files for highlighting R code in the Dokuwiki system. That’s really fantastic! See this page for some examples.

So the main infrastructure has been built till now; the next step is to modify my animation package and include more functions which have been in my mind for a long time. Then I will begin to migrate the old web pages in http://R.yihui.name to AniWiki.

Dec 312007

I suddenly realized just now that the line breaks for LaTeX code in Rd files are also \cr instead of \\ (which I thought to be in the past). R CMD CHECK will replace your \cr with \\ when producing LaTeX documentation files. If you write \\ directly in Rd files, it will be recognized as “an escape character + a backslash“, which surely is not a line break symbol.

I found such a problem in the documentation file for the function brownian.motion() in my package “animation“. The original code is:

\deqn{x_{k + 1} = x_{k} + rnorm(1)\\
  y_{k + 1} = y_{k} + rnorm(1)}{x[k + 1] = x[k] + rnorm(1)\cr y[k + 1] = y[k] + rnorm(1)}

And actually it should be:

\deqn{x_{k + 1} = x_{k} + rnorm(1)\cr
  y_{k + 1} = y_{k} + rnorm(1)}{x[k + 1] = x[k] + rnorm(1)\cr y[k + 1] = y[k] + rnorm(1)}

I was just to write an email for help but I understood the reason for my problem (by checking the result from R CMD CHECK) before I sent my email.

Oct 192007
Boadilla” is a quite plain theme in beamer class; I like plain styles, but I also want a headline in the top indicating the sections so that I can know where I am when I’m giving a speech. Thus I gave some slight changes to this plain theme: the default headline has been replaced with “infolines“, while the definition of the outer themeinfolines” has also been modified as I don’t have any subsections.

The original headline:

\ifbeamer@secheader\else\setbeamertemplate{headline}[default]\fi

The modified version:

\ifbeamer@secheader\else\setbeamertemplate{headline}[infolines]\fi

Downdload the file here

You may visit http://yihui.name/en/2007/10/jokes-in-statistics-a-talk-to-be-given-in-cueb/ to see the effects of my modifications.

Oct 032007

When I was adjusting my slides for a talk (to be given in Oct 16) in the Capital University of Economics and Business, I found the table of contents was a little bit too long to be placed in a single page while the titles of all sections are not so wide, thus I searched for some instructions on multi-column pages in LaTeX, and easily got this Wikipedia page: http://en.wikibooks.org/wiki/LaTeX/Page_Layout. Some codes as follows will just help us separate a part of texts into several columns:

...
\usepackage{multicol}
...
\begin{multicols}{3}    % 3 columns
   If you are using a standard Latex document class,
   then you can simply pass the optional argument twocolumn
   to the document class: \documentclass[twocolumn]{article}
   which will give the desired effect.
\end{multicols}
...

This is what my table of contents looks like now:

WWW.YIHUI.NAME XIE@YIHUI.NAME © 2007 - 2010 by Yihui Xie