daqana / tikzDevice

A R package for producing graphics output as PGF/TikZ code for use in TeX documents.
https://daqana.github.io/tikzDevice
132 stars 26 forks source link

output file in UTF8 encoding #154

Closed jszhao closed 7 years ago

jszhao commented 7 years ago

If the plot code involve some Chinese character, and I also hope to use xetex as the engine, then the output TeX file, including tikzStringWidthCalc.tex, should be in UTF8 encoding. However, under Windows system in Chinese, it seems that the output file is always in CP936 encoding. It will cause the font metrics can not be calculated by xelatex.

yihui commented 7 years ago

Thanks for the report! I guess we probably won't have time to fix it in the near future. If you could figure out the fix, we will appreciate your pull request!

jszhao commented 7 years ago

Here is a mini example, that demo something:

library(tikzDevice)
options(tikzDefaultEngine = "xetex")
options(tikzXelatexPackages = c(getOption("tikzXelatexPackages"),
                                "\\setmainfont{Times New Roman}\n",
                                "\\usepackage[math-style=TeX]{unicode-math}\n",
                                "\\setmathfont{Times New Roman}\n",
                                "\\usepackage{xeCJK}\n", 
                                "\\setCJKmainfont{SimHei}\n\n"
                                ))
tikz('Fig2-a.tex', standAlone = TRUE, width=4, height=4)
plot(0, type = "n", xlab = "", ylab = "模式")

Then there is warning message:

Measuring dimensions of:  模式
Warning message:
In getMetricsFromLatex(TeXMetrics, verbose = verbose) :
  XeLaTeX was unable to calculate metrics for some characters:
         Missing character: There is no 模 in font ecrm1000!
         Missing character: There is no 式 in font ecrm1000!

After that:

dev.off()

The part of output file that related with the warning message is: \node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 10.80,150.54) {ģʽ}; If I replace 模式 with 中文,then I can get correct result, although the Fig2-a.tex is also in CP936 encoding.

jszhao commented 7 years ago

After change Line 273 of latexStrWidth.R in current git master from

texIn <- file(texFile, 'w')

to

texIn <- file(texFile, 'w', encoding="UTF-8")

the above warning message disappear. However, the final output file Fig2-a.tex also corrupted with \node[text=drawColor,rotate= 90.00,anchor=base,inner sep=0pt, outer sep=0pt, scale= 1.00] at ( 10.80,150.54) {ģʽ};