13

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?

12

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)

  • Since the question also asks for "how to increase distance between two consecutive dashes?", the distance \, between -\,- as it was used before the last edit is correct. – dexteritas Sep 6 '18 at 15:04
9

use

\lstset{language=sh,basicstyle=\ttfamily}

and if you want to use bold chracters than use a font like Bera Mono or Luxi Mono!

\documentclass[10pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[scaled=0.83]{beramono}
\usepackage{listings} %for listings of the source code
\lstset{basicstyle=\ttfamily}
\begin{document}

\lstset{language=sh}
\begin{lstlisting}
--    # Works!
a--b  # Works
if else fi # Keywords work too! 
\end{lstlisting}

\end{document}
  • You are right, but CM typewriter does not have bold or italic variants - see my answer – Boris Dec 12 '11 at 18:45
  • sure and Courier is a "really not nice looking font ..." – user2478 Dec 12 '11 at 20:08
  • Agreed, Courier is not nice. Bera Mono is better - especially for presentation slides, where it can be combined with Bera Sans for body text and Arev for math - my favorite sans combinations – Boris Dec 12 '11 at 21:47
  • txfonts has a very nice typewriter font with italic, bold, and bold italic. (see also newtxtt) – user4686 Sep 6 '18 at 18:42
3

First, if you use long dashes for options, then this works:

a --b

Second, the choice of propotional fonts for listings is ugly. Unfortunately, CM typewriter does not have bold or italic variants, so just setting \lstset{basicstyle=\ttfamily} is not what you want. I prefer courier, which gives more satisfactory results:

\documentclass[10pt]{standalone}
\usepackage[utf8x]{inputenc}
\usepackage{courier}
\usepackage{listings} %for listings of the source code
\lstset{basicstyle=\ttfamily}
\begin{document}

\lstset{language=sh}
\begin{lstlisting}
--    # Works!
a--b  # Works
if else fi # Keywords work too! 
\end{lstlisting}

\end{document}

enter image description here

  • txfonts provides very nice typewriter font. (nowadays newtxtt may be preferred) – user4686 Sep 6 '18 at 18:43

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.