I am interested to know how listings
can be customized to mimic the Pygments's Prolog lexer (i.e. make Prolog code represented by listings
look the same as it is done with minted
)
I previously asked this question: How to make a minted code listing centered on a page?
Jubobs commented on that question and encouraged me to post this question.
My previous question was answered by egreg, who provided the following example, and this is exactly how I want my code listing to look like in my report:
However, I need a lot of extra packages for this solution. So I am wondering if the same (or very similar) result can be achieved by customizing listings
.
Jubobs already provided the following lstdefinestyle
example:
\lstdefinestyle{myPrologstyle}
{
language=Prolog,
basicstyle = \ttfamily\color{blue},
moredelim = [s][\color{black}]{(}{)},
literate =
{:-}{{\textcolor{black}{:-}}}2
{,}{{\textcolor{black}{,}}}1
{.}{{\textcolor{black}{.}}}1
}
This however is not sufficient enough for me. For example, the variables are not highlighted in the dark blue color as it is done in minted
.
Here is another (more complete) example of how Prolog code is highlighted with minted
:
\begin{minted}{prolog}
somePredicate(A, B) :-
arbitraryPredicate(A, _, 1, 2),
predicateWithAtom(someAtom),
anotherPredicate(B, someAtom, myPredicate(A, _)),
findall(X, ('testString'(X), myPredicate(A, X)), L1),
member(A, L1),
!.
\end{minted}
The snippet above produces the following output:
As can be seen from this example, atoms are highlighted red, anonymous variables (underscores) are highlighted green, and numbers are grey (or is it cyan?).
Does anyone know how I can customize listings
the way I want? Keep in mind that I want my listing to be centered on the page. (See my other question if you want to know how I make my listing centered.)
Best Answer
Months later, after much contorsion, I've managed to put a viable (even if ugly) solution together... thereby making a liar out of myself
-_-'
Automatic recognition of Prolog predicates, atoms and variables
The solution shown below automatically distinguishes (and highlights in a style very similar to Pygments') Prolog predicates, atoms and variables. No need to manually enter long lists of keywords! In a nutshell, the approach consists in the following:
listings
treat the opening-parenthesis character as a "letter", in order to test whether that character occurs at the end of identifiers; if it does, then the identifier in question is a predicate.[A-Z_]
; if the test passes, the identifier is a variable.For a comparison, see the screenshot below.
Improvement over Pygments' Prolog lexer
I noticed that Pygments indiscriminately highlights underscores in dark green, and highlights whatever word immediately follows that character as an atom... That doesn't make sense to me, because, according to the Wikipedia page on Prolog syntax, an identifier starting by an underscore is a variable, not an atom.
The approach shown below improves upon the Pygments Prolog lexer insofar as it highlights underscores differently in different contexts:
Correct me if I'm wrong, here; I'm no Prolog expert.
Missing feature
I didn't bother replicating the highlighting style for numbers; as other questions on the subject attest, that is notoriously difficult, so I'll pass on that, at least for now.
Code