CTeX-org / ctex-kit

Macro Packages and Scripts for Chinese TeX users
976 stars 124 forks source link

xeCJK inserts extra spaces in listings environment #526

Closed cykerway closed 4 years ago

cykerway commented 4 years ago

The problem is exactly as posted here: https://tex.stackexchange.com/a/178708/4288

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}
\lstset{commentstyle=\color{blue},morecomment=[l]{//}}

\usepackage{xeCJK}
\setCJKmainfont{FandolSong-Regular.otf}

\begin{document}

\section{Success}
\begin{lstlisting}
// tomorrow明天
\end{lstlisting}

\end{document}

The package shouldn't introduce extra spaces in code block, otherwise when users copy-paste the code they may get incorrect result. For example: print("一二三") is not the same as print("一 二 三").

cykerway commented 4 years ago

Adding columns=fullflexible in \lstset fixed this problem. Probably a bug in listings not xeCJK.

muzimuzhi commented 4 years ago
  1. listings decides to set columns=[c]fixed by default. This is not a bug. See https://github.com/CTeX-org/forum/issues/7 and this question on TeX.SX.
  2. There is no guarantee that TeX can produce what-you-copied-is-what-you-see PDF, especially for spaces (or more generally horizontal skips between characters), font encodings issues (try to copy text containing ⟨...⟩ from listings.pdf), and xetex's codepoint degeneration (see #286 and this answer on TeX.SX). Also, PDF readers may behave differently when copying from the same PDF, see #400.