Syntax highlighting of roxygen documentation in TextMate

August 2, 2011

With roxygen now on github, the release of roxygen2 and Hadley’s might now behind the project I expect roxygen to gain even more momentum.

R development in TextMate is great with the R bundle. Unfortunately the R bundle does not support highlighting of roxygen documentation by default. That was always a sticking point for me switching to roxygen because TextMate has really nice highlighting for Rd files.

So I hacked on the syntax file to get at least some preliminary highlighting of stuff after roxygen comments (#'). The file is in a gist over at github. It doesn’t yet work quite as well as I would like but it is better than nothing. In particular, highlighting of R code in examples does not work. Contributions/additions are welcome.

You must have must have the R bundle installed. The easiest way to install is to open the bundle editor (“Bundles” > “Bundle Editor” > “Show Bundle Editor”) and copy this file over the R syntax file from the R bundle. Then “Reload Bundles” or restart TextMate.

You may need to install the R bundle first if you have not already:

mkdir -p /Library/Application\ Support/TextMate/Bundles
cd /Library/Application\ Support/TextMate/Bundles
svn co http://svn.textmate.org/trunk/Bundles/R.tmbundle

Then either reload the bundles or restart TextMate to get the bundle activated.

2

Synctex with Sweave/pgfSweave in TeXShop/TeXWorks

June 28, 2011

Ever been editing an .Rnw (Sweave) file and tried to sync a pdf with the source in TeXShop (or TeXWorks) and had it open the .tex file? This is because the synctex information (in the .synctex.gz file) is messed up. Both TeXShop and TeXWorks support synctex, that means that if everything is groovy, we should be able to jump between the corresponding places in the source and pdf windows.

Unfortunately all that goes out the window when using Sweave. Fortunately we can make it work! By that I mean it is possible to sync beween a pdf and the .Rnw source file. We need three things:

  1. Add the concordance option when compiling with Sweave. That means add the line \SweaveOpts{concordance=T} to the top of your document after loading the Sweave style file.

  2. Enable synctex with the --synctex=1 flag when compiling the tex file.

  3. From the patchDVI (sweavesearch project on R-Forge), you need to use the patchSynctex function. This package is not available on CRAN afaik so you should install it from source from R-Forge:

    install.packages("patchDVI", repos="http://R-Forge.R-project.org")

Here is an example file for either Sweave or pgfSweave:

\documentclass{article}

\usepackage{tikz}

\usepackage{Sweave}
\begin{document}
\SweaveOpts{concordance=TRUE}

\title{Concordance Example}
\author{Cameron Bracken}
\maketitle

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas
eget dolor enim. Donec tincidunt augue porta justo lacinia
convallis. Quisque convallis mi sit amet augue volutpat gravida.
Class aptent taciti sociosqu ad litora torquent per conubia nostra,
per inceptos himenaeos. Proin consequat fringilla adipiscing.
Integer interdum risus id lorem luctus et convallis mauris rutrum.
Suspendisse at mauris velit. Cras sed nisl ipsum, et sodales nulla.
Integer in enim id ante aliquam tincidunt eget sed urna. Fusce et
eros sit amet lacus hendrerit rutrum. Proin a tellus sed magna
feugiat mollis. Fusce est augue, sodales quis scelerisque quis,
fringilla at leo.


<<data>>=
x <- rnorm(10)
@

<<>>=
print(x)
@

\begin{figure}[!ht]
\centering
<<plot,echo=T,fig=T,width=3.1,height=3.1>>=
plot(x,t=’l')
@

\caption{Example figure.}
\end{figure}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed
leo at mauris lacinia consequat quis sed neque. Nulla auctor
malesuada leo, at sodales nulla lacinia et. Donec vel enim vitae
massa aliquam posuere. In nec convallis sapien. Aenean tincidunt,
ipsum non congue ornare, dui eros lobortis libero, sed egestas nibh
arcu vel metus. Donec condimentum, ipsum ut vehicula ullamcorper,
urna lacus aliquam est, at condimentum nunc odio at felis. Aenean
dapibus aliquam dolor, at elementum dolor imperdiet vitae. Proin
felis odio, ultricies ut gravida scelerisque, aliquet ut magna.
Nullam at sapien vel mauris iaculis viverra vel ut mi. Class aptent
taciti sociosqu ad litora torquent per conubia nostra, per inceptos
himenaeos. Nunc vulputate ipsum placerat enim hendrerit at aliquam
mi porttitor. Fusce eget commodo metus. Vivamus sed nisl eu enim
adipiscing feugiat at et dolor. Vestibulum ante ipsum primis in
faucibus orci luctus et ultrices posuere cubilia Curae;

\end{document}

Here is a script to compile:

#!/bin/bash

R CMD Sweave "$1"
latexmk -pdf -silent -pdflatex=‘pdflatex –shell-escape –synctex=1′ "${1%.*}"
Rscript -e "library(‘patchDVI’);patchSynctex(‘${1%.*}.synctex.gz’)"

say you name it runSweave.sh, then call it

$ runSweave.sh myfile.Rnw

A much easier way would be to set up an engine in TeXShop or a custom “processing tool” in TeXWorks. There is a function in the patchDVI package called SweavePDF that should do the same thing but I have not tried it.

After all the compiling and patching of the synctex file is done, try right clicking and selecting ‘Sync’ in TeXShop or ‘Jump to PDF’ in TeXWorks, and everything should work! NOTE: I dont use TeXWorks so I haven’t gotten it to work but others have reported it does.

As for pgfSweave, the current version does not correctly output concordance information. But a patch enables proper concordance support (thanks aecay!). The next version of pgfSweave should have this working. To compile with pgfSweave, it is pretty much the same:

#!/bin/bash

R CMD pgfsweave –graphics-only "$1"
make -j 2 -f "${1%.*}".makefile
latexmk -pdf -silent -pdflatex=’pdflatex –shell-escape –synctex=1 –file-line-error’ "${1%.*}"
Rscript -e "library(‘patchDVI’);patchSynctex(‘${1%.*}.synctex.gz’)"

0

Using tikzDevice with Sweave in R 2.13

April 12, 2011

R 2.13 introduces an option to specify a custom graphics device in an Sweave code chunk. This is really cool and allows you to use tikzDevice output like pgfSweave does. In an pinch, say when you dont have access to any non-core packages, you can use tikzDevice output with the regular Sweave driver (RweaveLatex) more simply than before. Here is a minimal example showing how.

\documentclass{article}

\usepackage[noae,nogin]{Sweave}
\usepackage{tikz}



\begin{document}
\SweaveOpts{prefix.string=fig}

Example using \texttt{tikzDevice} with the default \textbf{Sweave}
driver (\texttt{RweaveLatex}).  First define a custom device:

<<results=hide>>=
tikz.Swd <- function(name, width, height, …){
    require(tikzDevice)
    tikzDevice::tikz(file = paste(name, "tikz", sep = "."),
                 width = width, height = height)

}
@

Then use the device for plotting, but you have to include the figure manually.

<<myplot, fig=T, grdevice=tikz.Swd, include=F, width=4,height=4>>=
plot(1)
@
\input{fig-myplot.tikz}


\end{document}

WARNING: This could be very slow if your graphic is complex

6

There is a new version of pgfSweave, and its awesome!

April 6, 2011

I just uploaded version 1.2.1 of pgfSweave, and I think it is just great! The most notable new feature is the use of the TikZ externalization library list and make feature instead of the pgf \begingraphicnamed functionality. This has some important consequences:

  1. PGF 2.10 is required! This has been out for some months now but many LaTeX distros are notoriously slow at updating, so you may need to update manually.
  2. Backward compatibility may break with some documents who still have the \pgfrealjobname hardcoded or who use the tex.driver option.
  3. A new workflow is required to compile documents (it includes make), this is outlined in the vignette.
  4. You can use all the nifty features in the TikZ externalization library, see the TikZ/PGF manual for more details.
  5. Most importantly it is faster!

There are a boat load of other changes and new features, I think you like it, check them out:

————————–
Version 1.2.1 – 2011-04-03
————————–
* [NEW] If PGF 2.10 is not available, issue a startup message but still
        load the package (So the package can go on CRAN). PGF 2.10
        is still required to compile .tex file produced with pgfSweave
* [NEW] Remove null device code which could cause problems with grid
        graphics
* [NEW] Example using xelatex for external graphics
* [NEW] Chunk options `relwidth’ and `relheight’ for scaling figures
        relative to the `width’ and `height’ options (thanks to Fabrice
        Rossi for the patch)
* [CHANGED] Fix the internal caching of external graphics, it now works as
        expected and can save significant time

————————–
Version 1.2.0 – 2011-02-21
————————–
* [NEW] Use the tikz externalization library instead of the pgf backend.
        This allows for more flexibility and faster compilations but
        BEWARE, this requires at least pgf 2.10 and may break backward
        computability in some cases
* [NEW] GNU Make is now a system requirement for externalizing graphics,
        NOT for compiling the package
* [NEW] Option to specify number of processors when externalizing graphics
* [NEW] Externalized graphics now have another layer of crude caching
* [NEW] Highlighted output is now output in a separate environment to fix
        the centering issues
* [NEW] Example document using plain latex and the tikz external library
        to generate eps graphics,
        `inst/example/pgfSweave-latex-example.Rnw`
* [CHANGED] The command line interface is now much simpler internally
        and uses optparse instead of getopt for more flexibility (the
        optparse package is now a dependency)
* [CHANGED] The `tex.driver’ option has been removed, the same effect can
        be achieved using the tikz externalization library, see the new
        example for how
* [DEPRECIATED] The `pgf’ option is depreciated, please use the `tikz’
        option

4

pgfSweave 1.1.3 and Beyond

January 19, 2011
Tags:

pgfSweave 1.1.3 is now on CRAN. This release adds a few new features and fixes some bugs

  • The pesky Rplots.pdf file is not generated anymore
  • Brand new vignette by Hans Ekbrand on the use of pgfSweave with large data sets based on this site.
  • New example on using caching by Yihui Xie
  • Reusing code chunks now works as expected
  • Inline comments are now preserved when tidy=T (thanks to changes in the formatR)

Please see the NEWS file for the full list sicce version 1.1.0.

I am also now working on the next version of pgfSweave to be version 1.2. This version will make some considerable changes to improve efficiency, namely

  • Scrap the “old” externalization (\beginpgfgraphicnamed) in favor of the tikz external library (\tikzexternalize) which will allow for parallel compilation of external graphics
  • Improve recognition of changes to plotting chunks for efficiency
  • Fix centering of \include‘d images and highlighted output
  • Improve upon the package load time
  • Spif up the command line interface with some new options
1

pgfSweave 1.1.0 now on CRAN!

December 2, 2010
Tags: ,

The next release of pgfSweave is now on CRAN! It has been a while since I posted about pgfSweave and there have been some significant changes in the past couple of months.

The main new features are:

  • Automatic code highlighting via the highlight package. This can be turned off with the highlight option.
  • “Tidying” of source code output via the tidy option.
  • Access to tikzDevice sanitization through a code chunk option sanitize
  • Automatic addition of the \pgfrealjobname command if it does not exist similarly to the addition of the \usepackage{Sweave} line.
  • Setting tex.driver=latex will now (in addition to working) generate an eps file

And of course bug fixes:

  • Fixes for bunches of issues related to the changes in Sweave in R 2.12. I think these issues are now resolved (fingers crossed)
  • keep.source actually works now.

See the NEWS file for the complete list of changes and the vignette for information on now to use the new options.

7

No Sidebar, Fullscreen, Super Minimal Gmail (for ninjas)

November 17, 2010
Tags: ,

Real Gmail Ninjas dont want all that sidebar clicky junk on their screens. Because of keyboard shortcuts, you can do nearly everything in Gmail with the keyboard and the sidebar becomes irrelevant. Also I use Adium, so I don’t need the build in chat.

I created a css file that kills the sidebar and some other things:

  • Inbox spans full screen
  • Extra attachment icons
  • No ads
  • Messages span whole screen
  • No footer

Please hack away. This script has pieces cobbled from various user scripts and extensions, I am really sorry but I don’t remember all my sources, please call me out on that.

Get it here.

UPDATE: Gmail changed some things, so I updated the css file fo fix some weirdness. It also now works with viewing contacts!

4

Save time when compiling with microtype

August 25, 2010

Recently there was a discussion on the MacOSX-TeX about compilation time with the microtype package. Under some circumstances (particularly when using latexsym), very large increases in compilation time were seen with microtype enabled. The full solution was given by Robert Schlicht, the maintainer of microtype. In the end it comes to adding these few lines after loading microtype:

\usepackage{microtype}
\makeatletter
\def\MT@register@subst@font{
   \MT@exp@one@n\MT@in@clist\font@name\MT@font@list
   \ifMT@inlist@\else\xdef\MT@font@list{\MT@font@list\font@name,}\fi}
\makeatother

0

New stack exchange site for stats

July 26, 2010

http://stats.stackexchange.com/

0

Automated way to check for PGF version

April 21, 2010
Tags: , ,

This is one way to check for the version of PGF that is installed in an automated way. First create a tex file with the following contents:

\documentclass{article}
\usepackage{tikz}
\batchmode
\makeatletter
\typeout{PGFVersion=\pgfversion}
\@@end

Say you named it test-pgf-version.tex. Then:

pdflatex test-pgf-version.tex
cat test-pgf-verson.log | grep PGFVersion | sed ‘s/PGFVersion=//’

should display the version number. I happen to need to do this from R so using similar logic:

logFile <- ‘test-pgf-version.log‘    
logContents <- readLines( logFile )
match <- logContents[ grep(‘PGFVersion=‘, logContents) ]
version <- gsub([=A-Za-z-]‘,”,match)
print(version)

which accomplishes the same task as the second line above.

2