# How do I display Unicode characters with minted?

I use a minimal code example with Julia

\documentclass{article}

\usepackage[cache=false]{minted}

\begin{document}
\inputminted{julia}{polynomial_regression.jl}
\end{document}


on

# Polynomial Features ϕ = [1,x,x²,x³,...,xⁿ]' of Degree n
ϕ(x,n) = (x' .^ collect(range(0, stop=n, length=(n+1))))';

function polynomial_regression(x,y,xq,n)
𝚽  = ϕ(x,n); # Training data

# Linear Regression
θ = inv(𝚽'*𝚽)*𝚽'*y;

# Predict Query Data
yp = ϕ(xq,n)*θ;

return(yp);
end;


I get a

./Example-minimal.out.pyg:2: Package inputenc Error:    Unicode character Ï (U+3D5)
(inputenc)                not set up for use with LaTeX.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
...

l.2 ...1,x,xÂ²,xÂ³,...,xâ¿]\PYGZsq{} of Degree n}

• Welcome. Did you do a search in the forum to "Package inputenc Error: Unicode character"? There are numerous similar problems, probably they will give a solution. – albert Nov 6 '18 at 11:02
• Hm. You should perhaps consider to use xelatex or lualatex if you want to use so much unicode. And you probably will have to use some other font inside the listing, see e.g. tex.stackexchange.com/a/100621/2388. – Ulrike Fischer Nov 6 '18 at 11:08

## In the Legacy Toolchain

Load the legacy text encodings you’ll need with fontenc, then fill in any missing characters with \newunicodechar.

\documentclass{article}
\usepackage[LGR, T1]{fontenc}
\usepackage{textcomp}
\usepackage[utf8]{inputenc} % The default since 2018
\usepackage{lmodern, newunicodechar}
\usepackage[cache=false]{minted}
% To fit within the width allowed on TeX.SX:
\usepackage[paperwidth=10cm]{geometry}

% ϕ(U+03D5) is the math symbol with a vertical stroke.  It is in OML.
\renewcommand\textphisymbol{\ensuremath{\mathtt{\phi}}}
\newunicodechar{ⁿ}{\textsuperscript{n}}
% 𝚽(U+1D6DF) is present in certain font packages, but there is no typewriter
% bold upright font face of either cmtt or lmtt in LGR or OT1.  Should fall
% back to normal weight.
\newunicodechar{𝚽}{{\usefont{LGR}{\ttdefault}{bf}{n}\selectfont\symbol{"46}}}
% Should be the mathematical symbol, rather than the Greek lowercase letter:
\renewcommand\texttheta{\ensuremath{\mathtt{\theta}}}

\begin{document}
\inputminted{julia}{polynomial-regression.ji}
\end{document}


The following is cropped for display here:

Unfortunately, the alphabeta and babel packages appear to be incompatible with minted, but you can load textgreek as an alternative to redefining \texttheta, etc.

You’ll likely want to be more consistent than I was about whether you use upright or slanted Greek letters. The default font family doesn’t have a bold typewriter Φ, so in this sample it fell back to regular weight. You can substitute the glyph you prefer from any Type 1 font of your choice. The Φ glyph is in the legacy LGR, OML and OT1 encodings, and in several fonts with non-standard encodings as well.

## In the Modern Toolchain

Although you tagged the question with pdftex, the easiest and best solution by far is to load fontspec and a monospaced font that contains all the symbols you use, such as the one in your editor. Then compile with LuaLaTeX or XeLaTeX.

The following patches the CM Unicode fonts with a few Greek math symbols from Latin Modern Math:

\documentclass{article}
\usepackage{unicode-math}
\usepackage[cache=false]{minted}
\usepackage{newunicodechar}
% To fit within the width allowed on TeX.SX:
\usepackage[paperwidth=9cm]{geometry}

\defaultfontfeatures{Scale = MatchLowercase}
\setmainfont{CMU Serif}[Scale = 1.0]
\setsansfont{CMU Sans Serif}
\setmonofont{CMU Typewriter Text}
\setmathfont{Latin Modern Math}

% Although you could select a font that contains all the symbols your
% source uses, I’ll fall back here on redefining the handful that are
% missing.

\newfontface\mathsymbolfont{Latin Modern Math}
\newunicodechar{ϕ}{{\mathsymbolfont\mitphi}}
\newunicodechar{𝚽}{{\mathsymbolfont\mbfPhi}}

\begin{document}
\inputminted{julia}{polynomial-regression.ji}
\end{document}


This has a number of advantages, including that you can copy the source from the PDF and paste into an editor.

An alternative to ɸ would be to redefine ϕ (U+03D5, Greek Phi Symbol) as ɸ (U+0278, Latin Small Letter Phi), which CMU Typewriter Text does contain. Or you can instead make other Greek letters, such as θ, slanted for consistency.

The best solution that I could determine was manually defining all missing characters by

\DeclareUnicodeCharacter{3B4}{$\bm{\delta}$}
\DeclareUnicodeCharacter{3B8}{$\bm{\theta}$}
\DeclareUnicodeCharacter{3BB}{$\lambda$}
\DeclareUnicodeCharacter{3BC}{$\mu$}
\DeclareUnicodeCharacter{3BC}{$\pi$}
\DeclareUnicodeCharacter{3C3}{$\sigma$}
\DeclareUnicodeCharacter{3C0}{$\pi$}
\DeclareUnicodeCharacter{3D5}{$\bm{\phi}$}


until it shows no more errors. If anyone knows a more elegant approach, please add it!