使用 writeLines() 在 Windows 中写入 UTF-8
Writing UTF-8 in Windows using writeLines()
我使用 Window10 64、R Studio 版本 1.1.383 和 MiKTeX 2.9。
我尝试使用函数使用 rmarkdown 从 html 文本打印 PDF。
# function html -> pdf
write_pdfx <- function(x){
for(i in 1:nrow(x)) {
message(sprintf("Processing %s", x$id[i]))
tf <- tempfile(fileext=".html")
writeLines(x$content[i], tf, useBytes = FALSE)
pandoc_convert(
input = tf,
to = "latex",
output = sprintf("%s.pdf",x$id[i]),
wd = getwd()
)
unlink(tf)
}}
df 包含两列:带有 id 的 id
和带有 html 文本的 content
。编码为UTF-8
Encoding(df$content) <- "UTF-8"
不幸的是,html 文本包含很多特殊字符。他们中的大多数(例如“ü”或“ä”不会引起任何问题)。但是,有些,如“ẗ”会导致错误:
pandoc.exe: Cannot decode byte '\xfc': Data.Text.Internal.Encoding.Fusion.streamUtf8: Invalid UTF-8 stream
Error: pandoc document conversion failed with error 1
如果我设置 useBytes = TRUE
我会得到另一个错误:
! Package inputenc Error: Unicode char ẗ (U+1E97)
(inputenc) not set up for use with LaTeX.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
l.212 ...sene Vergleich, wonach ersterer gestüẗ
Try running pandoc with --latex-engine=xelatex.
pandoc.exe: Error producing PDF
我也试过xelatex,没有成功。
Package inputenc Error & Error: pandoc document conversion failed with error 43
没有解决问题。
我也找到了这个信息,但是我无法实现它:
https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16064
在我的例子中,有什么方法可以在 Windows 上使用 writeLines()
编写 UTF-8?
找到解决方案:我没有使用正确的设置切换到 xelatex。
将 options ="--latex-engine=xelatex
添加到 pandoc_convert
解决了问题! :D
我使用 Window10 64、R Studio 版本 1.1.383 和 MiKTeX 2.9。 我尝试使用函数使用 rmarkdown 从 html 文本打印 PDF。
# function html -> pdf
write_pdfx <- function(x){
for(i in 1:nrow(x)) {
message(sprintf("Processing %s", x$id[i]))
tf <- tempfile(fileext=".html")
writeLines(x$content[i], tf, useBytes = FALSE)
pandoc_convert(
input = tf,
to = "latex",
output = sprintf("%s.pdf",x$id[i]),
wd = getwd()
)
unlink(tf)
}}
df 包含两列:带有 id 的 id
和带有 html 文本的 content
。编码为UTF-8
Encoding(df$content) <- "UTF-8"
不幸的是,html 文本包含很多特殊字符。他们中的大多数(例如“ü”或“ä”不会引起任何问题)。但是,有些,如“ẗ”会导致错误:
pandoc.exe: Cannot decode byte '\xfc': Data.Text.Internal.Encoding.Fusion.streamUtf8: Invalid UTF-8 stream
Error: pandoc document conversion failed with error 1
如果我设置 useBytes = TRUE
我会得到另一个错误:
! Package inputenc Error: Unicode char ẗ (U+1E97)
(inputenc) not set up for use with LaTeX.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
...
l.212 ...sene Vergleich, wonach ersterer gestüẗ
Try running pandoc with --latex-engine=xelatex.
pandoc.exe: Error producing PDF
我也试过xelatex,没有成功。
Package inputenc Error & Error: pandoc document conversion failed with error 43 没有解决问题。
我也找到了这个信息,但是我无法实现它:
在我的例子中,有什么方法可以在 Windows 上使用 writeLines()
编写 UTF-8?
找到解决方案:我没有使用正确的设置切换到 xelatex。
将 options ="--latex-engine=xelatex
添加到 pandoc_convert
解决了问题! :D