Sign up ×
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.

I want to write MATLAB code in a sentence.

I installed package mcode, no problem if I want to display the code by inserting the code in environment \begin{verbatim} and \end{verbatim}.

How could I possibly insert the MATLAB code in a sentence?

share|improve this question
You might be interested in the matlab-prettifier package; see this answer. – Jubobs Apr 28 '14 at 15:35

1 Answer 1

up vote 8 down vote accepted

The mcode package provides \mcode{<code>} for inline MATLAB code. The following example is taken almost verbatim from the demo documentation:

enter image description here

This inline demo \mcode{for i=1:3, disp('cool'); end;} uses \verb|\mcode{}|.

Fundamentally, mcode uses listings but sets all the appropriate keys so you don't have to worry about setting it yourself.

For full-width listings, use the regular lstlisting environment rather than verbatim, since the formatting will otherwise be different (again, taken verbatim from the documentation demo):

enter image description here

for i = 1:3
    if i >= 5                   % literate programming replacement
        disp('cool');          % comment with some §\mcommentfont\LaTeX in it: $\mcommentfont\pi x^2$§
    [~,ind] = max(vec);
    x_last = x(1,end);
    really really long really really long really really long really really long really really long line % blaaaaaaaa

listings also provide a language=Matlab setting, but the formatting differs from that provided by mcode.

share|improve this answer
thank you so much, it works. :-) – azlan aznam Aug 30 '12 at 6:57

Your Answer


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.