Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
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
1  
I misread it sorry. Thank you for your help in LaTeX and in Matlab! –  Adam May 7 at 21:51

2 Answers 2

up vote 5 down vote accepted

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.

share|improve 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,
        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

share|improve this answer
1  
Do that if you want, but that's very misleading, because it shows incorrect syntax. –  Jubobs May 7 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.