5

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 '13 at 17:01
  • @Rico thanks, but even \centering is not working... – user34295 May 22 '13 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 '13 at 19:47
6

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

  • 1
    This solution makes line numbers disappear. – Vallentin May 30 '18 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 at 23:19
0

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

\usepackage{changepage}

\begin{adjustwidth}{.3\textwidth}{}

\end{adjustwidth}
  • It would be good to make this answer complete (compilable) in the context of the OP's question. – A Feldman Apr 24 '16 at 17:06
0

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}}
instead of
.7\textwidth
if you have all the lines in your tex file
\end{minted}
\end{minipage}
\caption{Measuring the line length}
\end{listing}
\end{document}

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.