# Using minted with mixed Python code and output

I'm using minted to highlight code in Python. In some cases I want to mix code with output that the user will see. This gives me the red syntax error boxes, for example like so:

And sometimes it gives me no error but I would like to turn off the highlighting (second line):

So what I would like to know if it's possible to turn off the highlighting for a few lines but at the same time keeping the options such as the line numbering and background.

MWE:

\documentclass{article}

\usepackage{minted}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\newminted{python3}{bgcolor=bg, linenos=true, tabsize=4}

\begin{document}

I want lines 2 and 4 not to be highlighted but still keep the background and line numbering.

\begin{python3code}
>>> type(3.14)
<class 'float'>
>>> print(name)
NameError: name 'name' is not defined
\end{python3code}

\end{document}


Edit: To clarify: There are lines that I want highlighted that start with >>> and there are lines I want highlighted which don't start with >>>. There are also lines that I don't want highlighting for. So the default should be that it highlights the line but that I can add an exception to some lines.

-

The short and the long of this is: write a special lexer. But you’re in luck, it already exists, just use pycon as the language.

\newminted{pycon}{bgcolor=bg, linenos=true, tabsize=4}

-
When I use the pycon lexer all I get is grey text. Any idea why this would be? –  moorepants Feb 13 at 19:43

A very simple solution to your problem is to pre-process the code by adding a # before each line that does not start with a >>>, which will at least look OK even though it won't be a perfect copy of the python console output. This can be done with a lot of editors by using the regex option or by using other tools (sed for example) on Unix.

The leading >>> might still throw the Python lexer off though. In which case you could delete those (you loose more fidelity to the original but it'll be readable since the output will look like comments).

-

From a quick look at minted's documentation, it doesn't seem the package supports that feature. Either way specifying particular line numbers seems like a really ugly way to go at it.

But it uses pygmentize as a back-end and the lines you want to not ignore fit a very specific pattern, they all start with >>>, and it must therefore be easy to detect and ignore them programmatically.

Looking at the pygments documentation about writing your own lexer, it seems you can specify that for some patterns (regex) you want to let another lexer handle the string. You could then handle the lines that do not start with >>> and pass those that do (without the >>>) to the regular Python lexer.

-
They don't all start with >>>, for example I don't want the line with the NameError in the example to be highlighted. –  pg-robban Dec 2 '11 at 14:19
Don't you want the lines that start with >>>, that is the lines you write, to be highlighted and the lines Python outputs back at you to be ignored? –  iKs Dec 2 '11 at 14:22
The book I'm writing starts off with teaching the basics of Python using the interactive mode and then moving on to scripts, so in those cases there won't be any >>> in front of my code. –  pg-robban Dec 2 '11 at 14:26
But the output won't appear at the same location either. It'll probably make more sense to then include the output in a separate (verbatim) environment. No? Either way you need a way, described in English first, to describe what constitutes and what doesn't constitute a valid line. From there regexes are likely to do the trick programmatically. –  iKs Dec 2 '11 at 16:22
I don't think I can say anything general about them, since they are linked with the code which will sometimes be in the interactive mode and sometimes in the script mode. I guess what I can do is to use different environments (python3code and textcode) to make the alternating lines for now. –  pg-robban Dec 2 '11 at 16:55