30

When using Minted i get a red box around greek characters Annoying red box around greek characters

Can this behaviour be disabled or is there a specific line that needs to be changed in the python lexer?

A minimal working example

\documentclass[12pt,a4paper]{article}

\usepackage{fontspec}
\setmainfont{XITS}
\setmonofont{Consolas}

\usepackage{minted}
\setminted[python]{
    linenos=true,
    breaklines=true,
    encoding=utf8,
    fontsize=\footnotesize,
    frame=lines
}
\begin{document}
\section{Some code in this section}
\begin{minted}{python}
def add(α, β):
    return α + β
\end{minted}
\end{document}

Output of minimal working example

edit: I am using xelatex

3
  • Can you please add a minimal example of code?
    – egreg
    Dec 11, 2016 at 16:12
  • 1
    You get this behaviour, too, if you use German umlauts (ä, ö, ü). Is there a "real" solution yet? May 7, 2018 at 15:13
  • 1
    This seems to be a feature of the underlying Pygments library (cf pygments.org/demo/6790720), so you would probably need to open an issue at bitbucket.org/birkenfeld/pygments-main/issues (if there isn't one already, I haven't checked) to get this changed. From the LaTeX side of things there are probably only work-around that treat the symptom, not the disease.
    – moewe
    May 7, 2019 at 7:57

5 Answers 5

35

This seems to be an error in the Python lexer. If you don't need \fcolorbox inside the minted environment, here's a hack:

\documentclass[12pt,a4paper]{article}

\usepackage{fontspec}
\setmainfont{XITS}
\setmonofont{Source Code Pro} % I don't have Consolas

\usepackage{minted}
\setminted[python]{
    linenos=true,
    breaklines=true,
    encoding=utf8,
    fontsize=\footnotesize,
    frame=lines
}
\usepackage{etoolbox}

\makeatletter
\AtBeginEnvironment{minted}{\dontdofcolorbox}
\def\dontdofcolorbox{\renewcommand\fcolorbox[4][]{##4}}
\makeatother

\begin{document}

\section{Some code in this section}
\begin{minted}{python}
def add(α, β):
    return α + β
\end{minted}
\end{document}

\end{document}

enter image description here

Here's the patch also for \inputminted:

\begin{filecontents*}{\jobname.py}
def add(α, β):
    return α + β
\end{filecontents*}

\documentclass[12pt,a4paper]{article}

\usepackage{fontspec}
\setmainfont{XITS}
\setmonofont{Source Code Pro} % I don't have Consolas

\usepackage{minted}
\setminted[python]{
    linenos=true,
    breaklines=true,
    encoding=utf8,
    fontsize=\footnotesize,
    frame=lines
}
\usepackage{etoolbox,xpatch}

\makeatletter
\AtBeginEnvironment{minted}{\dontdofcolorbox}
\def\dontdofcolorbox{\renewcommand\fcolorbox[4][]{##4}}
\xpatchcmd{\inputminted}{\minted@fvset}{\minted@fvset\dontdofcolorbox}{}{}
\xpatchcmd{\mintinline}{\minted@fvset}{\minted@fvset\dontdofcolorbox}{}{} % see https://tex.stackexchange.com/a/401250/
\makeatother

\begin{document}

\section{Some code in this section}
\begin{minted}{python}
def add(α, β):
    return α + β
\end{minted}

\inputminted{python}{\jobname.py}

\end{document}

enter image description here

9
  • This hack seems to work fine for my minimal example but when i use it in conjunction with \inputminted I still run in to problems. Dec 11, 2016 at 17:21
  • !input minted causing problems Dec 11, 2016 at 17:31
  • 3
    @KimPetersen Patch for \inputminted added. I used filecontents just to make the example self-contained.
    – egreg
    Dec 11, 2016 at 19:06
  • I tried to understand that code, but i wasn't able to :-( . Could provide me someone please a sample to stop hightlight errors in Minted Inline? I will be very thankful.
    – Ivo Looser
    Jan 29, 2017 at 18:15
  • 1
    @IvoLooser The code simply disables \fcolorbox inside minted snippets.
    – egreg
    Jan 29, 2017 at 18:53
10

I suggest you try to use a minted style that does not higlight \PYG{err}

\usemintedstyle{xcode}

Of course if your red boxes was generated because of some \PYG{err}

1
  • This is the best answer. I would suggest also mentioning that styles can be defined for individual instances instead of globally if you normally want syntax highlighting. For example, I was just editing a book on C which states, “The form 'abc' is officialy undefined.” I typeset it using \mintinline[style=xcode]{C}{'abc'} to avoid the red boxes around the single-ticks.
    – hackerb9
    Sep 18 at 23:47
3

Just for completeness and since I can't comment yet:

The hack in the Answer by egreg can also be applied to the command \mintinline by

\xpatchcmd{\mintinline}{\minted@fvset}{\minted@fvset\dontdofcolorbox}{}{}

Greetings

1
  • Why re-adding the code when egreg already provided the same solution for the inline variant?
    – epR8GaYuh
    Feb 11, 2020 at 8:44
3

This usually works for julia code, where the @ would have a red box around it

\begin{minted}[escapeinside=||]{julia}
|@|. a .+ b
\end{minted}

If you use | in your code, you can change the escape character, e.g.

\begin{minted}[escapeinside=??]{julia}
?@?. a .+ b
\end{minted}
1
  • This was the only answer that worked for me without forcing me to change colour scheme, as it also prevents the text changing colour for these sorts of symbols which seems to happen in addition to the box.
    – Tom Wyllie
    Mar 1, 2021 at 14:45
1

I had a similar problem but couldn't disable error highlighting for all minted environments. And I came up with the following modified version of @egreg that worked for me.

\documentclass[12pt,a4paper]{article}

\usepackage{fontspec}
\setmainfont{XITS}
\setmonofont{Source Code Pro} % I don't have Consolas

\usepackage{minted}
\setminted[python]{
    linenos=true,
    breaklines=true,
    encoding=utf8,
    fontsize=\footnotesize,
    frame=lines
}
\usepackage{etoolbox}

\makeatletter
\AtBeginEnvironment{noerr}{\dontdofcolorbox}
\def\dontdofcolorbox{\renewcommand\fcolorbox[4][]{##4}}
\makeatother

\newenvironment{noerr}{}

\begin{document}

\section{Some code in this section}
\begin{noerr}
\begin{minted}{python}
def add(α, β):
    return α + β
\end{minted}
\end{noerr}

\end{document}

So instead of replacing fcolorbox in the minted environment, I create a new environment noerr, in which fcolorbox is replaced. This way I can use the minted environment in the noerr environment to suppress the highlight but also use it outside to get the highlighting.

I currently don't have a corresponding version for \mintinline or \inputminted.

2
  • I like this answer and hope that something similar is implemented in the greatly revamped version of minted which is being worked on.
    – hackerb9
    Sep 18 at 23:51
  • 1
    @hackerb9 I opend an Issue with a feature request on the minted github repo at <github.com/gpoore/minted/issues/374>
    – Skgland
    Sep 20 at 18:17

You must log in to answer this question.

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