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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With