# Centering \inputminted inside a figure?

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}

• using \begin{center} within figures is deprecated the normal way would be to use \centering
– Rico
May 22, 2013 at 17:01
• @Rico thanks, but even \centering is not working... May 22, 2013 at 17:03
• @Rico deprecated is not the right word. I would rather say that using a center environment within a figure is ill-advised. See this. May 22, 2013 at 19:47

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}.

• This solution makes line numbers disappear. May 30, 2018 at 5:13
• This solution does not only ignore the line numbers but also the autogobble option. Maybe even more minted options. I only tried these. The minted documentation also says (page 74): "Getting the inline command to work correctly require redefining Verbatim to be BVerbatim temporarily. This approach would break if BVerbatim were ever redefined elsewhere." Not sure if that is related. Jul 27, 2019 at 23:19

The other alternative would be to use the changepage package and adjustwidth environment,

\usepackage{changepage}


• It would be good to make this answer complete (compilable) in the context of the OP's question. Apr 24, 2016 at 17:06

This solution uses a minipage of known (estimated) size to wrap the minted. And I give the example with the listing environment of the minted package but it works equally well with figures.

Pro: Can display line numbers from minted (unlike @egreg's solution)

Con: You have to know/guess/estimate the size of the longest line (easy for text that you put inside a \begin{minted}...\end{minted} environment directly, see below).

\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{listing}[H]
\centering
\begin{minipage}[t]{.7\textwidth}% !!! you have to estimate this value !!!
\inputminted[fontsize=\scriptsize,linenos,
firstline=4,lastline=11]{latex}{./test.tex}
\end{minipage}
\caption{Guessing the line length}
\end{listing}
% If on the other hand you use \begin{minted}...\end{minted} it is easy to
% measure the longest line with \widthof{...}:
\begin{listing}[H]
\centering
\begin{minipage}[t]{\widthof{\texttt{\scriptsize if you have all the lines in your tex file}}}
\begin{minted}[linenos,fontsize=\scriptsize]{latex}
use
\widthof{\texttt{the longest line}}