When Chinese string inside R code chunk, the compiled PDF will get a redundancy space before the string, how to avoid this extra space? Please refer the minimum case in github - bookdown-chinese .
Asked
Active
Viewed 331 times
15
-
10Come on! I have been frustrated so many times by those who vote to close questions without looking at the tags. The OP has given a perfect minimal reproducible example, and have any of you looked at it at all? How many of you actually use bookdown? And how many of you write Chinese? If you are not an expert in these areas, please give beginners a chance to defend themselves before you put questions on hold! – Yihui Xie Dec 27 '17 at 16:08
-
@YihuiXie voted to reopen, but it seems virtually none closed post could be flipped. – georgexsh Dec 27 '17 at 18:54
-
@YihuiXie Thanks for your support. And your bookdown. – Andrew.T Dec 28 '17 at 03:03
-
@georgexsh Thank you improved my question description. – Andrew.T Dec 28 '17 at 03:04
-
2Here is the solution to your problem: https://github.com/yihui/bookdown-chinese/commit/c70ef766b117f3 I cannot post it as an answer since they have put this question on hold, but the solution is simple enough. – Yihui Xie Dec 28 '17 at 05:15
-
@YihuiXie Thanks a lot. It's work! – Andrew.T Dec 28 '17 at 10:04
1 Answers
14
This issue was caused by the LaTeX package xeCJK. By default, it adds spaces between Chinese and non-Chinese characters, except in verbatim
environments. In your case, the code was not actually in a verbatim
environment, so you have to let xeCJK know that it should not add spaces automatically.
The solution is to add this line to your LaTeX preamble (the Highlighting
environment was defined by Pandoc when converting Markdown to LaTeX to syntax highlight code, and it is based on the fancyvrb package):
\RecustomVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},formatcom=\xeCJKVerbAddon}
For R Markdown documents, this line can be save in a .tex file, e.g., preamble.tex
, and included via the includes
option, e.g.,
output:
pdf_document:
includes:
in_header: preamble.tex
See this Github issue for the full technical background.

Yihui Xie
- 28,913
- 23
- 193
- 419