The problem is that a listing
is a float, like table
or figure
, and floats aren't allowed inside the multicol
environment. If you're happy to place the listing where you want it by hand, you can achieve the effect you're after by removing the listing
environment, using the caption
package, and changing \caption
to \captionof
:
\documentclass{article}
\usepackage{minted, caption}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
This is a listing inside a multicolumn:
\begin{minted}{sql}
SELECT * FROM table1
\end{minted}
\captionof{listing}{Some random SQL Query}
\label{lst:sql}
\end{multicols}
\end{document}
Alternatively, if you need the float facility, another option is to use the listing*
environment instead. This, however, will set the listing across the whole page rather than in a single column, and also means that it will never appear on the page being typeset when the command is read (always on some subsequent page).
If you want all minted
environments to be centered and you don't need the Verbatim
environment (provided by fancyvrb
) for other purposes, then
\documentclass{article}
\usepackage{lipsum} % just for the example
\usepackage{minted}
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}
\renewcommand{\figurename}{Listing}
\begin{document}
\lipsum[2]
\begin{figure}[htp]
\centering
\begin{minted}{prolog}
somePredicate(A, B) :-
arbitraryPredicate(A),
anotherPredicate(B).
\end{minted}
\caption{My Prolog Predicate}
\label{lst:firstListing}
\end{figure}
\end{document}
If, instead, you want to choose between centered and full width minted
environments, define a cminted
environment; the definition is a bit convoluted: we save a copy of \minted
and modify it to issue the \RecustomVerbatimEnvironment
command locally.
\documentclass{article}
\usepackage{lipsum}
\usepackage{minted}
\usepackage{xpatch,letltxmacro}
\LetLtxMacro{\cminted}{\minted}
\let\endcminted\endminted
\xpretocmd{\cminted}{\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}}{}{}
\renewcommand{\figurename}{Listing}
\begin{document}
\lipsum[2]
\begin{figure}[htp]
\centering
\begin{cminted}{prolog}
somePredicate(A, B) :-
arbitraryPredicate(A),
anotherPredicate(B).
\end{cminted}
\caption{My Prolog Predicate}
\label{lst:firstListing}
\end{figure}
\end{document}
Best Answer
The following code creates a new environment,
mintednc
, that strips comments completely. You can add it to the preamble. I've tested it with both C and Python. It's possible that it will need a few tweaks to deal with additional languages.This code works by creating a custom version of
\minted@pygmentize
. In this customized version, the highlighted Pygments output is opened via a\write18
Python command, and any line beginning with\PY{c}
,\PY{c+cm}
, or\PY{c+c1}
is discarded. These are the Pygments highlighting commands with arguments that indicate comments. This approach completely eliminates any line that begins with a comment. But inline comments that follow code would be left intact.Edit in response to question in comments: Adding support for
\inputminted
is just a matter of creating a wrapper command (\inputmintednc
) that uses the customized\minted@pygmentize
. I've added this at the end of the code.Edit in response to error messages in comments: It looks like you are using the development version of
minted
. The patch only works forminted
1.7. I've added code that works (for now, anyway) with the development version.Code for
minted 1.7
(which is in current TeX distros)Code for current development version of
minted