I am trying to include MatLab code in my document using the listings package, but it seems like strings are not interpreted correct.

fprintf('ABC %s:\n', mystring);
fprintf("ABC %s:\n", mystring);
\usepackage[english]{babel}     %use english or ngerman
\usepackage[utf8]{inputenc}     %German äöü for input
\usepackage[T1]{fontenc}        %German äöü for pdf
\usepackage{textcomp}           %Additional Symbols e.g. degree

\definecolor{keyword}{RGB}{0,175,203}      %color
\definecolor{comment}{rgb}{0.06,0.58,0.07} %color
\definecolor{label}{rgb}{0.5,0.5,0.5}      %color

\lstset{                                   %Configure code


    caption={Broken Syntax Highlighting}
fprintf('ABC %s:\n', mystring);
fprintf("ABC %s:\n", mystring);



Result of the MWE

Is it possible to add " to the same list that ' is in?


The Matlab language definition only defines ' as string delimiter by default. But you can also make " one by adding the following line to the listing's options:


m is a special delimiter type for Matlab code defined as

[...] a special type for Ada and Matlab and possibly other languages where the string delimiters are also used for other purposes. It is equivalent to d, except that a string does not start after a letter, a right parenthesis, a right bracket, or some other characters.

If you don't want that special behavior, use morestring=[d]".

You can also define a new language which is based on Matlab with the new string type added:


Using that new language in your listing gives the desired output:

enter image description here

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.