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}

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

 \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

    "Quotes in the document work properly."

    displayTextLine(1, "Right color, wrong quotes"); // comments are green
    for(1, ``Right quotes, wrong color''); // nonsensical code but keywords are blue


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:


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


to your lstset command.

| improve this answer | |
  • 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.