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:

    \caption{Example programming styles}
  • 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...
    – user34295
    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.
    – jub0bs
    May 22, 2013 at 19:47

3 Answers 3


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:


\caption{Example programming styles}

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

  • 1
    This solution makes line numbers disappear.
    – vallentin
    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.
    – Lucas
    Jul 27, 2019 at 23:19

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



  • It would be good to make this answer complete (compilable) in the context of the OP's question.
    – A Feldman
    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).

\begin{minipage}[t]{.7\textwidth}% !!! you have to estimate this value !!!
\caption{Guessing the line length}
% If on the other hand you use \begin{minted}...\end{minted} it is easy to
% measure the longest line with \widthof{...}:
\begin{minipage}[t]{\widthof{\texttt{\scriptsize if you have all the lines in your tex file}}}
\widthof{\texttt{the longest line}}
instead of
if you have all the lines in your tex file
\caption{Measuring the line length}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.