# How do I avoid \PYG{err} in minted with pygments when using unicode/UTF-8 input?

Consider the following code:

\documentclass{minimal}
\usepackage[utf8x]{inputenc}
\usepackage{minted}
\usepackage{newunicodechar}
\newunicodechar{→}{\ensuremath{\to}}
\begin{document}
\begin{minted}{coq}
Require Import Utf8.
Check Set → Set.
\end{minted}
\end{document}


It outputs a pdf with this:

I don't like the red box; it is an indication of an error, despite the fact that the code is valid Coq. I say it is an error because in the _minted-... directory there is a file B9B79F25159396E029D998834CEDDFB4348AD1BE427E596276BBAC3B129C03FF.pygtex whose contents is

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kn}{Require} \PYG{k+kn}{Import} \PYG{n+nn}{Utf8}\PYG{p}{.}
\PYG{n+nc}{Check} \PYG{k+kn}{Set} \PYG{err}{→} \PYG{k+kn}{Set}\PYG{o}{.}
\end{Verbatim}


What I expect is coloring identical to if I replace → with ->:

The code in 538AF91E35E82BEEAFB959F2DD5440A2348AD1BE427E596276BBAC3B129C03FF.pygtex is then

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kn}{Require} \PYG{k+kn}{Import} \PYG{n+nn}{Utf8}\PYG{p}{.}
\PYG{n+nc}{Check} \PYG{k+kn}{Set} \PYG{o}{\PYGZhy{}\PYGZgt{}} \PYG{k+kn}{Set}\PYG{o}{.}
\end{Verbatim}


So my question is, how do I get minted to replace \PYG{err} with \PYG{o}?

If it's relevant, my version of pdflatex and pygmentize is

$pdflatex --version pdflatex: warning: running with administrator privileges MiKTeX-pdfTeX 2.9.6839 (1.40.19) (MiKTeX 2.9.6840 64-bit) Copyright (C) 1982 D. E. Knuth, (C) 1996-2018 Han The Thanh TeX is a trademark of the American Mathematical Society. using bzip2 version 1.0.6, 6-Sept-2010 compiled with curl version 7.56.1; using libcurl/7.56.1 WinSSL compiled with expat version 2.2; using expat_2.2.0 compiled with jpeg version 9.2 compiled with liblzma version 50020032; using 50020032 compiled with libpng version 1.6.34; using 1.6.34 compiled with libressl version LibreSSL 2.5.3; using LibreSSL 2.5.3 compiled with MiKTeX Application Framework version 3.6802; using 3.6802 compiled with MiKTeX Core version 9.6844; using 9.6844 compiled with MiKTeX Archive Extractor version 1.6300; using 1.6300 compiled with MiKTeX Package Manager version 3.6833; using 3.6833 compiled with poppler version 0.60.1 compiled with uriparser version 0.8.4 compiled with zlib version 1.2.11; using 1.2.11$ pygmentize.exe -V
Pygments version 2.0.2, (c) 2006-2014 by Georg Brandl.


The pdflatex log is available on my gist. According to the MiKTeX console, I am using minted v2.5 (2017/07/19).

• As pygments.org/demo/6790521 shows this is a feature of the Python library used by minted as such this is not a LaTeX problem. bitbucket.org/birkenfeld/pygments-main/issues/971/… and bitbucket.org/birkenfeld/pygments-main/issues/1395/… suggest this is known, but it doesn't seem that a fix is available. – moewe May 5 '19 at 20:16
• I don't think \newunicodechar works with utf8x, as I have specifically excluded it. Indeed, if you remove the calls to newunicodechar, you get exactly the same output. – egreg May 5 '19 at 21:02
• Huh, what's the reason for not supporting utf8x, or where's documentation explaining it? – Jason Gross May 6 '19 at 1:02
• @JasonGross utf8x takes a different approach towards managing UTF-8; plus, it's been unsupported for several years. – egreg May 6 '19 at 6:18

I see no other way than disabling \fcolorbox inside minted, see https://tex.stackexchange.com/a/343506/4427

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{minted}
\usepackage{newunicodechar}
\usepackage{xpatch}

\newunicodechar{→}{\ensuremath{\to}}

\makeatletter
\AtBeginEnvironment{minted}{\dontdofcolorbox}
\def\dontdofcolorbox{\renewcommand\fcolorbox[4][]{##4}}
\xpatchcmd{\inputminted}{\minted@fvset}{\minted@fvset\dontdofcolorbox}{}{}
\makeatother

\begin{document}
\begin{minted}{coq}
Require Import Utf8.
Check Set → Set.
\end{minted}

\end{document}


I get different coloring, though.

Please, take care that newunicodechar doesn't work with utf8x; you get the arrow nonetheless because utf8x provides a definition for →.

Package newunicodechar Warning: This package only works if the document
(newunicodechar)                encoding is utf8'.
`
• Thanks! Since I'm marking this answer as accepted, perhaps it should also mention that the error box comes from pygments and error on Unicode is not a minted thing, as per @moewe's comment? – Jason Gross May 6 '19 at 1:06