5

I'm using lstlisting to display some ProLog code, in this code I need to add 5 dashes after another: -----, but they keep combining into one line, which is not what I want.

I tried using \lstinline and \verb, but it seems like these aren't allowed in lstlisting, -{}-{}-{}-{}-{} won't work either as it'll just display {} too.

What is the correct way to do this in lstlisting?

I currently have something like this:

\documentclass[11pt,a4paper]{report}

\usepackage[a4paper,left=3.5cm, right=2.5cm, top=3.5cm, bottom=3.5cm]{geometry}
\usepackage[dutch]{babel}
\usepackage{graphicx}
\usepackage[latin1]{inputenc}
\usepackage{listings}
\usepackage{verbatim}                   % weergeven van code, commando's, ...
\usepackage{hyperref}                   % maak PDF van de thesis navigeerbaar
\usepackage{url}                        % URL's invoegen in tekst met behulp van \url{http://}
\usepackage[small,bf,hang]{caption}     % om de captions wat te verbeteren
\usepackage[final]{pdfpages}            % gebruikt voor het invoegen van het artikel in pdf-formaat
\usepackage{pslatex}
\usepackage{sectsty}
\usepackage{float} 
\usepackage{amsmath}

\usepackage{mdwlist}
\usepackage{paralist}

\begin{document}
\selectlanguage{dutch}

\definecolor{dkgreen}{rgb}{0,0.5,0}
\lstset{language=Prolog, commentstyle=\color{dkgreen}}
\begin{lstlisting}[frame=LR]
% Bunch of prolog code that displays fine
evidence(burglary,false).
evidence(alarm,false).
-----
evidence(earthquake,false).
evidence(alarm,true).
evidence(burglary,true).
-----
evidence(burglary,false).
\end{lstlisting}

\end{document}

The dashes don't display as above though.

6
  • (1) Welcome, (2) on this site we like to get fully compilable example we can test, just code sniplets like this. Do does not know for example which packages you use
    – daleif
    Dec 9, 2015 at 11:36
  • (1) Thank you, (2) I edited to code, this is just a part of a pretty big document though, so should I really show you all packages used or...? Dec 9, 2015 at 11:49
  • Yes, do post a compilable minimal example. With \usepackage{babel} (or preferably without \selectlanguage, if you still have the problem without it) and whatever else you need to compile the code. Dec 9, 2015 at 12:15
  • 1
    @CounterFlame your example should of course be compilable, a first order approximation probably does not need to set a language, we also do not have the color definition for that color.
    – daleif
    Dec 9, 2015 at 12:28
  • I added about all the packages I use now, as well as the color definition, although that one doesn't really matter I think. Dec 9, 2015 at 12:38

1 Answer 1

6

One trick to prevent TeX from "joining" consecutive hyphens is to (ab)use the literate option by replacing each - character by... itself!

enter image description here

\documentclass{report}

\usepackage{listings}
\lstset{
  language = Prolog,
  literate = {-}{-}1, % <------ trick!
}

\begin{document}

\begin{lstlisting}
% Bunch of prolog code that displays fine
evidence(burglary,false).
evidence(alarm,false).
-----
evidence(earthquake,false).
evidence(alarm,true).
evidence(burglary,true).
-----
evidence(burglary,false).
\end{lstlisting}

\end{document}
3
  • @CounterFlame your MWE compiles ok for me, the dashes are very close together but still separated. Maybe it's a pdf viewer issue? Apart from @Jubobs 's answer you could also put a space between the dashes, as in - - - - -. Succes met je document!
    – Marijn
    Dec 9, 2015 at 17:27
  • I get distinct dashes without this trick (applying ligatures would also in a way defeat a verbatim environment like lstlisting in the first place...
    – cgnieder
    Dec 9, 2015 at 17:57
  • This indeed works yes, thanks a lot! @Marijn They are indeed still seperated, but when printed out on paper it's just about impossible to see, which is definitely not what I wanted. En bedankt! :) Dec 9, 2015 at 18:05

You must log in to answer this question.

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