在R语言中使用Verdana字体生成PDF文件的技术问题
Got it, let's get your Verdana font working in your R-generated PDFs smoothly. Since you've confirmed the font is already installed locally (and shows up in fonts()), here's what you need to do:
第一步:安装并加载字体管理包
R's basepdf()function can be finicky with system fonts, so we'll use theextrafontpackage to bridge the gap. Install and load it first:install.packages("extrafont") library(extrafont)第二步:注册Verdana到R的字体库
Run these commands to make R recognize your local Verdana font (you only need to do this once—after that, the font will stay available even if you restart R):font_import(pattern = "Verdana") # Target only Verdana to speed up the process loadfonts(device = "pdf") # Load the font specifically for PDF output第三步:生成PDF with Verdana
Now you can directly specifyfamily = "Verdana"in yourpdf()call, just like you did with Helvetica:pdf("abc.pdf", height = 11, width = 8.5, family = "Verdana") # Add your plotting/output code here, e.g.: plot(1:10, main = "This Title Uses Verdana!") dev.off()Alternative: Use
cairo_pdf()if you hit snags
If the basepdf()method doesn't work for your system,cairo_pdf()has better native support for system fonts. Try this instead:cairo_pdf("abc_cairo.pdf", height = 11, width = 8.5, family = "Verdana") plot(1:10, main = "Verdana via Cairo PDF") dev.off()
A quick note: font_import() might take a minute to run—if you omit the pattern argument, it will import all your system fonts, but targeting just Verdana saves time. Also, make sure you're running a relatively recent version of R to avoid compatibility hiccups.
内容的提问来源于stack exchange,提问作者Vik G

