# tikzDevice is not getting sizes right (knitr)

I am using OSX, pdflatex, R (cran), tikz/tikzDevice and knitr to create my documents. My problem is that the font sizes of the produced tikZ-file do not match the LaTeX document font sizes.

## Result / Problem

Here's a screenshot of the result:

As you can see, the text is way to big and the points/circles are too small.

## Files

I have a main LaTeX file, called main.tex, which contains:

\documentclass[a4paper,12pt]{article}

\usepackage{tikz}

\begin{document}

%% begin.rcode myplot, dev='tikz',  external=FALSE, fig.width=5, fig.height=5
% plot(cars)
%% end.rcode

\hspace*{6.3cm}\color{red} speed

\end{document}


I then use a R-Script, called knit.R to convert the result of plot to tikZ-Code:

library(knitr)
render_latex()
knit("/Users/ralfix/Documents/main.tex")


It creates a new LaTeX file, called main-out.tex which contains the following:

...
\input{figure/myplot.tikz}
...
\hspace*{6.3cm}\color{red} speed
...


and a new tikZ-file, called myplot.tikz:

% Created by tikzDevice version 0.6.2-92-0ad2792 on 2013-03-30 18:14:48
% !TEX encoding = UTF-8 Unicode
\begin{tikzpicture}[x=1pt,y=1pt]
...
\node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  3.00] at (185.47,  6.24) {speed};
...
\end{tikzpicture}


I can remove the scale=3.0 in the tikz file to get the text right, but then some positioning is broken and the circles stay too small.

## First Idea

If I remove the [a4paper,12pt] from my documentclass declaration and re-run my script, I get this much better result:

But if you compare the tikz output with the red LaTeX-output, the text is still too big. The tikz-file says scale=1.2 for text now.

How and where can I modify something to get every sizing correct?

Just found the solution myself:

Someone can set the point size for tikzDevice manually — With knitr, this is done by adding

dev.args=list(pointsize=12)


to the chunk options.

Hope this will help someone ;)

This turns out to be a bug of tikzDevice package, which has been reported long time ago (but still not fixed). The problem is the regular expression used to detect the pointsize was wrong (they should have used pt instead of [pt]):

> tikzDevice:::getDocumentPointsize
function (docString)
{
psLocation <- regexpr("\\d+[pt]", docString, ignore.case = T,
perl = T)
if (psLocation == -1) {
return(NA)
}
else {
pointsize <- substr(docString, psLocation, psLocation +
attr(psLocation, "match.length") - 2)
return(as.numeric(pointsize))
}
}


There are a number of ways to fix this problem. The best way is to fix it in tikzDevice, of course. Before that happens, you can use this simple trick:

\documentclass[12pt,a4paper]{scrartcl}


That is, switch 12pt with a4paper so that 12 instead of 4 can be detected.

• Welcome to TeX.sx! – Peter Jansson Apr 2 '13 at 20:09
• Thank you for your answer! In my real setup, I do not set the font and paper size explicitly. I use my own document class which sets the sizes internally, so the LaTeX parser of tikzDevice does not see the font size. So the only solution for me was to set the sizes with knitr. – Peater de Xel Apr 4 '13 at 19:09