4

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,
        commentstyle=\color{Emerald}\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}

enter image description here

jub0bs
  • 58,916
Adam
  • 4,684
  • 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'). – jub0bs May 07 '14 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 07 '14 at 21:44
  • Don't you get error when you compile your program in Matlab? – Hunsu May 07 '14 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? – jub0bs May 07 '14 at 21:47
  • 1
    I misread it sorry. Thank you for your help in LaTeX and in Matlab! – Adam May 07 '14 at 21:51

2 Answers2

5

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:

enter image description here

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

jub0bs
  • 58,916
1

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,
        commentstyle=\color{Emerald}\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}

enter image description here