2

I posted this question at tex.stackexchange.com less than two weeks ago, nobody has been able to suggest a solution, so I hope that it is not a problem that I duplicate the question here.

I want to use tikz to inlcude R plots in my documents. I want to set absolutely all typefaces, fontsizes, fontweights to the TeX-document font body. I looked up the documentation of tikzDevice and par(), however I wasn't able to find a way to do this. Attached is a sample R-script generating some figure which needs to be cured:

library(tikzDevice)
tikz("/home/marius/Dokumenter/stk1110/oblig2/fig1.tex", width = 6, height = 5, pointsize=11)

# some data
menn = c(36.1,36.3,36.4,36.6,36.6,36.7,36.7,37.0,36.5,37.1)  
kvinner = c(36.6,36.7,36.8,36.8,36.7,37.0,37.1,37.3,36.9,37.4)

par(mfrow=c(2,3))

# first row
boxplot(menn)
qqnorm(menn)
qqline(menn)
hist(menn, freq=FALSE)
x = seq(36,37.4,0.01)
lines(x, 1/(2*pi*var(menn))^0.5*exp(-(x-mean(menn))^2/(2*var(menn))))

# second row
boxplot(kvinner)
qqnorm(kvinner)
qqline(kvinner)
hist(kvinner, freq=FALSE)
lines(x, 1/(2*pi*var(kvinner))^0.5*exp(-(x-mean(kvinner))^2/(2*var(kvinner))))

par(ps=1, cex=1, cex.main=111, cex.lab=111, cex.axis=111, cex.sub=111, font.main=1)
dev.off()

And a TeX document:

\documentclass[11pt,norsk,a4paper]{article}
\usepackage[norsk]{babel}
\usepackage[paper=a4paper, top=1in,bottom=1.1in,right=0.9in,left=0.9in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[makeroom]{cancel}
\usepackage{amsmath,amsthm}
\usepackage{newtxtext}
\usepackage[T1]{fontenc}
\usepackage{pgf, pgfplots}
\usepackage{textcomp}
\date{\normalsize \today}
\begin{document}
I want all fonts in R plots to be exactly like this one.
\begin{figure}[!htbp]
  \begin{center}
    \input{fig1.tex}
  \end{center}
\end{figure}
\end{document}

I'd like this to work for the majority of different plots in R, if not all, so that I can have exactly the same font as my document body. Even for figure titles. Are you able to suggest an solution?

Mikkel Rev
  • 863
  • 3
  • 12
  • 31
  • Did you solve your Problem? I do try to do the same and can't figure out a solution. if you solved your Problem please post your Solution. – Hans Peter Nov 03 '21 at 08:49

1 Answers1

0

tikz() will output latex code. If you include this code in a latex document, the fonts should follow the settings of this main document in terms of typefaces etc.

That the titles are boldfaced and not all text has the same size is not an issue of tikz(), but of plot() and has to be handled there (to the best of my knowledge). See for example here: Adjust plot title and sub-title in base R . I.e. you might want to set cex etc.: hist(kvinner, freq=FALSE, cex.main = 1, font.main = 1). See also: tikzDevice main caption without boldface

You can also create a standalone figure by setting tikz(..., standAlone = TRUE) that you can then render via pdflatex test.tikz (or LuaTeX or XeLaTeX with some extra settings).

In this case you need to pass in the relevant parts of the preamble, such as packages via options()

options(tikzDocumentDeclaration = "\\documentclass[11pt,norsk,a4paper]{article}\n", 
        tikzLatexPackages = 
            c(getOption("tikzLatexPackages"), 
            "\\usepackage[paper=a4paper, top=1in,bottom=1.1in,right=0.9in,left=0.9in]{geometry}\n",
            "\\usepackage[utf8]{inputenc}\n")
    )  
tikz("fig1.tex", width = 6, height = 5, standAlone = TRUE)
...
dev.off()

We need to add new options to the default options and not overwrite them, which is why we concatenate with getOption("tikzLatexPackages"). Also note that you have to escape \in the R strings.

fry
  • 509
  • 3
  • 10