[Tex/LaTex] How to typeset – – with lstlisting package

listingspunctuation

Consider this small example:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{listings} %for listings of the source code

\begin{document}

\lstset{language=sh}
\begin{lstlisting}
--   <-- Works!
a--b <-- Does not work!
-{}- <-- Won't help!
\end{lstlisting}

\end{document}

How to force lstlisting to typeset -- instead of a long dash always? Also, how to increase distance between two consecutive dashes?

Best Answer

You can use the option literate to modify the output:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{listings} %for listings of the source code

\begin{document}

\lstset{language=sh,literate={--}{{-{}-}}1}
\begin{lstlisting}
--   <-- Works!
a--b <-- Does not work!
-{}- <-- Won't help!
\end{lstlisting}

\end{document}

Furthermore you shouldn't use the option utf8x. An explanation can be found in the question utf8x vs. utf8 (inputenc)

Related Question