# Unicode characters misplaced within lstlistings environment

I am compiling with XeLaTex.

Within the lstlistings environment (provided by the listings package), special unicode characters appear to be misplaced.

This is a minimal demonstration. Code:

\documentclass{article}

\usepackage[mathrm=sym]{unicode-math}

\usepackage{listings}

\lstset{
inputencoding = utf8x
, extendedchars = \true
, mathescape = false
, escapechar = {$} , basicstyle = \ttfamily , numbers = left } \setmonofont[Scale=0.85]{Courier New} \begin{document} \begin{lstlisting} (ζ). ( ζ). (ζ ). ζ (abcζ). (abc ζ). ( abcζ). \end{lstlisting} \end{document}  Output (cropped): The special unicode char is the zeta. It appears that what's happening is that the zeta is printed to the left of word immediately previous to it (words are delimited up by spaces, line-breaks, and some characters). In certain circumstances (e.g. lines 4 and 5) the special unicode character is shifted backward past a line-break. Another odd behavior (e.g. line 1) is that when there is no line-break previous to the special unicode char, the backward-shift interferes with line-numbers' placement (indenting the "1" for line 1). I honestly have no idea what is happening. What I'd like to preserve is the ability to enter unicode characters into lstlisting environments without special decoration or padding with spaces. ## 1 Answer Your settings are wrong, you don't need an inputencoding, and the value of extendendedchars should be true not \true. But the main problem is that listings is not prepared to handle chars above 256. You need to add such to the processing list. See also https://tex.stackexchange.com/a/25396/2388 \documentclass{article} \usepackage[mathrm=sym]{unicode-math} \usepackage{listings} \lstset{ , mathescape = false , escapechar = {$}
, basicstyle = \ttfamily
, numbers = left
}

\setmonofont[Scale=0.85]{Courier New}

\makeatletter
\lst@InputCatcodes
\def\lst@DefEC{%
\lst@CCECUse \lst@ProcessLetter
^^80^^81^^82^^83^^84^^85^^86^^87^^88^^89^^8a^^8b^^8c^^8d^^8e^^8f%
^^90^^91^^92^^93^^94^^95^^96^^97^^98^^99^^9a^^9b^^9c^^9d^^9e^^9f%
^^b0^^b1^^b2^^b3^^b4^^b5^^b6^^b7^^b8^^b9^^ba^^bb^^bc^^bd^^be^^bf%
^^c0^^c1^^c2^^c3^^c4^^c5^^c6^^c7^^c8^^c9^^ca^^cb^^cc^^cd^^ce^^cf%
^^d0^^d1^^d2^^d3^^d4^^d5^^d6^^d7^^d8^^d9^^da^^db^^dc^^dd^^de^^df%
^^e0^^e1^^e2^^e3^^e4^^e5^^e6^^e7^^e8^^e9^^ea^^eb^^ec^^ed^^ee^^ef%
^^f0^^f1^^f2^^f3^^f4^^f5^^f6^^f7^^f8^^f9^^fa^^fb^^fc^^fd^^fe^^ff%
^^^^03b6% <--- for ζ
^^00}
\lst@RestoreCatcodes
\makeatother

\begin{document}

\begin{lstlisting}
(ζ).
( ζ).
(ζ ).
ζ

(abcζ).
(abc ζ).
( abcζ).
\end{lstlisting}

\end{document}


• That works perfectly, thank you very much! It is a slightly clunky solution, but that's fine. Also, for others' reference, this solution works for multiple unicode characters at once, for example the ^^^^03b6 can be replaced by ^^^^03b1^^^^03b2^^^^03b3 to analogously register α, β, γ. Mar 31 '20 at 22:18