# Converting latex highlighted code listing to image

Does someone know how I could submit a paper to a journal which contains source code highlighting with minted https://github.com/gpoore/minted ? I really like the highlighting and don't want to go back to lstlisting, however I somehow doubt the journal editors will have the latest Tex Live installation as well as pygments installed in their python installation.

Is there a way maybe to convert the minted code listings to svgs and put these in their stead in the latex document?

• Copy your minted setup and the actual code to a new document with class standalone and you get a pdf file. – Johannes_B Oct 22 '15 at 11:08
• Good idea. I tried it with standalone (which requires by the way the varwidth option to compile) but it's cutting off my line numbering and the border lines – Stefan Oct 22 '15 at 11:30
• try border=1cm as global option. I don't know how minted typesets stuff, and there isn't anything to test, so this is all guessing. – Johannes_B Oct 22 '15 at 11:37
• Hey, nice, the border fixed it. Now I just need to crop the pdf and I can insert it as a figure in my main document. Thanks! – Stefan Oct 22 '15 at 12:04
• I really like the highlighting and don't want to go back to lstlisting [...] Why not? It's usually possible, with marginal effort, to set up listings to highlight code like minted would. IMO, that would be preferable to having listings as images. – jubobs Oct 22 '15 at 14:34

As per @Johannes_B suggestion, I created a separate tex file for each code listing which I then compile to pdf and use as figures in my original document. I used document class standalone to create a separate pdf file for each code listing.

\documentclass[varwidth=6cm, border={0.5cm 0.1cm 0.1cm 0.1cm}]{standalone}
\usepackage{minted}
\setminted[python]{breaklines, linenos, frame=lines, framesep=2mm, fontsize=\footnotesize, numbersep=5pt}
\begin{document}
\begin{minted}{python}
cool = python.code()
\end{minted}
\end{document}


Being in the same situation and having many code listings, I didn't want to make a separate tex file for each listing. I found that by declaring the minted environment as a "standalone environment", on line 4 below, you can have multiple listings that get separate pages in the produced pdf:

\documentclass[varwidth=6cm, border={0.5cm 0.1cm 0.1cm 0.1cm}]{standalone}
\usepackage{minted}
\setminted[python]{breaklines, linenos, frame=lines, framesep=2mm, fontsize=\footnotesize, numbersep=5pt}
\standaloneenv{minted}
\begin{document}

\begin{minted}{python}
cool = python.code()
\end{minted}

\begin{minted}{python}
more_cool = python.code()
\end{minted}

% Many more minted blocks go here

\end{document}


Using the nice pdftk program we can split this into one pdf per page:

\$ pdftk document-name.pdf burst


which gives us pdf files named pg_NNNN.pdf with NNNN ranging from 0001 and up. Each page is cropped on the left, top and bottom correctly, but they have some whitespace on the right such that all of them have the width specified in the class option. By setting varwidth to be the desired line width in the paper, this works very well with includegraphics.

To get floats named "Listing 1" etc. in the other document (where these pdfs are included now as graphics), one can put the following in the preamble:

\usepackage{float}
\floatstyle{ruled}
\newfloat{listing}{thp}{lop}
\floatname{listing}{Listing}


and then in the document body

\begin{listing}[htbp]
\includegraphics[width=\linewidth]{pg_0001.pdf}
\caption{Showing some code.}
\label{lst:somecode}
\end{listing}
See the code in \cref{lst:somecode} for an example.