2

I have don't know how to fix the double quote character (’"’) with the listing package. Listing get this as the beginning of a string but it is not.

Here is the colored minimal example to show the problem :

\documentclass{standalone}
\usepackage{listings}
\usepackage{color}
\lstset{
  language=Bash,
  stringstyle=\color{red},
  commentstyle=\color{green},
  showspaces=false,
  showstringspaces=false,
}

\begin{document}
\begin{lstlisting}
    file_name=$(cut -d '"' -f 2)
    # This shoud be a comment
    echo "This should be a String"
\end{lstlisting}
\end{document}

Results in :

enter image description here

Thanks for any help !

Edit : Note that if I replace the double quote character by the letter a, the coloration is working well :

enter image description here

  • Welcome to TeX.SX! I guess that the quotes are creating the problem, rather than ". – egreg Jun 26 '15 at 21:26
  • Thanks for welcoming me ! Yes in fact I would like to escape the character between the quote but I don't know how to do that ;) – Marc Jun 26 '15 at 21:30
  • The output here seems to be as-expected... – Werner Jun 26 '15 at 21:41
  • i'm a bit confused here, as an answer has already been accepted that i would have thought not what was really wanted. what i would have expected is that the double quote should be the "ditto", or straight double quote, unicode U+0022, whereas what's given in the answer is the right double quote, U+201D. if it's really the straight quote, i know an approach that works with the "typewriter" font, and could try to adapt it to be used with lstlisting. – barbara beeton Jun 27 '15 at 12:35
  • Thanks for your help ! I don't really care about which unicode double quote character is used. I accepted the answer as it solves what I care about : I want the comment to appear in green and I want the String to appear in red. I apologies for being not clear in first place. – Marc Jun 27 '15 at 13:40
2

Try with moredelim:

\documentclass{standalone}
\usepackage{listings}
\usepackage{color}
\lstset{
  language=bash,
  stringstyle=\color{red},
  commentstyle=\color{green},
  showspaces=false,
  showstringspaces=false,
  moredelim=[s]'',
}

\begin{document}
\begin{lstlisting}
    file_name=$(cut -d '"' -f 2)
    # This shoud be a comment
    echo "This should be a String"
\end{lstlisting}
\end{document}

Here is the output:

Output of the code

Or maybe morestring is more appropriate. In bash, is ' escaped via \ or by doubling it? I guess via \:

\documentclass[border=6pt]{standalone}
\usepackage{listings}
\usepackage{color}
\lstset{
  language=bash,
  stringstyle=\color{red},
  commentstyle=\color{green},
  showspaces=false,
  showstringspaces=false,
  morestring=[b]',
}

\begin{document}
\begin{lstlisting}
    file_name=$(cut -d '"' -f 2)
    # This shoud be a comment
    echo "This should be a String"
\end{lstlisting}
\end{document}

Output:

Output of second example

  • This is exactly the kind of solution I am looking for :) – Marc Jun 26 '15 at 22:57
  • Unfortunately, my minimal example seems to be incomplete : I need to include existing code. – Marc Jun 26 '15 at 22:59
  • Is there a way to have morestring or moredelim working with included file ? – Marc Jun 26 '15 at 22:59
  • \lstinputlisting[language=bash]{test.sh} – Marc Jun 26 '15 at 23:01
  • i justed changed the example by moving the code in a separate test.sh file – Marc Jun 26 '15 at 23:01

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.