# Introducing section headings in minted

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.

-
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

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.

\documentclass{article}

\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}

-
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