4

I started using the awesome knitr package, and whilst I love the idea of using tikz graphics, it's not always very practical (time consuming). Thus, I end up switching back and forth between the good old pdf device, and tikz. Mathematical expressions are generally not handled in the same way in both devices; I find it much preferable to use LaTeX syntax in tikz, but it becomes hard to read in pdf, and vice-versa.

Have there been attempts at providing a framework to seamlessly switch from one notation to another? Something like a translate(x, from="plotmath", to="tex", ams=TRUE, ...) function, that could be hooked internally with knitr to the device in use. How would one go about doing it?

baptiste
  • 75,767
  • 19
  • 198
  • 294

1 Answers1

4

This sounds hard to me. Translation from plotmath to LaTeX may be easier, e.g. replace alpha with \alpha, and x[i] with x_{i}, etc, but the other way can be difficult since plotmath is only a "subset" of LaTeX expressions.

If the time-consuming generation of tikz graphics is an issue for you, you may consider turning on the cache (as I did in the knitr manual and many other example documents). Once a tikz figure is generated, it will not be regenerated the next time if cache=TRUE, so it will be pretty fast, and it will be even faster if you also convert tikz to pdf by the chunk option external=TRUE.

The only reason that I may not use tikz is when the plot is too large in size, as I explained in the manual. Other than that, tikz is always preferable to me. This is slightly off-topic, though.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • I've used `cache`, and it does help to some extent, but the combination `ggplot2` + `tikz` remains quite slow, for the first run at least. By time-consuming, I also mean the fact that `tikz` is necessarily a non-interactive device. In contrast, I can easily switch between, say, `quartz()` and `pdf()` using the same labels and annotations. – baptiste Jan 16 '12 at 19:27
  • plotmath to latex is probably the most useful conversion, but even that is not so trivial when dealing with obscure `bquote`, `substitute`, etc. constructs. – baptiste Jan 16 '12 at 19:29
  • Yes, only the first run is slow. – Yihui Xie Jan 16 '12 at 19:44