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

1

15

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

Gnotobiotics answered 27/12, 2017 at 10:6 Comment(6)
Come 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!Rebec
@YihuiXie voted to reopen, but it seems virtually none closed post could be flipped.Acree
@YihuiXie Thanks for your support. And your bookdown.Gnotobiotics
@Acree Thank you improved my question description.Gnotobiotics
Here is the solution to your problem: 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.Rebec
@YihuiXie Thanks a lot. It's work!Gnotobiotics
R
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.

Rebec answered 31/12, 2017 at 5:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.