12

So, I am writing a report with Prolog code listings in it. I have been using lstlisting to make my listings and I made my code centered on the page, such as this:

\renewcommand{\figurename}{Listing}
\begin{figure}[thp]
 \centering
 \begin{tabular}{c}
  \begin{lstlisting}[language=Prolog]
somePredicate(A, B) :-
    arbitraryPredicate(A),
    anotherPredicate(B).
  \end{lstlisting}
 \end{tabular}
 \caption{My Prolog Predicate}
 \label{lst:firstListing}
\end{figure}

Generated output:

First Listing

It works very well. This is based on the following answer on stackexchange: https://tex.stackexchange.com/a/5822/46424

However, I want to switch to the minted-package to represent my code snippets, as it shows the code with nice colors. This is where it goes wrong. I got minted to work, but I cannot get my listing centered anymore.

After searching on the web I found only one question about aligning minted code: LaTeX align minted code fragments

So, just as it shows there in the answers of that question, I tried to use a minipage. This, however, did not work as expected. The code is simply aligned left instead of centered if I write the following:

\renewcommand{\figurename}{Listing}
\begin{figure}[thp]
 \begin{minipage}[t]{\textwidth}
  \centering
  \begin{minted}{prolog}
somePredicate(A, B) :-
    arbitraryPredicate(A),
    anotherPredicate(B).
  \end{minted}
  \caption{My Prolog Predicate}
  \label{lst:firstListing}
 \end{minipage}
\end{figure}

Generated output:

enter image description here

I have also tried to combine tabular with minted, but then pdflatex gets an error and no pdf-file gets generated.

So, my question is: how do I make my code listings, that are represented by the minted-package, centered on my page?

Thanks in advance!

3
  • 1
    Are you sure you want to switch to minted? There may be a way of configuring listings to get the highlighting you want. Try, for instance, \lstdefinestyle{myPrologstyle} { language=Prolog, basicstyle = \ttfamily\color{blue}, moredelim = [s][\color{black}]{(}{)}, literate = {:-}{{\textcolor{black}{:-}}}2 {,}{{\textcolor{black}{,}}}1 {.}{{\textcolor{black}{.}}}1 }. I encourage you to post another question about how to customise listings to mimic the Pygments's Prolog lexer (used internally by minted).
    – jub0bs
    Commented Feb 18, 2014 at 19:38
  • @Jubobs If it can be done with listings instead of minted, then I am willing to stick to listings. Your suggestion is a good start for mimicking the Pygments's Prolog lexer, but there are still some differences.. for instance: the color blue is different, and you did not make the variables blue. Yes, maybe I will post another question then, but first I will see if egreg's and Herbert's answers work for me. Thanks for your comment though.
    – PJvG
    Commented Feb 19, 2014 at 10:06
  • @Jubobs As you encouraged me to do, I posted another question: tex.stackexchange.com/questions/161235/…
    – PJvG
    Commented Feb 19, 2014 at 11:20

3 Answers 3

16

If you want all minted environments to be centered and you don't need the Verbatim environment (provided by fancyvrb) for other purposes, then

\documentclass{article}
\usepackage{lipsum} % just for the example

\usepackage{minted}
\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}

\renewcommand{\figurename}{Listing}

\begin{document}

\lipsum[2]

\begin{figure}[htp]
\centering
\begin{minted}{prolog}
somePredicate(A, B) :-
    arbitraryPredicate(A),
    anotherPredicate(B).
\end{minted}

\caption{My Prolog Predicate}
\label{lst:firstListing}
\end{figure}
\end{document}

If, instead, you want to choose between centered and full width minted environments, define a cminted environment; the definition is a bit convoluted: we save a copy of \minted and modify it to issue the \RecustomVerbatimEnvironment command locally.

\documentclass{article}
\usepackage{lipsum}
\usepackage{minted}
\usepackage{xpatch,letltxmacro}
\LetLtxMacro{\cminted}{\minted}
\let\endcminted\endminted
\xpretocmd{\cminted}{\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}}{}{}

\renewcommand{\figurename}{Listing}

\begin{document}

\lipsum[2]

\begin{figure}[htp]
\centering
\begin{cminted}{prolog}
somePredicate(A, B) :-
    arbitraryPredicate(A),
    anotherPredicate(B).
\end{cminted}

\caption{My Prolog Predicate}
\label{lst:firstListing}
\end{figure}
\end{document}

enter image description here

8
  • Hey, thanks for your answer. I have not tried it, but I am sure it works perfectly. I have gone with Herbert's answer because it looks easier to use.. However, I am interested in why you would define a cminted environment instead of simply using a minipage. Can you elaborate on that?
    – PJvG
    Commented Feb 19, 2014 at 10:23
  • 1
    @PJvG Because you can't know how wide you have to make the minipage, can you?
    – egreg
    Commented Feb 19, 2014 at 10:24
  • 1
    @PJvG Try making both methods with longer lines and you'll see the difference.
    – egreg
    Commented Feb 19, 2014 at 10:30
  • Umm no I do not know that. However, I use the following settings in my report: \documentclass[a4paper,10pt]{report} \usepackage[top=70pt,bottom=70pt,left=65pt,right=65pt]{geometry}. My guess is that someone would know how wide the minipage needs to be, if he or she knows these settings. Is my assumption wrong here? Anyway, I can see now that your answer is more generic than Herbert's answer. As I think genericity is more important than simplicity, I am going to accept your answer now. Thanks.
    – PJvG
    Commented Feb 19, 2014 at 10:31
  • 1
    @PJvG It doesn't depend on the global text width, but on the width of the particular listing. With Herbert's method you need to guess the width of the listing; if the guess is wrong by defect, the listing will stick out on the right, if by excess it will stick out on the left. With the BVerbatim you get centering without any guess.
    – egreg
    Commented Feb 19, 2014 at 10:36
5

your minipage makes no sense. Use it this way:

\documentclass{article} 
\usepackage{minted}
\begin{document}

\begin{figure}[thp]
\centering 
\begin{minipage}{0.4\textwidth}
\begin{minted}{prolog}
somePredicate(A, B) :-
    arbitraryPredicate(A),
    anotherPredicate(B).
\end{minted}
\end{minipage}
\caption{My Prolog Predicate}\label{lst:firstListing}
\end{figure}

\end{document}

enter image description here

2
  • Hey thanks, it works. However, can you please elaborate why my minipage makes no sense? I do not have much experience with minipages, but I am willing to learn how they work. Also, I have accepted your answer for its simplicity.
    – PJvG
    Commented Feb 19, 2014 at 10:20
  • I have changed my mind on which answer to accept. See the comments on egreg's answer for the reason why.
    – PJvG
    Commented Feb 19, 2014 at 10:48
0

@egreg's answer is great and inspired what I'm doing now...

\newminted[ccodefig]{c}{fontsize=\footnotesize,linenos}
\xpretocmd{\ccodefig}{\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}}{}{}
\xpretocmd{\ccodefig*}{\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}}{}{}

The first line creates a new minted environment called ccodefig (I read this as "c code [for use in] figure[s]"), but you can name it anything. It also creates ccodefig* which allows option overrides over the defaults that you set for this command.

Then I just do what egreg suggested to change the underlying implementation from Verbatim to BVerbatim.

I like that this lets me set defaults for a given language at the same time as I create the new command. Then I just patch it up for horizontal centering.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .