1

I have the minimal document here:

\documentclass{article}
\usepackage{unicode-math}
\usepackage{listings}

\setmainfont{Stix}
\setmathfont{Latin Modern Math}
\setmonofont{Fira Mono}

\begin{document}
this is λ lambda
$$\int_0^\infty λ\lambda$$
\begin{lstlisting}
# some random pseudocode using unicode
def function(λ): 
    αδ = βθ

def function( λ): 
    pass
\end{lstlisting}
\end{document}

producing with lualatex:

enter image description here

Look at how the unicode characters are rendered in the listing, but not in the right place.

1

2 Answers 2

3

I'm afraid you need to add manually all the Unicode characters you need.

\documentclass{article}
\usepackage{unicode-math}
\usepackage{listings}

\setmainfont{STIX Two Text}
\setmathfont{STIX Two Math}
\setmonofont{Fira Mono}

\makeatletter % see https://tex.stackexchange.com/a/320345
\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%
  ^^a0^^a1^^a2^^a3^^a4^^a5^^a6^^a7^^a8^^a9^^aa^^ab^^ac^^ad^^ae^^af%
  ^^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%
  ^^^^20ac^^^^0153^^^^0152%
  ^^^^03b1% alpha
  ^^^^03b2% beta
  ^^^^03b4% delta
  ^^^^03bb% lambda
  ^^^^03b8% theta  
  ^^00}
\lst@RestoreCatcodes
\makeatother

\lstset{basicstyle=\ttfamily}


\begin{document}
this is λ lambda
\[\int_0^\infty λ\lambda\]
\begin{lstlisting}
# some random pseudocode using unicode
def function(λ): 
    αδ = βθ

def function(λ): 
    pass
\end{lstlisting}
\end{document}

enter image description here

1

As an alternative you can use the minted package (compile with --shell-escape). Note that I changed the fonts because I don't have STIX or Fira Mono.

\documentclass{article}
\usepackage{unicode-math}
\usepackage{minted}

\setmainfont{CMU Serif}
\setsansfont{CMU Sans Serif}
\setmonofont{CMU Typewriter Text}
\setmathfont{Latin Modern Math}

\begin{document}
this is λ lambda
$$\int_0^\infty λ\lambda$$
\begin{minted}{python}
# some random pseudocode using unicode
def function(λ): 
    αδ = βθ

def function( λ): 
    pass
\end{minted}
\end{document}

enter image description here

4
  • It does not answer the question which was about listings. It works well with minted, even the unicode characters appear with a red rectangle around. Commented Dec 2, 2020 at 10:47
  • @HugoTrentesaux I agree that it does not answer the question as asked, but it may be a useful alternative (for you and/or for future visitors of the site). As you can see I don't have the red rectangles in my output (and indeed this is valid Python code).
    – Marijn
    Commented Dec 2, 2020 at 10:54
  • Maybe tex.stackexchange.com/questions/343494/… could be useful to address the red rectangles, see also tex.stackexchange.com/questions/14166/….
    – Marijn
    Commented Dec 2, 2020 at 10:55
  • 1
    I upvoted this answer since it can be useful for other people but in my case I do not want to migrate my whole document to minted, that's why I trying to fix it using listings. Commented Dec 2, 2020 at 11:03

Not the answer you're looking for? Browse other questions tagged .