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}
user34295
  • 305

3 Answers3

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

egreg
  • 1,121,712
  • 1
    This solution makes line numbers disappear. – vallentin May 30 '18 at 05: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 '19 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}
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

Lucas
  • 322