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

Kim Petersen
  • 415
  • 4
  • 7
  • Can you please add a minimal example of code? – egreg Dec 11 '16 at 16:12
  • 1
    You get this behaviour, too, if you use German umlauts (ä, ö, ü). Is there a "real" solution yet? – elementzero23 May 07 '18 at 15:13
  • 1
    This seems to be a feature of the underlying Pygments library (cf http://pygments.org/demo/6790720/), so you would probably need to open an issue at https://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 07 '19 at 07:57

5 Answers5

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

egreg
  • 1,121,712
  • 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. – Kim Petersen Dec 11 '16 at 17:21
  • 3
    @KimPetersen Patch for \inputminted added. I used filecontents just to make the example self-contained. – egreg Dec 11 '16 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 '17 at 18:15
  • 1
    @IvoLooser The code simply disables \fcolorbox inside minted snippets. – egreg Jan 29 '17 at 18:53
  • It would be nice to have this also include the \mintinline patch from https://tex.stackexchange.com/a/401250/2066: \xpatchcmd{\mintinline}{\minted@fvset}{\minted@fvset\dontdofcolorbox}{}{} – Jason Gross Mar 30 '20 at 17:43
  • @JasonGross Yes, it would be good, thanks for noting and for pointing to the patch. – egreg Mar 30 '20 at 20:24
  • Some stylings like perldoc seem to use \colorbox to mark errors. By using \renewcommand{\colorbox}[3][]{#3} you can override those colorboxes. – eco Jul 16 '20 at 10:29
  • I rejected an edit suggesting \makeatletter{} and \makeatother{} to keep ChkTeX quiet. Sorry: if ChkTeX can't cope with standard constructions, it's its fault and I'm not adding useless, misleading and wrong braces in order to keep it happy. – egreg Jul 04 '22 at 09:46
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}

AntoineKa
  • 151
  • 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 '23 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

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}
  • 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 01 '21 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.

Skgland
  • 111