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

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

2 Answers 2

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.

3
  • He will get an error in Matlab if he doesn't do so.
    – Hunsu
    May 7, 2014 at 21:45
  • @user230137 Exactly.
    – jub0bs
    May 7, 2014 at 21:48
  • Ok I get it, thank you very much! In this site I am learning also about Matlab! :P
    – Adam
    May 7, 2014 at 21:50
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

1
  • 2
    Do that if you want, but that's very misleading, because it shows incorrect syntax.
    – jub0bs
    May 7, 2014 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .