8

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!

  • 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 Feb 18 '14 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 Feb 19 '14 at 10:06
  • @Jubobs As you encouraged me to do, I posted another question: tex.stackexchange.com/questions/161235/… – PJvG Feb 19 '14 at 11:20
8

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

  • 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 Feb 19 '14 at 10:23
  • 1
    @PJvG Because you can't know how wide you have to make the minipage, can you? – egreg Feb 19 '14 at 10:24
  • 1
    @PJvG Try making both methods with longer lines and you'll see the difference. – egreg Feb 19 '14 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 Feb 19 '14 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 Feb 19 '14 at 10:36
3

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

  • 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 Feb 19 '14 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 Feb 19 '14 at 10:48

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.