[Tex/LaTex] Centering \inputminted inside a figure

floatshorizontal alignmentminted

I'm using minted package to import some code and also kind of images using ASCII art. I've wrapped the \inputminted with \figure with a caption and all works fine.

The only thing I can't accomplish is centering the ASCII art figure. I've tried:

\begin{figure}[ht]
    \begin{center}
        \inputminted[fontsize=\scriptsize]{text}{./samples/styles.txt}
    \end{center}
    \caption{Example programming styles}
\end{figure}

Best Answer

Pygmentize outputs a file that contains \begin{Verbatim} and \end{Verbatim} and changing this is out of the question. But we can exploit fancyvrb's features:

\documentclass{article}
\usepackage{minted}

\begin{document}
\begin{figure}[htp]
\centering
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}
\inputminted[fontsize=\scriptsize]{latex}{./inpmint.tex}
\caption{Example programming styles}
\end{figure}
\end{document}

I have used the LaTeX file itself for the example: the Verbatim environment is “recustomed” to use BVerbatim instead. Doing this in the figure environment ensures the environment will revert to its previous meaning after \end{figure}.

enter image description here

Related Question