XeTeX – Fixing Unicode Rendering Issues Inside Minted Environment

mintedunicodexetex

Using xelatex, here is my MWE,

% Created 2021-10-30 Sat 14:35
% Intended LaTeX compiler: xelatex
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
% \usepackage[utf8]{inputenc}
\usepackage[mathletters]{ucs}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{eurosym}
\usepackage[T1]{fontenc}
% \usepackage{Alegreya} %% Option 'black' gives heavier bold face 
% \renewcommand*\oldstylenums[1]{{\AlegreyaOsF #1}}
% \setmonofont{} %CMU Typewriter Text

\usepackage{hyperref}
\hypersetup{colorlinks, allcolors=., colorlinks=true,linkcolor={blue!78!white}, urlcolor={purple}, filecolor={winered}}
\usepackage{xcolor} % to access the named colour LightGray
\definecolor{LightGray}{gray}{0.2}
\usepackage{minted}
\usemintedstyle{monokai}
\date{\today}
\title{}
\hypersetup{
 pdfauthor={},
 pdftitle={},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 27.2 (Org mode 9.4.4)}, 
 pdflang={English}}
\begin{document}
\begin{minted}[frame=lines,fontsize=\scriptsize,linenos=false, bgcolor=LightGray, mathescape]{julia}
function derivative_symb(exp)
    @variables ξ
    Dξ=Differential(ξ)
    ϕe = exp(ξ) # (
    return (eval(build_function(expand_derivatives(Dξ(ϕe)), ξ)),
    expand_derivatives(Dξ(ϕe)))
end
\end{minted}

\end{document}

enter image description here

Best Answer

If you want to use XeLaTeX, you should not place \usepackage[T1]{fontenc} in your preamble (as already pointed out by Ulrike in the comments). The default monospaced font used by TeX does not contain Greek letters. Therefore, as you use XeLaTeX, you should load a monospaced font that contains greek using the fontspec package. Your minimal setup (I removed all the packages that are not necessary for rendering this MWE) could then be as follows:

\documentclass[12pt]{article}

\usepackage{fontspec}
\setmonofont{TeX Gyre Cursor}

\usepackage{xcolor}
\definecolor{LightGray}{gray}{0.2}

\usepackage{minted}
\usemintedstyle{monokai}

\begin{document}

\begin{minted}[frame=lines, fontsize=\scriptsize, linenos=false, bgcolor=LightGray, mathescape]{julia}
function derivative_symb(exp)
    @variables ξ
    Dξ=Differential(ξ)
    ϕe = exp(ξ) # (
    return (eval(build_function(expand_derivatives(Dξ(ϕe)), ξ)),
    expand_derivatives(Dξ(ϕe)))
end
\end{minted}

\end{document}

This would give you the following:

enter image description here

Related Question