# Syntax highlighting code fragments

I am trying to split a long code listing into two columns, on a Beamer slide, using Minted. However, because the second column isn't syntactically valid (i.e., without the lead in from the first), Pygments is highlighting the errors.

\begin{frame}[fragile=singleslide]
\begin{columns}[T]
\begin{column}{0.48\textwidth}
\begin{minted}{json}
{
"this": "is valid JSON",
\end{minted}
\end{column}
\hfill
\begin{column}{0.48\textwidth}
\begin{minted}{json}
"but": "this is not!"
}
\end{minted}
\end{column}
\end{columns}
\end{frame}


Is there any way to avoid this, or to let Minted know that the second part is the continuation of the first?

The easiest way to deal with this would probably be to put the code in an external file (or a temp file, as in the example below) and then input the different segments at the appropriate points.

\documentclass{beamer}
\usepackage{minted}
\begin{document}

\begin{VerbatimOut}{minted.tmp}
{
"this": "is valid JSON",
"but": "this is not!"
}
\end{VerbatimOut}

\begin{frame}[fragile=singleslide]
\begin{columns}[T]
\begin{column}{0.48\textwidth}
\inputminted[firstline=1,lastline=2]{json}{minted.tmp}
\end{column}
\hfill
\begin{column}{0.48\textwidth}
\inputminted[firstline=3,lastline=4]{json}{minted.tmp}
\end{column}
\end{columns}
\end{frame}

\end{document}

• Excellent, thank you :) As a follow-up, could this technique improve compilation times when you've got a lot of independent code snippets? i.e., Group them altogether (making sure the syntax is fine, so some tweaks may be necessary) and then \inputminted them, modulo the lines you are interested in? (Kind of like how CSS sprites work.) Jun 26 '15 at 8:21
• @Xophmeister The best thing you can do for compilation times (if you haven't already) is to use the last release of minted (2.0), which caches output and thus removes most of the package overhead on later compiles. Using a single file for lots of snippets would save some caching checksum calculations, which might be noticeable under XeTeX. But using a single longer file also means that the entire thing would be read in each time, with all of the irrelevant lines thrown away. And it would prevent effective caching; any change would cause everything to be highlighted again. Jun 26 '15 at 12:14