2

I have a book in which I'm using lstlisting for pseudocode. In some cases, I have strings, such as here:

\begin{lstlisting}
foreach (name in NameCollection)
{
    Print ``Hello, {name}!'' 
}
\end{lstlisting}

My problem is that no matter what I do, I end up with a lot of extra space inside the double quotes; they're angled correctly but take up as much space as two characters.

enter image description here

0
2

Two consecutive backticks `` and two consecutive typewriter apostrophes '' are TeX ligatures to produce English quotation marks and , respectively. In code listing, you should use a single symbol ", which is the most common way of introducing a string of text. I also strongly recommend to use a mono spaced font for code listing (they are designed for this purpose!).

\documentclass{article}
\usepackage{listings}
\lstset{
  basicstyle=\ttfamily
}
\begin{document}
\begin{lstlisting}
foreach (name in NameCollection)
{
    Print "Hello, {name}!"
}
\end{lstlisting}
\end{document}

code listing

3
  • Unfortunately I get the same results whether I use the two apostrophes or a quote mark. But it does look better with a monospace font.
    – William
    Nov 2 '18 at 1:59
  • @William Is it possible that, when you typed ", your TeX editor automatically converted the single symbol into two separate apostrophes? In that case, I suggest using the backspace or Ctrl+Z to get the single " back. Nov 2 '18 at 4:31
  • Huh, you're right - it did!
    – William
    Dec 12 '18 at 1:51

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.