Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xeCJK: Verbatim里不应该自动加空格 #580

Closed
zhsj opened this issue Jul 7, 2021 · 8 comments
Closed

xeCJK: Verbatim里不应该自动加空格 #580

zhsj opened this issue Jul 7, 2021 · 8 comments

Comments

@zhsj
Copy link

zhsj commented Jul 7, 2021

\documentclass{article}
\usepackage{fancyvrb}
\usepackage[utf8]{inputenc}
\usepackage{xeCJK}

\begin{document}

{
\CJKsetecglue{}
\begin{Verbatim}
"你好"
\end{Verbatim}
}

{
\begin{Verbatim}
"你好"
\end{Verbatim}
}

{
"你好"
}

\end{document}

使用 xelatex 编译后渲染出来的效果是

image

我认为Verbatim环境里不应该自动加空格,不需要用户手动设置 \CJKsetecglue{}

原始问题 sphinx-doc/sphinx#9414 在使用 sphinx 编译文档时,发现渲染出来的代码片段里有多余的空格。

@sikouhjw
Copy link

sikouhjw commented Jul 7, 2021

已知问题:#331

@zhsj
Copy link
Author

zhsj commented Jul 7, 2021

但是问题好像并没有修复呀,是不在xeCJK修复的范围吗? 😢

另外,如果不在 xeCJK 中修复的话,在下游软件(比如sphinx)中推荐的修复方法是哪个呀? #331 中好像提到了几种方案。。

@sikouhjw
Copy link

sikouhjw commented Jul 7, 2021

但是问题好像并没有修复呀,是不在xeCJK修复的范围吗? 😢

我觉得是不好修,因为这个环境不是内核里有的。

另外,如果不在 xeCJK 中修复的话,在下游软件(比如sphinx)中推荐的修复方法是哪个呀? #331 中好像提到了几种方案。。

在导言区对环境打补丁。

@stone-zeng
Copy link
Member

可以考虑给 fancyvrb 包单独打个补丁的。

@muzimuzhi
Copy link
Contributor

一种补丁方式

\documentclass{article}
\usepackage{fancyvrb}
% \usepackage{fvextra}
\usepackage{xeCJK}

\makeatletter
\ExplSyntaxOn
\AtBeginDocument
  {
    \xeCJK_if_package_loaded:nTF {fvextra}
      {
        \tl_gput_left:Nn \FV@FormattingPrep@PostHook
          { \__xeCJK_verb_font_hook: }
      }
      {
        \xeCJK_if_package_loaded:nT {fancyvrb}
          {
            \tl_gput_right:Nn \FV@FormattingPrep
              { \__xeCJK_verb_font_hook: }
          }
      }
  }
\ExplSyntaxOff
\makeatother

\begin{document}

normal: "你好"

in \verb|\verb| and \verb|verbatim|
\verb|"你好"|, \begin{verbatim}
"你好"
\end{verbatim}

in \verb|\Verb| and \verb|Verbatim|
\Verb|"你好"|, \begin{Verbatim}
"你好"
\end{Verbatim}

\end{document}

image

@zhsj
Copy link
Author

zhsj commented Jul 7, 2021

多谢。 @stone-zengsphinx-doc/sphinx#9414 (comment) 上的 comment 好像比较简单 😂

@muzimuzhi
Copy link
Contributor

muzimuzhi commented Jul 7, 2021

这里考虑得更多,例如

  • 如果有全局的 \fvset{formatcom=<code>},则再用一次 formatcom=... 会覆盖之前的 <code>。所以我在 CJK render issue in latexpdf code block sphinx-doc/sphinx#9414 (comment) 建议使用 formatcom*。这对于程序自动生成的 Verbatim 环境,应该够用了。
  • 对于用户手动输入的情况,我提供的补丁方式,还能让行为受 xeCJK 选项 Verb 的控制、在需要开启时自动应用 formatcom*=\xeCJKVerbAddon 等。

@muzimuzhi
Copy link
Contributor

直接 patch fancyvrb 里和 \verbatim@font 对应的 \FV@SetupFont 也是个办法

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{xeCJK}

\makeatletter
\ExplSyntaxOn
\AtBeginDocument
  {
    \xeCJK_if_package_loaded:nT {fancyvrb}
      {
        \tl_put_right:Nn \FV@SetupFont
          { \__xeCJK_verb_font_hook: }
      }
  }
\ExplSyntaxOff
\makeatother

\begin{document}
normal: "你好"

in \verb|\verb| and \verb|verbatim|
\verb|"你好"|, \begin{verbatim}
"你好"
\end{verbatim}

in \verb|\Verb| and \verb|Verbatim|
\Verb|"你好"|, \begin{Verbatim}
"你好"
\end{Verbatim}

\end{document}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants