# [Tex/LaTex] How to make a minted code listing centered on a page

horizontal alignmentlistingsminted

So, I am writing a report with Prolog code listings in it. I have been using lstlisting to make my listings and I made my code centered on the page, such as this:

\renewcommand{\figurename}{Listing}
\begin{figure}[thp]
\centering
\begin{tabular}{c}
\begin{lstlisting}[language=Prolog]
somePredicate(A, B) :-
arbitraryPredicate(A),
anotherPredicate(B).
\end{lstlisting}
\end{tabular}
\caption{My Prolog Predicate}
\label{lst:firstListing}
\end{figure}


Generated output:

It works very well. This is based on the following answer on stackexchange: https://tex.stackexchange.com/a/5822/46424

However, I want to switch to the minted-package to represent my code snippets, as it shows the code with nice colors. This is where it goes wrong. I got minted to work, but I cannot get my listing centered anymore.

After searching on the web I found only one question about aligning minted code: LaTeX align minted code fragments

So, just as it shows there in the answers of that question, I tried to use a minipage. This, however, did not work as expected. The code is simply aligned left instead of centered if I write the following:

\renewcommand{\figurename}{Listing}
\begin{figure}[thp]
\begin{minipage}[t]{\textwidth}
\centering
\begin{minted}{prolog}
somePredicate(A, B) :-
arbitraryPredicate(A),
anotherPredicate(B).
\end{minted}
\caption{My Prolog Predicate}
\label{lst:firstListing}
\end{minipage}
\end{figure}


Generated output:

I have also tried to combine tabular with minted, but then pdflatex gets an error and no pdf-file gets generated.

So, my question is: how do I make my code listings, that are represented by the minted-package, centered on my 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}