1

I created a dynamic ".rnw" file with knitr package. The file contains some Chinese characters. The document works fine when the inline output function ``\Sexpr{}'' is not included in the script.

\documentclass[11pt]{article}
\usepackage{ctex}
\begin{document}
<<>>=
sessionInfo()
mp <- mean(pressure$pressure)
@
平均值
\end{document}

And the output of the script is:

## R version 3.3.1 (2016-06-21)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.12.1 (Sierra)
##
## locale:
## [1] C
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] knitr_1.14
##
## loaded via a namespace (and not attached):
## [1] magrittr_1.5 tools_3.3.1 stringi_1.1.2 stringr_1.1.0 evaluate_0.10

平均值

But if I used the inline output function ``\Sexpr{}'' to print out the results, the Chinese characters become garbled. Any suggestions?

\documentclass[11pt]{article}
\usepackage{ctex}
\begin{document}
<<>>=
mp <- mean(pressure$pressure)
@
平均值=\Sexpr{mp}
\end{document}

And the output becomes: < U+5E73>< U+5747>< U+503C>=124.3367053

Cœur
  • 37,241
  • 25
  • 195
  • 267
Likan Zhan
  • 1,056
  • 6
  • 14

1 Answers1

0

Copyed from another question

Type

system("defaults write org.R-project.R force.LANG en_US.UTF-8")

and press Enter

Restart R

Likan Zhan
  • 1,056
  • 6
  • 14