Introducing section headings in minted

Henry B. yesterday at 08:56. 1 answers, 0 views
sectioning 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.

1 Answers


G. Poore 01/18/2013.

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}

Language

Popular Tags