5

How can I get minted to escape LaTeX code inside docstrings? Should I use some switch?

\documentclass[a4paper,12pt]{article} 
\usepackage[italian]{babel}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{minted}
\renewcommand{\theFancyVerbLine}{\sffamily
  \textcolor[rgb]{0.5,0.5,1.0}{\scriptsize
  \oldstylenums{\arabic{FancyVerbLine}}}}

\title{General Title}
\author{Infrid}

\begin{document}
\maketitle 

\begin{minted}[mathescape,linenos=true]{python}
def naive(a,x):
    """
    lorem ipsum....

    this code will not be escaped
    $ a = \{a_0, a_1, a_2, a_3, \dots, a_n\} $

    """
    # this code will be escaped
    p = a[0] # $ p = \frac{1}{3} $
    y = x
    for ai in a[1:]:
        p = p + ai*y
        y = y*x

    return p
\end{minted}

\end{document}
Caramdir
  • 89,023
  • 26
  • 255
  • 291
Infrid
  • 153

2 Answers2

4

As others have pointed out, this is because minted only activates mathescape inside comments.

FWIW, the same is true for the t-vim module in ConTeXt. It is similar to the minted package for LaTeX, but uses vim instead of pygments for syntax highlighting.

t-vim provides an option to load an arbitrary vim file before the source code is parsed. So, it is possible to change the parser on the fly. For example, to identify docstrings as comments, you can use the vim file given in this thread in the vim mailing list.

\usemodule[vim]

\startvimrc[name=python-docstring]
syn match  pythonBlock      ":$" nextgroup=pythonDocString skipempty skipwhite
syn region pythonDocString  matchgroup=Normal start=+[uU]\='+ end=+'+ skip=+\\\\\|\\'+ contains=pythonEscape,@Spell contained
syn region pythonDocString  matchgroup=Normal start=+[uU]\="+ end=+"+ skip=+\\\\\|\\"+ contains=pythonEscape,@Spell contained
syn region pythonDocString  matchgroup=Normal start=+[uU]\="""+ end=+"""+ contains=pythonEscape,@Spell contained
syn region pythonDocString  matchgroup=Normal start=+[uU]\='''+ end=+'''+ contains=pythonEscape,@Spell contained
hi def link pythonDocString Comment
\stopvimrc

\definevimtyping[PYTHON][syntax=python, extras=python-docstring]

\starttext

\startPYTHON[escape=on]
def naive(a,x):
    """
    lorem ipsum....

    this code will be escaped (note no spaces)
    \math{a=\{a_0,a_1,a_2,a_3,\dots,a_n\}}

    """
    # this code will be escaped
    p = a[0] # \math{p=\sqrt{\frac{1}{3}}}
    y = x
    for ai in a[1:]:
        p = p + ai*y
        y = y*x

    return p
\stopPYTHON
\stoptext

which gives:

enter image description here

One difference in t-vim is that you need to use \math{...} (or \m{...}) to enable math mode rather than $...$. As with minted and listings do not use spaces in math mode.

To do something similar in minted, you will need to change the python parser so that it identifies docstrings as comments.

Aditya
  • 62,301
3

From the manual of minted:

mathescape (boolean) (default: false)
Enable LaTeX math mode inside comments. Do not use spaces inside math mode — they will be rendered like other full-width verbatim spaces. Usage as in package listings.

The package aims to typeset code, so code must be. I don't think that

$ a = \{a_0, a_1, a_2, a_3, \dots, a_n\} $

is legal Python code.

egreg
  • 1,121,712