Reputation: 16089
I would like to use a consistent typeface for my plots in a knitr
document. Right now I am switching between cairo_pdf
and pdf
. The reason I am using cairo_pdf
in some plots is to avoid en-dashes in certain circumstances. But when cairo_pdf
is invoked on my system, I get an Arial font; whereas pdf
uses Helvetica.
\documentclass{article}
\begin{document}
<<>>=
knitr::opts_chunk$set(fig.height = 1, out.height = "1in", fig.width=1, out.width="1in")
@
<<pdf, dev='pdf'>>=
library(ggplot2)
qplot(x = 1, y = 1, geom = "blank") +
xlab("RQac")
@
<<cairo, dev='cairo_pdf'>>=
qplot(x = 1, y = 1, geom = "blank") +
xlab("RQac")
@
\end{document}
Ideally, I'd like to select the font (and not just Arial or Helvetica). However, I cannot install the extrafonts
package.
install.packages("extrafonts")
results in a prompt for the installation of Rttf2pt1
which fails to compile.
Warning: running command 'make --no-print-directory -f "Makefile.win"' had status 2
ERROR: compilation failed for package 'Rttf2pt1'
* removing 'C:/R/R-3.3.0/library/Rttf2pt1'
How can I use Arial font in every chunk? How can I use Helvetica in every chunk?
Upvotes: 1
Views: 753
Reputation: 16089
The interim solution I had was to use the showtext
package. The disadvantage is that I have to specify the face through a theme. However, this downside can be alleviated by using a default theme. As I am using the \usepackage{helvet}
in LaTeX, this solution also ensures I am using the identical family as the body text, rather than a (very close) clone.
Using tikz
appears to be a plausible solution, though is quite unwieldy. (Almost every charts required attention outside knitr.)
\documentclass[a4paper,10pt]{article}
\begin{document}
<<font_add>>=
library(showtext)
library(sysfonts)
library(knitr)
font.add("helvet",
regular = "C:/Program Files/MiKTeX 2.9/fonts/type1/urw/helvetic/uhvr8a.pfb",
bold = "C:/Program Files/MiKTeX 2.9/fonts/type1/urw/helvetic/uhvb8a.pfb",
italic = "C:/Program Files/MiKTeX 2.9/fonts/type1/urw/helvetic/uhvro8a.pfb")
my_pdf <- function(file, width, height){
pdf(file = file, width = width, height = height
# ,family = "helvet"
)
}
@
<<>>=
knitr::opts_chunk$set(fig.height = 1, out.height = "1in", fig.width=5, out.width="5in")
@
\subsubsection*{No distinction between hyphens and negative symbols}
<<pdf, dev='pdf'>>=
library(ggplot2)
qplot(x = 1, y = 1, geom = "blank") +
xlab(paste0("RQac 2012-13", "\U2212", "500"))
@
\subsubsection*{Wrong family, though distinction preserved}
<<cairo, dev='cairo_pdf'>>=
qplot(x = 1, y = 1, geom = "blank") +
xlab(paste0("RQac 2012-13", "\U2212", "500"))
@
\subsubsection*{Correct family and distinction, though the base family must be called}
<<my_pdf, dev='my_pdf', fig.ext='pdf', fig.showtext=TRUE>>=
qplot(x = 1, y = 1, geom = "blank") +
theme_gray(base_family = "helvet") +
xlab(paste0("RQac 2012-13", "\U2212", "500"))
@
\end{document}
Upvotes: 2