# Listings problem with quotes in a string

A single quote occuring inside a listings string seems to be interpreted as ending the string. Is there a way to avoid this from happening?

\documentclass[12pt]{article}
\usepackage[top=0.3in, bottom=1.2in, left=0.8in, right=0.8in]{geometry}

\setlength{\parindent}{0cm}

\usepackage[english]{babel}

\usepackage{listings}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\begin{document}

\lstset{language=Matlab,
basicstyle=\ttfamily,
keywordstyle=\color{Blue}\ttfamily,
stringstyle=\color{Red}\ttfamily,
morecomment=[l][\color{Magenta}]{\#},
breaklines=true,
breakindent=0pt,
breakatwhitespace,
columns=fullflexible,
showstringspaces=false
}

\begin{lstlisting}
a=1;
if a==1
disp('Is 1')
elseif a==2
disp('Is 2')
else
disp('I don't know')
end
\end{lstlisting}

\end{document}


-
Because single quotes are used as string delimiters in Matlab, you need to escape that character in order to make it part of the string. This is done by typing two single quotes in a row: disp('I don''t know'). –  Jubobs May 7 at 21:42
It adds two single quotes in the output. I want the output to have a single quote. If you see the Matlab documentation it uses single quotes. –  Adam May 7 at 21:44
Don't you get error when you compile your program in Matlab? –  user230137 May 7 at 21:47
If what you want to show is the exact Matlab code, then it's correct to show the two single quotes. Can you post a link to the online Matlab help showing two single quotes as one? –  Jubobs May 7 at 21:47
I misread it sorry. Thank you for your help in LaTeX and in Matlab! –  Adam May 7 at 21:51

Because single quotes are used as string delimiters in Matlab, you need to escape that character in order to make it part of the string. This is done by typing two single quotes in a row:

disp('I don''t know')


The Matlab language defined by listings is clever enough to typeset the right thing, in this case:

As a side note, you might be interested in the matlab-prettifier package; see this answer.

-
He will get an error in Matlab if he doesn't do so. –  user230137 May 7 at 21:45
@user230137 Exactly. –  Jubobs May 7 at 21:48
Ok I get it, thank you very much! In this site I am learning also about Matlab! :P –  Adam May 7 at 21:50

A possible solution from listings.pdf with mathescape parameter:

\documentclass[12pt]{article}
\usepackage[top=0.3in, bottom=1.2in, left=0.8in, right=0.8in]{geometry}

\setlength{\parindent}{0cm}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{listings}

\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\begin{document}

\lstset{language=Matlab,
basicstyle=\ttfamily,
keywordstyle=\color{Blue}\ttfamily,
stringstyle=\color{Red}\ttfamily,
morecomment=[l][\color{Magenta}]{\#},
breaklines=true,
breakindent=0pt,
breakatwhitespace,
columns=fullflexible,
showstringspaces=false
}

\begin{lstlisting}[mathescape]
a=1;
if a==1
disp('Is 1')
elseif a==2
disp('Is 2')
else
disp('I don$\texttt{\color{red}'}$t know')
end
\end{lstlisting}

\end{document}


-
Do that if you want, but that's very misleading, because it shows incorrect syntax. –  Jubobs May 7 at 22:16