# TikZ vs R for Math Workbooks

Should I be using R for vector graphics instead of LaTeX and TikZ?

I've been teaching myself LaTeX and TikZ for a while in order to make math worksheet PDFs for students.

Programmable vector graphics are absolutely crucial to the whole crux of my pedagogy.

Aside from the past 18 months of TikZ and LaTeX, I have zero-ish experience in coding... but I can't say that I love either TikZ or LaTeX. Overall, I'm somewhat discouraged at, among other things, how hard it is to grok:

• changing fonts
• making my tables look right
• remembering all the syntax and making sense of it
• manuals and documentation that everyone recommends
• simple calculations

Earlier today, though, I was told that doing calculations and graphics in R and then importing them into LaTeX for typesetting might be a whole ton easier.

Does anyone know if that's true? Or does it depend greatly on the kind of graphics I'm trying to make?

• Changing fonts in R might be harder than in LaTeX. R often produces figures with way too much white space around them, needs cropping. For some stuff it is handy to have R make the data and then for example use pgfplots to plot the data. There is a Tikz output mode for R, but the code produced in not always optimal. This is just my experience with working with R users (I know next to nothing about R myself) – daleif Sep 20 '16 at 7:45
• Not sure if the Tikz output mode for R is always optimal or not, but for me seem enough. See my answer. The white space around R graphs can be controlled easily with R code, this should not be a problem for an average R user. – Fran Sep 20 '16 at 13:05
• It's not just about optimizing. I also want to know how difficult it is to create those graphics in R. I find TikZ to be a pain. Arranging TikZ pictures in a LaTeX table is double pain. Then, using PGF random number stuff is triple pain. I'm wondering if R is easier. – WeCanLearnAnything Sep 21 '16 at 6:11

R vs TikZ? Why not R with TikZ? (and knitr)

A non-sense MWE:

\documentclass{article}
\usepackage{bookman}
\usepackage{miama}
\usepackage[T1]{fontenc}
\begin{document}
<<echo=F,  dev='tikz', fig.cap="Relationship \\LaTeX{} {\\fmmfamily\\huge quality}\\ and mass variation">>=
# install.packages("tikzDevice",dep=T)
plot(rnorm(500,sd=10)~rnorm(500,sd=5),
ylab="\\LaTeX{} {\\fmmfamily\\huge quality}\\ in R",
xlab="Mass variation \$$m = \\left( \\frac{m_o}{\\sqrt{1-\\frac{v^2}{c^2}}}\\right) \$$")
@
Default Bookman and {\fmmfamily\huge Miama} fonts outside R
\end{document}

• How hard is it to bring R pictures into LaTeX? – WeCanLearnAnything Sep 21 '16 at 6:23
• @WeCanLearnAnything Test yourself. Save my example file with the .Rnw extension. Open it with RStudio and just push the "Compile PDF" icon. If this fails, be sure that in the "Global options" the "Weave Rnw files using" option is set to "knitr" (and that R knitr package is installed, of course ). – Fran Sep 21 '16 at 8:27
• @WeCanLearnAnything. If you are an average R user, you only need to know the knitr syntaxis for the "optional options" of the R chunks as echo=FALSE to hide the R code, etc. Sweave have a similar approach but it is much less flexible (for instance, you cannot use dev='tikz'). – Fran Sep 21 '16 at 9:00