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

CJK render issue in latexpdf code block #9414

Closed
zhsj opened this issue Jul 6, 2021 · 9 comments · Fixed by #9419
Closed

CJK render issue in latexpdf code block #9414

zhsj opened this issue Jul 6, 2021 · 9 comments · Fixed by #9419

Comments

@zhsj
Copy link
Contributor

zhsj commented Jul 6, 2021

Describe the bug

Some extra spaces are inserted in the code block if there are CJK characters.

How to Reproduce

Create a simple project with 3 files.

==> conf.py <==
project = 'test'
copyright = '2021, test'
author = 'test'
language = 'zh_CN'
==> index.rst <==
.. code-block:: json

   {
     "test": "你好"
   }

.. code-block:: json

   {
     "test": "こんにちは"
   }
==> Makefile <==
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS    ?=
SPHINXBUILD   ?= sphinx-build
SOURCEDIR     = .
BUILDDIR      = _build

# Put it first so that "make" without argument is like "make help".
help:
        @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
        @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

Built with the docker

docker run --rm -it -v `pwd`:/docs sphinxdoc/sphinx-latexpdf

Expected behavior

No extra spaces.

Your project

none

Screenshots

image

There are some extra space between " and the first CJK character.

OS

Linux

Python version

3.9.6

Sphinx version

4.0.3

Sphinx extensions

No response

Extra tools

No response

Additional context

No response

@zhsj zhsj added the type:bug label Jul 6, 2021
@zhsj
Copy link
Contributor Author

zhsj commented Jul 6, 2021

Probably the root cause is at xeCJK 🤔

@tk0miya
Copy link
Member

tk0miya commented Jul 7, 2021

It seems this issue came from the combination of the JSON lexer of pygments and xeCJK package.

I can reproduce it with the following command (it does not use Sphinx at all):

echo '"你好"' | pygmentize -l json -f latex -O full -O 'preamble=\usepackage{xeCJK}' > json.tex && xelatex json.tex

スクリーンショット 2021-07-07 10 45 11

It seems whitespace appears just after the opening quote.

But it's not reproduced if I use python lexer.

echo '"你好"' | pygmentize -l python -f latex -O full -O 'preamble=\usepackage{xeCJK}' > python.tex && xelatex python.tex

スクリーンショット 2021-07-07 10 44 53

I don't know this bug is either pygments or xeCJK. But this is not related to Sphinx itself, I believe. Could you report this to their project, please?

@zhsj
Copy link
Contributor Author

zhsj commented Jul 7, 2021

Thanks for narrowing down the issue.

@zhsj zhsj closed this as completed Jul 7, 2021
@zhsj
Copy link
Contributor Author

zhsj commented Jul 7, 2021

https://tex.stackexchange.com/questions/374469/extra-spaces-when-using-fancyvrb-with-xecjk

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



\begin{document}

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

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

\end{document}

the output is

image

Any suggestion to create a local env and add \CJKsetecglue{} for Verbatim to workaround this?

@zhsj
Copy link
Contributor Author

zhsj commented Jul 7, 2021

I miss a better solution in the tex.stackexchange.com,

Adding \AtBeginEnvironment{Verbatim}{\CJKsetecglue{}} is just enough.

So for sphinx, it's adding \AtBeginEnvironment{sphinxVerbatim}{\CJKsetecglue{}} to preamble.

@tk0miya could sphinx workaround this by adding above line to default preamble when xeCJK is used?

@stone-zeng
Copy link

stone-zeng commented Jul 7, 2021

A better solution:

% Preamble
\fvset{formatcom=\xeCJKVerbAddon}

or

% Locally
\begin{Verbatim}[formatcom=\xeCJKVerbAddon]
"你好"
\end{Verbatim}

See CTeX-org/ctex-kit#331.

@zhsj
Copy link
Contributor Author

zhsj commented Jul 7, 2021

CTeX-org/ctex-kit#331 is written in Chinese, in summary, it's not easy to fix on xeCJK side.

@zhsj
Copy link
Contributor Author

zhsj commented Jul 7, 2021

I can confirm adding \fvset{formatcom=\xeCJKVerbAddon} to sphinx preamble configuration fix the problem.

@muzimuzhi
Copy link

Checking how they are defined, formatcom*=\xeCJKVerbAddon is better than formatcom=.... Also I've proposed a patch to xeCJK in CTeX-org/ctex-kit#580 (comment).

zhsj added a commit to zhsj/sphinx that referenced this issue Jul 7, 2021
zhsj added a commit to zhsj/sphinx that referenced this issue Jul 7, 2021
zhsj added a commit to zhsj/sphinx that referenced this issue Jul 7, 2021
zhsj added a commit to zhsj/sphinx that referenced this issue Jul 8, 2021
make xeCJK not add extra spaces in fancyvrb Verbatim enviroment.

Fixes: sphinx-doc#9414

Signed-off-by: Shengjing Zhu <i@zhsj.me>
zhsj added a commit to zhsj/sphinx that referenced this issue Jul 8, 2021
Prevent xeCJK from adding extra spaces in fancyvrb Verbatim enviroment.

Fixes: sphinx-doc#9414

Signed-off-by: Shengjing Zhu <i@zhsj.me>
@tk0miya tk0miya added this to the 4.1.0 milestone Jul 10, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants