首页 > 解决方案 > svg 设备、表达式()和 unicode 字符的问题

问题描述

我在使用带有数学表达式和 unicode 的 svg 设备时遇到了困难(在这种情况下,是左引号和右引号)。这只发生在我的 Windows 10 计算机上;我的 osx 电脑似乎不受影响。考虑以下 R 代码:

output = tempfile(fileext = ".svg")

svg(file = output)

plot(1, axes=FALSE, ty='n')

## Line A: 
mtext(side = 2, text = expression(paste("A")))
## Line B:
mtext(side = 3, text = expression(paste("“B")))
## Line C: 
mtext(side = 4, text = expression(paste("C")))

axis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)

dev.off()

## Uncomment and run to open the SVG file to look at it 
# browseURL(output)

(注意 B 行中的左引号)。在运行 C 线时,我得到

Error in mtext(side = 4, text = expression(paste("C"))) : 
  metric information not available for this device

那么,这似乎是默认字体 , 和 unicode 之间的svg交互expression。但仅在 Windows 10 上。我的 OSX 计算机(请参阅下面的详细信息)没有问题。

sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reprex_0.3.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6    ps_1.3.2        digest_0.6.25   R6_2.4.1        evaluate_0.14   rlang_0.4.5     fs_1.4.1       
 [8] callr_3.4.3     whisker_0.4     rmarkdown_2.1   Cairo_1.5-12    tools_3.6.2     xfun_0.13       compiler_3.6.2 
[15] processx_3.4.2  clipr_0.7.0     htmltools_0.4.0 knitr_1.28.3   

OSX 会话信息(在这台电脑上没问题)

> xfun::session_info('knitr')
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.4, RStudio 1.2.5031

Locale: en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8

Package version:
  evaluate_0.14   glue_1.4.0      graphics_3.6.1  grDevices_3.6.1
  highr_0.8       knitr_1.28.3    magrittr_1.5    markdown_1.1   
  methods_3.6.1   mime_0.9        stats_3.6.1     stringi_1.4.6  
  stringr_1.4.0   tools_3.6.1     utils_3.6.1     xfun_0.12      
  yaml_2.2.1 

知道是什么原因造成的吗?

标签: rsvgunicode

解决方案


我在我的 32 位 Windows 工作 PC 上遇到了同样的问题。这是一个棘手的错误,但我最终发现它GMMathTextgraphics.c的函数中被抛出。正如您所描述的,这似乎只是expressions 在某些图形设备上包含某些字符的问题。特别是,在 Windows 上使用 Cairo 的任何东西似乎都在同一点上碰壁。

在尝试了几种技巧来解决这个问题后,我发现最好的解决方案是简单地使用bquote而不是expression. 这使您可以保留plotmath获得的所有功能expression而不会产生错误:

svg(output)
plot(1, axes=FALSE, ty='n')
mtext(side = 2, text = bquote("A"))
mtext(side = 3, text = bquote("“B"))
mtext(side = 4, text = bquote("C"))
axis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)
dev.off()

这给出了以下 SVG:

在此处输入图像描述

有一个很好的概述如何使用来生成你的标签,包括你需要bquote所有数学表达式


推荐阅读