Bookdown:修复R代码块中的中文字符串之前的额外空间

时间:2022-06-08 20:22:20

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 .

当中文字符串里面的R代码块时,编译后的PDF会在字符串前得到一个冗余空间,如何避免这个额外的空间呢?请参考github中的最小案例 - bookdown-chinese。

Bookdown:修复R代码块中的中文字符串之前的额外空间

1 个解决方案

#1


10  

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.

此问题是由LaTeX包xeCJK引起的。默认情况下,它会在中文和非中文字符之间添加空格,但逐字环境除外。在您的情况下,代码实际上并不是逐字环境,因此您必须让xeCJK知道它不应该自动添加空格。

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):

解决方案是将此行添加到LaTeX前导码中(突出显示环境由Pandoc在将Markdown转换为LaTeX时定义为语法高亮代码,并且它基于fancyvrb包):

\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.,

对于R Markdown文档,该行可以保存在.tex文件中,例如preamble.tex,并通过includes选项包括,例如,

output:
  pdf_document:
    includes:
      in_header: preamble.tex

See this Github issue for the full technical background.

有关完整的技术背景,请参阅此Github问题。

#1


10  

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.

此问题是由LaTeX包xeCJK引起的。默认情况下,它会在中文和非中文字符之间添加空格,但逐字环境除外。在您的情况下,代码实际上并不是逐字环境,因此您必须让xeCJK知道它不应该自动添加空格。

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):

解决方案是将此行添加到LaTeX前导码中(突出显示环境由Pandoc在将Markdown转换为LaTeX时定义为语法高亮代码,并且它基于fancyvrb包):

\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.,

对于R Markdown文档,该行可以保存在.tex文件中,例如preamble.tex,并通过includes选项包括,例如,

output:
  pdf_document:
    includes:
      in_header: preamble.tex

See this Github issue for the full technical background.

有关完整的技术背景,请参阅此Github问题。