6

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}
4
  • Welcome to TeX.sx!! Instead of posting a “Thank you” as an additional answer, you should thank [user] by upvoting [his/her] answer (with the upward pointing arrow to the left of it; you need 15 reputation points before you can upvote) and accepting it (by clicking on the checkmark). We want to keep the answer space reserved for actual answers, so this non-answer will be removed from public view soon. Commented Jun 17, 2012 at 9:46
  • You can use the option mathescape. Commented Jun 17, 2012 at 9:50
  • 2
    Please add a minimal working example (MWE) that illustrates your problem. Commented Jun 17, 2012 at 10:06
  • Pygments will use the option mathescape only for comments. pygments.org/docs/formatters Commented Jun 17, 2012 at 18:05

2 Answers 2

5

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.

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.

2
  • 1
    The package does nothing ;-) -- pygments will do the job. However the problem is linked in my comment above. Commented Jun 17, 2012 at 18:12
  • @MarcoDaniel If pseudocode is to be typeset, then this should be written explicitly.
    – egreg
    Commented Jun 17, 2012 at 18:50

You must log in to answer this question.

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