Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bookdown: Fix extra space before Chinese string inside R code chunk

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 .

example

like image 883
Andrew.T Avatar asked Dec 27 '17 10:12

Andrew.T


1 Answers

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.

like image 182
Yihui Xie Avatar answered Nov 08 '22 17:11

Yihui Xie