1

My problem is that when I use the correct quotation marks ``'', lstset does not recognize strings in my code and thus does not color-code them. When I use standard double quotes "", the leading quotes are backwards. Thoughts?

\documentclass[12pt, letterpaper]{article}
\usepackage [english]{babel}
\usepackage [autostyle, english = american]{csquotes}
\MakeOuterQuote{"}

 \usepackage{listings}
 \newcommand{\dbllq}{``}
 \newcommand{\dblrq}{''}
 \lstset{
    literate={``}{\dbllq}1 {''}{\dblrq}1  % so that double quotes appear correctly in the code
 }

 \usepackage{color}
 \definecolor{mygreen}{rgb}{0,0.6,0}
 \definecolor{mygray}{rgb}{0.5,0.5,0.5}
 \definecolor{mymauve}{rgb}{0.58,0,0.82}
 \lstset{ %
    basicstyle=\normalsize,        % the size of the fonts that are used for the code
    commentstyle=\color{mygreen},    % comment style
    keywordstyle=\color{blue},       % keyword style
    language=C,                     % the language of the code
    stringstyle=\color{mymauve},     % string literal style
    showspaces=false, 
 }

\begin{document}
    "Quotes in the document work properly."

\begin{lstlisting}
    displayTextLine(1, "Right color, wrong quotes"); // comments are green
    for(1, ``Right quotes, wrong color''); // nonsensical code but keywords are blue
\end{lstlisting}

\end{document}
2

For your first example using wrong quotes, you can correct them by making all your quotes neutral (non-curly). See How can I get straight double quotes in listings? Alternatively, you can simply use typewriter font in your basic or even just your string style. I usually do this anyway for listings:

basicstyle=\normalsize\ttfamily, 

When using the correct LaTeX quotes, you need to tell listings to recognize strings using these delimiters, so add

morestring=[s]{``}{''},

to your lstset command.

  • The morestring command did not work when I tried it, but using typewriter font worked as I wanted. – Margaux Apr 12 '15 at 20:27
  • 1
    Curious to know why morestring didn't work. Did it compile? If not, you may have an older version of listings. Otherwise, I should have noted that you have to remove your literate option, since this redefines the delimiters that we are choosing with the morestring option. – alaferg Apr 14 '15 at 20:57
  • I did remove the literate option. When I used morestring, the quotes faced the right way and the text was the correct color, but the double quotation marks were oddly spaced as if they were two separate characters instead of one. It was a bit weird-looking. Not sure why that happened. – Margaux Apr 15 '15 at 16:42
  • Hmmm, you're right. That is odd... – alaferg Apr 15 '15 at 21:52

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.