Minted – How to Use a $ Sign in Minted

minted

I have code like this:

\documentclass[11pt]{report}
\usepackage{minted}
\usepackage{listings}

\begin{document}

\begin{listing}[ht]
    \begin{minted}{csharp}
    private static string Abc() =>
                \$"Abc";
    \end{minted}
\caption{Abc}
\label{listing:Abc}
\end{listing}

\end{document}

and i have frame like this (compiled on overleaf):

enter image description here

With Only "$":

enter image description here

How can I get rid of the red frame?

Best Answer

Firstly, you don't need to escape the $.

This is a pygments bug relating to its C# lexer, see https://github.com/pygments/pygments/issues/1573, which has been fixed in pygments 2.10.0, released on August 15th, 2021.

Jun 12, 2022 updates:

Unfortunately, Overleaf.com's texlive 2021 still provides pygments v2.9.0. You can check by compiling the example below on overleaf:

\documentclass{article}
\usepackage{bashful}
\begin{document}
\bash[stdout]
pygmentize -V
\END
\end{document}

How I find the above info

  • The red frame in pdf output is the style of pygments parsing error, indicating it's a pygments issue.
  • Then I tested OP's minted snippet both locally and on https://pygments.org/demo/#try, and each of them works ok (with non-escaped $). I also checked $"string" is a valid C# syntax. All that suggest it's a fixed pygments issue.
  • So I directly jump to pygments' changelog and search for "c#/csharp". This gives
    • Updated lexers:

      • [...]
      • C# (#1573, #1869)
  • I checked #1573 and found it's exactly the same issue OP encountered.
  • The rest is straight: checking that issue was fixed and the fixing was published, getting the minimal version containing the fixing (double checked with the sectioning lines in CHANGE).