Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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

enter image description here

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.

share|improve this question

3 Answers 3

up vote 11 down vote accepted

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}
share|improve this answer

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).

share|improve this answer

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.