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 apologize if this has been answered elsewhere. I haven't been able to find it. I am using the minted package and I would like to have sections similar to

\section{Section Headings}

So I have two problems:

  1. How do I use the latex command \section in the middle of the minted environment

  2. I thought maybe I could just use \end{minted}\section{My Section}\begin{minted}[ ... ], however this introduces a new problem where my line numbers start counting from 1 again in the new section, which I don't want.

Any help or insight would be much appreciated.

share|improve this question
1  
You can specify the number of the first line of the listing with \begin[linenos,firstnumber=...,], although i'm affraid you need to manually insert the first line number yourself. –  guillem Jan 17 '13 at 15:41
add comment

1 Answer

up vote 6 down vote accepted

Without potentially painful tinkering with minted and/or fancyvrb internals, you will have to use the firstnumber option for the environment following the section, manually setting the starting line number, as @guillem said.

If you don't have to use minted, you could use my pythontex package (latest version on GitHub, on CTAN soon). It uses the same Pygments highlighting library that minted does and thus produces very similar output.

enter image description here

\documentclass{article} 

\usepackage[pygopt={texcomments=true}]{pythontex}
\setpygmentsfv[python]{numbers=left,firstnumber=last}


\begin{document}

\begin{pygments}{python}
def f(x):
    # Comment $x^2$
    return x**2
\end{pygments}

\section{A section}

\begin{pygments}{python}
def g(x):
    return x/2
\end{pygments}

\end{document}
share|improve this answer
    
Wow, pythontex seems quite cool. I just finished watching your talk on Youtube. Can I embed latex in my comments in pythontex like I can in mint. I like that I can do something like "/* x = \frac{y}{z] */" in my C++-code and have it nicely rendered in my tex version of my code. –  Henry B. Jan 17 '13 at 23:19
    
@HenryB. You can use Pygments' texcomments and mathescape features when loading the package. You can also set these for individual languages, if you don't want the settings to apply to everything. I've updated the example to show texcomments in use. –  G. Poore Jan 18 '13 at 0:12
add comment

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.