0

I am trying to typeset text including CJK ideographs in a verbatim environment. Because I want to indent my verbatim text slightly, I am using the fancyvrb package. Here's a MWE:

\documentclass{article}
\usepackage{parskip}

\usepackage{fancyvrb}
\DefineVerbatimEnvironment{verbatim}{Verbatim}{xleftmargin=2em}

\usepackage{xeCJK}
\setCJKmonofont{Noto Sans CJK JP}

\begin{document}
Code like this\ldots
\begin{verbatim}
auto x = "林";
\end{verbatim}
\end{document}

However I find there's an extra space character in the output, immediately between the opening double-quote and the ideograph.

Screeshot

It does seem to be a space character and not just excessive kerning, as it copies-and-pastes from the PDF as a space character. Can anyone see how to get rid of it? In case it's relevant, I'm using XeLaTeX version 3.14159265-2.6-0.99996.

It doesn't happen with LaTeX's built-in verbatim environment, but I don't know a way of altering the margins of that. I'm not wedded to using fancyvrb, if I can set the left margin of the verbatim environment some other way.

2

I'm having the same problem as you. It's because xeCJK is inserting glue (a whitespace by default) between english and CJK characters.

Solution 1:

\xeCJKsetup{xCJKecglue={}}

Be careful that this is a global setup, it will affect the whole document, not only the verbatim environment.

Solution 2:

Wrap verbatim env inside {\CJKsetecglue{} ...your normal code... }, so it only takes effect locally.

{
    \CJKsetecglue{}
    \begin{verbatim}
    auto x = "林";
    \end{verbatim}
}
  • In the end I went with a compromise between your two solutions, and used a \renewenvironment{verbatim} to do the \CJKsetecglue{} automatically along the lines of this solution. I didn't want to do it unconditionally (per your first solution) because that glue is needed in ordinary text (e.g. "Set x to contain the glyph 林"). – richard Jun 20 '17 at 16:03
  • Also, now I know what to search for, this question is essentially the same problem. – richard Jun 20 '17 at 16:04
  • I too know what to search for after reading xeCJK document. Other than \renewenvironment you can also use \AtBeginEnvironment{verbatim}{\CJKsetecglue{}} – duyue Jun 21 '17 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.