Code covered by the BSD License

4.87179
4.9 | 120 ratings Rate this file 907 Downloads (last 30 days) File Size: 46.5 KB File ID: #8015 Version: 1.18

# M-code LaTeX Package

### Florian Knorn (view profile)

12 Jul 2005 (Updated )

Easily include nicely syntax highlighted m-code in your LaTeX documents.

File Information
Description

There are many packages out there that do pretty much the same. However, there are also usually many settings and tweaks needed to get it to look just right.
This package saves you from all this hassle — just include it and you're done.
I originally wrote this package for myself to have colourful source code highlighted in LaTeX, looking exactly like in the Matlab editor — being able to just copy-paste the code directly (no pre-treatment with highlight.m or the likes), or simply give a file name and have its contents included and syntax highlighted.

Again, this packages relies on a bunch of other packages (and in particular the listings package for the actual work), but spares you from loads of configurations... So please first check the source code or the configuration options of the packages used if you have any issues.

I've also thrown together a small demo-document (now included) which should give you a quick example on how to actually use the package.

Acknowledgements

This file inspired Speech Spectrogram, Analysis Modification Synthesis, Phase Spectrum Compensation, and Export Tex.

MATLAB release MATLAB 6.5 (R13)
Other requirements LaTeX with packages "listings", "xcolor" and "textcomp".
19 Aug 2015 Gabriel Ruiz

### Gabriel Ruiz (view profile)

Very useful! Thanks

26 Jun 2015 Lazaros Moysis

### Lazaros Moysis (view profile)

07 Apr 2015 Pritesh Shah

### Pritesh Shah (view profile)

04 Apr 2015 Sergei Paleichuk

19 Feb 2015 Nina

### Nina (view profile)

Dear all

I would love to hear if you have advise on this problem! I have this huge assignment (handing in on sunday). Unfortunate addig my programs the frame around the scripts fucks up, it floats in wrong directions - has anyone had this problem?
Best regard Nina from Denmark

Comment only
16 Feb 2015 Joey

### Joey (view profile)

Very well written and documented. I followed the documentation and it worked flawlessly.

Thank you

13 Feb 2015 Iftikhar Ali

### Iftikhar Ali (view profile)

Aweesome

Comment only
12 Feb 2015 Lazaros Moysis

### Lazaros Moysis (view profile)

That's genious! Does anyone know if there is a package to do this with mathematica code?

Comment only
04 Nov 2014 Georg

### Georg (view profile)

05 Oct 2014 Daeyun Shin

### Daeyun Shin (view profile)

04 Aug 2014 jyloup p

### jyloup p (view profile)

@Alon: mcode works perfectly with Bakoma Tex 10.40 (issued May 2014). Matlab code is tracked in both source and pdf display, except for selection in inline \mcode{....} but this is a detail.

01 Aug 2014 Abdullah Masroor Hashmi

### Abdullah Masroor Hashmi (view profile)

Nice package, have been using it for a year. I noticed that if you have double quotes in a comment it doesn't work. Gives an Input Encoding error.

15 Jul 2014 Sean

### Sean (view profile)

26 Jun 2014 Julien Cretel

### Julien Cretel (view profile)

Comment only
23 Jun 2014 Florian Knorn

### Florian Knorn (view profile)

@Yiang: Maybe try comment out the "upquote=true" statement in mcode.sty ...

Comment only
23 Jun 2014 Florian Knorn

### Florian Knorn (view profile)

@Michael: Nope, read the listings package docs why.

Comment only
21 Jun 2014 Michael Kruger

### Michael Kruger (view profile)

Good day. Is there a setting that allows for auto code wrapping?
Regards

11 Jun 2014 Yiang

### Yiang (view profile)

Hello! I keep getting this error: \textquotesingle unavailable in encoding T1. If I remover the statements that include ' or "" it works but I need to keep the fprintf statements. Can someone help? I would appreciate it very much!

Comment only
29 Apr 2014 Florian Knorn

### Florian Knorn (view profile)

Cheeky indeed. But good work! About time someone did this properly.

Comment only
28 Apr 2014 Julien Cretel

### Julien Cretel (view profile)

At the risk of being cheeky, I'd like to advertise matlab-prettifier my LaTeX package for typesetting Matlab code: http://www.ctan.org/pkg/matlab-prettifier

It improves upon mcode in a number of ways: the context-sensitive keyword "end", section titles, system commands, line-continuation token (...) all get highlighted correctly. I also plan to automate the highlighting of unquoted strings (e.g. "on" in "hold on") in a future release.

Enjoy!

Comment only
23 Apr 2014 Stefan Karlsson

### Stefan Karlsson (view profile)

%%%% fix the underscore ( _ ) issue %%%%
\usepackage[T1]{fontenc}

%%%% fix the minus ( - ) issue %%%%
\makeatletter
\lst@CCPutMacro\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-{}}}
\@empty\z@\@empty
\makeatother

Comment only
16 Apr 2014 Stefan Karlsson

### Stefan Karlsson (view profile)

BTW, great work :)

Comment only
16 Apr 2014 Stefan Karlsson

### Stefan Karlsson (view profile)

Until Florian gets around to fixing the "minus sign bug" for us, here is the fix spelled out for all:

1. open the style file
2. go to end of file, just before the "\endinput" statement, enter the following:

\makeatletter
\lst@CCPutMacro\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-{}}}
\@empty\z@\@empty
\makeatother

save file and start typesetting. Haven't experimented with this too much yet, but it has worked for my examples. The minus sign is correctly interpreted by matlab when you copy from the pdf.

Comment only
16 Apr 2014 Stefan Karlsson

### Stefan Karlsson (view profile)

Until Florian gets around to fixing this for us, here is the fix spelled out for all:

1. open the style file
2. go to end of file, just before the "\endinput" statement, enter the following:

\makeatletter
\lst@CCPutMacro\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-{}}}
\@empty\z@\@empty
\makeatother

save file and start typesetting. Haven't experimented with this too much yet, but it has worked for my examples. The minus sign is correctly interpreted by matlab when you copy from the pdf.

15 Apr 2014 Ofelie

### Ofelie (view profile)

Thanks, it worked!

Comment only
14 Apr 2014 Florian Knorn

### Florian Knorn (view profile)

@Ofelie: I suspect something with file and/or font encodings. Try this. Find this line in your local mcode.sty "{delta}{{\tiny$\Delta$}}1 % \Delta" and insert this below "{µ}{{$\mu$}}1 % \mu" (without the quotes of course). Does that help?

By the way, adding a "µ" to the mcode demo document half-works. While it doesn't seem to render on screen, it also doesn't break compilation...

Comment only
13 Apr 2014 Supreeth

### Supreeth (view profile)

Thanks a lot! It works pretty well.

11 Apr 2014 Ofelie

### Ofelie (view profile)

@Florian: This is the error: "! Missing $inserted.<inserted text>$ ylabel('Voltage (µV)" It doesn't generate a pdf file if there are 'µ' signs in the code...

Comment only
09 Apr 2014 Florian Knorn

### Florian Knorn (view profile)

@Ofelie: "An error" is not very descriptive of the problem...

Comment only
09 Apr 2014 Florian Knorn

### Florian Knorn (view profile)

@Sevada: Have a look at the documentation (top of the mcode.sty file), in particular the "useliterate" option.

Comment only

Thx a lot, it works really nice for me, but I have a small problem,
In my matlab code I have variable, which name is "delta" in latex it converts to delta simbol (the triangle). is there any way to fix that? Of course I can change the name of variable, but I used it in many different places...

Comment only
07 Apr 2014 Ofelie

### Ofelie (view profile)

Thanks for the useful code! I got a little question: I used 'µ' in the comment of my matlab code and it gives an error in Latex. Do you have any idea how I can solve this?

Comment only

wonderful

Comment only
04 Dec 2013 Daniel

### Daniel (view profile)

20 Nov 2013 Selcuk Fidan

### Selcuk Fidan (view profile)

Nice work! Thanks for sharing!

06 Nov 2013 JH

### JH (view profile)

Florian-

Thanks for the help (4.8 in particular). My apologies for making you repeat yourself, but the direction to the correct item in the listings doc is greatly appreciated.

-Jason

Comment only
05 Nov 2013 Florian Knorn

### Florian Knorn (view profile)

@JH: As I said many times before, please have a look at the listings documentation (type "texdoc listings" in a command prompt, or find it here: http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf), Section 4.8 in particular.

Comment only
05 Nov 2013 Florian Knorn

### Florian Knorn (view profile)

@Tony Almeida: Thanks for the pointer. The font size is static for each code block / inline code is static (it's set in the .sty file), so it doesn't change with context (such as in a footnote). As a remedy, I've updated the package for you to include a new \mcodefn{} command, which produces the inline code in footnotesize. Have a look at the mcode.sty for more details, in case you want to further adapt it for your particular application.

Comment only
05 Nov 2013 Florian Knorn

### Florian Knorn (view profile)

@Alex: Not sure what your question is, but you can use this package for that purpose, provided you disable line numbering.

Comment only
04 Nov 2013 JH

### JH (view profile)

Hi, all-

Excellent package, but I don't know much about listings.sty. I am using the cut and paste approach to document my mcode in latex with the frame and line number options.

Does anyone know how to get contiguous line numbers from one \begin{lstlisting} block to another? Something like

\begin{document}

\begin{lstlisting}
%some matlab code
\end{lstlisting}

some latex text.

\begin{lstlisting}
%some more matlab code
\end{lstlisting}

Thanks,
-Jason

Comment only
03 Nov 2013 Alex

### Alex (view profile)

Hi, if anyone can advise on the following matter: I would like to include an m-file in my dissertation so that afterwards it is possible to simply copy-paste the code from the generated pdf-file to Matlab?

Comment only
18 Oct 2013 Tony Almeida

### Tony Almeida (view profile)

hi!
This turned to be a great and helpful package!

One question (and maybe this is due to my lack of knowledge on LaTeX): is it possible to set the font size other than default size? For example, I need to place some code lines in a footnote, is it possible to reduce the font?

08 Oct 2013 Florian Knorn

### Florian Knorn (view profile)

Hi Tintin, not sure what the issue is. Try using the demo file provided and work your way on from there...

Comment only
03 Oct 2013 Tintin Milou

### Tintin Milou (view profile)

Thanks for the package. Somehow my minus signs don't show up when compiling the latex document even though I put them in the original (matlab) code in my latex source code. What could be the reason for that and is there a remedy?
Thanks!

Comment only
08 Sep 2013 Florian Knorn

### Florian Knorn (view profile)

@Mike - automatic line breaking [especially with comments] is not supported by the listings package (which does the work under the hood). You unfortunately have to do that manually...

Comment only
08 Sep 2013 Mike

### Mike (view profile)

Very nice package! For normal use it works like a charm.
However I got an error with multiline comments, where the frame/border got horizontally seperated.
This happened with a broad line, and the Latex things like pi in your example.

Great work though, keep it going!

05 Sep 2013 Zoltán Csáti

### Zoltán Csáti (view profile)

Rodrigo: thank you for the comment, it helped me a lot. It was the case with the Hungarian letter á, now I could handle the error fortunately.

20 Aug 2013 Rodrigo

### Rodrigo (view profile)

Great package ;)
For french users, I found that wich works perfectly with the 'M-code LaTeX Package' :
/!\ That's only allow you including m-code WITHOUT accents into a latex code which may have accents./!\

> http://stackoverflow.com/a/14070484

\usepackage[utf8]{inputenc}
\usepackage[francais]{babel}
\usepackage[T1]{fontenc}
\usepackage{listings}
\lstset{language=R}
\lstset{%
inputencoding=utf8,
extendedchars=true,
literate=%
{é}{{\'{e}}}1
{è}{{\{e}}}1
{ê}{{\^{e}}}1
{ë}{{\¨{e}}}1
{û}{{\^{u}}}1
{ù}{{\{u}}}1
{â}{{\^{a}}}1
{à}{{\{a}}}1
{î}{{\^{i}}}1
{ç}{{\c{c}}}1
{Ç}{{\c{C}}}1
{É}{{\'{E}}}1
{Ê}{{\^{E}}}1
{À}{{\{A}}}1
{Â}{{\^{A}}}1
{Î}{{\^{I}}}1
}

Bönnê jõûrnéeè ;)

14 Aug 2013 Florian Knorn

### Florian Knorn (view profile)

@ Jean: Have a look at the listings documentation (type "texdoc listings" in a command prompt, or find it here: http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf), Section 4.4 in particular and the "linerange" option.

Comment only
13 Aug 2013 Jean

### Jean (view profile)

This is a great package!
I just have one question.
I would like to insert snippets of MATLAB code into my thesis, which is a template document when calls all the chapters (separate .tex files). This package works great for putting a whole .m file in my index. But what if I want to just copy and paste a bit of the code into a chapter in order to explain it? Is there a way to do this, or do I just have to make separate little .m files for each snippet?
Best, and thanks.

13 Aug 2013 Jimmy

### Jimmy (view profile)

18 Jul 2013 Zhongxian pan

### Zhongxian pan (view profile)

This package is very convenient.

But I have some advice. When I used some other packages, I found the 'framed' code can't spread in two pages. So you can add '\lstset{extendedchars=false}' in the framed condition.

12 Jul 2013 Florian Knorn

### Florian Knorn (view profile)

Well, I'm an Apple Macintosh user and like TeXShop, but I'm not sure if the editor itself has anything to do with it.

I would try to compile the demo document that comes with the package first, and take it from there. Oftentimes it's simply a clashing between packages.

Comment only
12 Jul 2013 Alon Ohev-Zion

### Alon Ohev-Zion (view profile)

I work always with the code, It is just easier to have the live preview.
is there a different editor you can recommend?

Comment only
12 Jul 2013 Alon Ohev-Zion

### Alon Ohev-Zion (view profile)

Thanks :)

Comment only
09 Jul 2013 Florian Knorn

### Florian Knorn (view profile)

@Alon: Sorry, but I cannot help you with BaKoMa TeX. LaTeX just isn't made for WYSIWYG, you're better off using the more traditional approach to this wonderful typesetting system.

Comment only
09 Jul 2013 Alon Ohev-Zion

### Alon Ohev-Zion (view profile)

I'ts a grate one!! though I have two problems.

I use BaKoma.
1. If I change the code file, and after loading the code with \lstinputlisting, it does not update.
2. If I save the file from the LaTex editor, it saves over the original file!!
Thank you for your help. :)

12 Jun 2013 Matthias Thul

### Matthias Thul (view profile)

Excellent!

29 May 2013 Florian Knorn

### Florian Knorn (view profile)

Hi Bob,

That means there's still something wrong with your TeX installation (textcomp requires more than just a .sty file chucked in the same path).

If you can't get it working, you can also disable it, by removing / commenting out the following in mcode.sty:

\ifx\textquotesingle\undefined%
\RequirePackage{textcomp}%
\fi

as well as

upquote=true, % upright quotes

Comment only
29 May 2013 Bob

### Bob (view profile)

@Florian Knorn : Whenever I load this package I consistently get the following error:

! Package Listings Error: Option upquote' requires textcomp'package.
(Listings) The option has been disabled.

I've installed the textcomp package, as well as yours, and both sty files are in my tex directory.

Any help would be appreciated!

Comment only

12 Apr 2013 kevin

### kevin (view profile)

@Florian Knorn : I double checked it and found the problem occured only when I had "\usepackage[papersize={3000mm,250mm}]{geometry}" in the preamble

Comment only
09 Apr 2013 Mustafa Isik

### Mustafa Isik (view profile)

This is a much appreciated contribution. Thank you, esp. for updating it.

09 Apr 2013 Florian Knorn

### Florian Knorn (view profile)

@ kevin: Strange, I haven't observed this behaviour. Can you email me a minimal working example that reproduces the problem?

Comment only
09 Apr 2013 kevin

### kevin (view profile)

The package is generally excellent except that I got one problem: there will be one line missing when the m file included is longer than one page; it seems something is needed to handle the page break issue

19 Feb 2013 Joshua Stough

### Joshua Stough (view profile)

Worked great out of the box. Thank you

06 Feb 2013 Florian Knorn

### Florian Knorn (view profile)

@ Jon: Yeah, there's something wrong with your LaTeX installation. In particular, you are missing the "pcr" font (that's the Courier font used by the package to display stuff in monospaced font, similar to Matlab...).

Comment only
06 Feb 2013 Jon

### Jon (view profile)

I can't get the demo file to work on a Mac installation using TexMaker. I get several errors similar to the following:

Any ideas?

Comment only
02 Feb 2013 Aidin

### Aidin (view profile)

30 Jan 2013 Timothy Kinyanjui

### Timothy Kinyanjui (view profile)

Great submission. Works pretty flawlessly.

Comment only
22 Nov 2012 Marcel Veerhoek

### Marcel Veerhoek (view profile)

22 Nov 2012 Marcel Veerhoek

13 Nov 2012 Abel

### Abel (view profile)

26 Oct 2012 Florian Knorn

### Florian Knorn (view profile)

Thanks @Eric. I've had a look at getting the slim line working. Half got there using this "literate programming" string:

{\%\%}{{{\color[gray]{.85}{\makebox[0em][l]{\raisebox{2ex}[0pt][0pt]{\rule{\linewidth}{.4pt}}}}}\%\%}}{2}

Problem: Cell comment (%%) must begin on the beginning of the line, but worse: the remainder of the line no longer gets printed in green. Not sure how you can use the "literate programming" feature but still trigger the comment-formatting...

Comment only
25 Oct 2012 Eric

### Eric (view profile)

@Hussain - I agree - cell mode would be nice. Not sure how to add a slim line above but I did make a change so the cell delimeter (%% ) would make the line bold. Just change the follwing lines (starting at line 198) from this:

% ---------------------------------------------------------------------------------
% define colours and styles
\ifbw % use font formating and gray 'colors'
\def\mcommentfont{\color[gray]{.75}\itshape} %comments light gray and italic
\lstset{language=matlabfloz, % use our version of highlighting
keywordstyle=\bfseries, % keywords in bold
stringstyle=\color[gray]{0.5} % strings darker gray
}
\else% notbw => use colors : )
\lstset{language=matlabfloz, % use our version of highlighting
keywordstyle=\color[rgb]{0,0,1}, % keywords in blue
stringstyle=\color[rgb]{.627,.126,.941} % strings in purple
}
\fi%bw

to this:

% ---------------------------------------------------------------------------------
% define colours and styles
\ifbw % use font formating and gray 'colors'
\def\mcommentfont{\color[gray]{.75}\itshape} %comments light gray and italic
\def\mcelldelimfont{\bfseries\color[gray]{.75}\itshape}
\lstset{language=matlabfloz, % use our version of highlighting
keywordstyle=\bfseries, % keywords in bold
stringstyle=\color[gray]{0.5} % strings darker gray
}
\else% notbw => use colors : )
\lstset{language=matlabfloz, % use our version of highlighting
keywordstyle=\color[rgb]{0,0,1}, % keywords in blue
stringstyle=\color[rgb]{.627,.126,.941} % strings in purple
}
\fi%bw

25 Oct 2012 Eric

### Eric (view profile)

12 Oct 2012 Francesco

### Francesco (view profile)

Hi there,
@Florian I've found a workaround for the accented character affair, it's a little bit tricky but it works: just added something like «{è}{{\e}}1» to the «literate» list to show properly «è» without the utf8 encoding error (a real pain for us latin language speaker).
Probably there is a better way to solve the problem that writing down every possible accented character twice (literate option on/off), but it's the only one that worked for me so far; even changing MATLAB encoding didn't work.
Great submission, by the way.

11 Sep 2012 kpi

### kpi (view profile)

Thanks, I like the formatting and would like to use it but I also have listings in more than one programming language in my latex document. The Matlab syntax highlighting seems to be used for all listings. How can I select only Matlab listings to format with mcode syntax highlighting?

Comment only
30 Aug 2012 Florian Knorn

### Florian Knorn (view profile)

@Juan: Not sure why you are having this issue. The package should load the textcomp package in case \textquotesingle is not defined already. Try manually including the textcomp package before loading mcode to see if that helps...

Comment only
13 Aug 2012 Henrik

### Henrik (view profile)

08 Aug 2012 Alejandro

### Alejandro (view profile)

Excellent package! It is everything I hoped for and more!

28 Jul 2012 ageage Juan

### ageage Juan (view profile)

Hi Florian,
I am using Lyx, and got an error message in the text below, when I tried to show the following one-line .m code: fprintf ( 1, ' Hello, world!\n' );

In the error message, it shows that LaTex Error: Command \textquotesingle unavailable in encoding T1. And the corresponding description shows more detail as follows:

"fprintf ( 1, '
Hello, world!\n' );
Type I <command> <return> to replace it with another command,
or <return> to continue without it."

Do you have any idea of what is going on? Thanks!!

Comment only
24 Jul 2012 KJS Verlinde

### KJS Verlinde (view profile)

Awesome package. thanks a lot!

26 May 2012 john

### john (view profile)

21 May 2012 Henning

### Henning (view profile)

Hey Florian,
cool tool! It worked when I changed 'utf8' to 'latin1'. I'm excited if this will work with all other usepackages I need...
But, nevertheless, great job!

12 May 2012 Johannes Naujocks

### Johannes Naujocks (view profile)

Sorry, never mind...works now!

Comment only
16 Apr 2012 Hussain

### Hussain (view profile)

Thanks alot, Florian; this code is incredibly easy to use!

Although, an update to reflect cell-mode which uses prints a slim line above commented lines beginning with %% would be a nice addition.

14 Apr 2012 Florian Knorn

### Florian Knorn (view profile)

@Mikkel, Sorry, not quite sure what you mean. Why would you want to break a path (?)

Comment only
13 Apr 2012 Mikkel

### Mikkel (view profile)

Florian,
I have e problem with the linebreak. I can see that you write in the mcode.sty that it is buggy, and don't break comments correctly.
I can't make it break the path from witch I'm reading a file. It there some way go go around that? or is it just bad luck that i don't work?

other than that, great work :D

Comment only
10 Apr 2012 Bence Bardóczy

### Bence Bardóczy (view profile)

Thank you very much Florian!

Comment only
26 Mar 2012 Florian Knorn

### Florian Knorn (view profile)

@Adam: Thanks! That's exactly what I designed it for. Quick and easy deployment. Save other people the hassle of tweaking the listings package's settings.

Comment only

Florian, this is perfect. Worked perfectly! I used it for my Masters' thesis, and it was a godsend!
THANK YOU!

20 Mar 2012 Dan

### Dan (view profile)

Beautiful...worked perfectly. Now it's everything I needed. Thanks so much Florian.

Comment only
20 Mar 2012 Florian Knorn

### Florian Knorn (view profile)

@Dan: Try replacing the command defining the default font to be used by something like this:

\def\lstbasicfont{\fontfamily{pcr}\fontsize{8}{8}\selectfont}

In the \fontsize command, the first number is the fontsize and the second is the line spacing (roughly speaking), so play around with the numbers a bit until you like the result.

It appears, however, that some number combinations don't play nicely with framed listings. I don't know why this is the case; this is an issue with the listings package.

Comment only
20 Mar 2012 Dan

### Dan (view profile)

Top notch...looks beautiful. Thanks! Ok, one question. My code is 300 lines long and I don't want it to take up 9 pages as is (and yes I need it all). I've never even looked inside a .sty file before, but after a little looking around I found that the first line after Customize Below lets me change font size. But the lines are still spaced like the rest of my document (which is huge). How can a decrease the line spacing?

13 Mar 2012 Joana

### Joana (view profile)

I had also the same problem with the text lines in the m file being too long. Thanks Laurens!

Comment only
13 Mar 2012 Joana

### Joana (view profile)

That works so nice! thanks a lot!

22 Feb 2012 Bjørn

### Bjørn (view profile)

Thanks for this great package!

15 Feb 2012 Florian Knorn

### Florian Knorn (view profile)

Hi there, can't investigate this right now, but to remove this feature just uncomment line 228 of the current version (2.2) of mcode.sty.

Comment only
13 Feb 2012 Maciek

### Maciek (view profile)

Excellent package!

Not sure if this can be categorized as a bug, but the tilde character used as a placeholder, e.g. [~,output2] = function..... is translated into a negation sign which you get with $\neg$ in Latex

26 Jan 2012 Rob H

### Rob H (view profile)

Many thanks Florian. This has saved me hours making beamer slides for my students.

I do get an options clash when trying to use "bw" but can't trace it - small worry!

15 Dec 2011 Ana Pabon

### Ana Pabon (view profile)

Thank you!

15 Dec 2011 Glauber Miranda

### Glauber Miranda (view profile)

Dear Florian,
Many thanks for your job. It saved me a lot of work after being lost for some days with my thesis.
The package is Amazing!

Comment only
09 Dec 2011 David

### David (view profile)

Thanks Florian, you made my day!

Comment only
24 Nov 2011 Florian Knorn

### Florian Knorn (view profile)

@Maxi: You can indeed do this provided you use the original command provided by the listings package. So have a look at the .sty how the \mcode command is defined. Basically, in your example, instead of using \mcode, use this:

\lstinline[basicstyle=\lstbasicfont\small]|Test={'hans' 'peter'}|

Comment only
24 Nov 2011 Maxi Leinenbach

### Maxi Leinenbach (view profile)

Quiet Amazing! Does somebody know how to transform cell arays into latex for example
Test={'hans' 'peter'}
/mcode{Test={'hans' 'peter'}} does not work...
Thanks

22 Nov 2011 Cory Simon

### Cory Simon (view profile)

Amazing. Thank you!

17 Nov 2011 Florian Knorn

### Florian Knorn (view profile)

@ Axel: Sorry, still not sure I understand your question. Can you email me directly? Thanks.

Comment only
16 Nov 2011 Axel Lødemel Holene

### Axel Lødemel Holene (view profile)

Now I'm embarrassed, I didn't see the .tex-demo untill you mentioned it. But actually it still doesn't solve my problem, just made me conscious that I wasn't very precise:

I have a mile long matlab code which I'm implementing in an appendix using
\lstinputlisting{Resources/Matlab/plots.m}.

Is there a way not having to write the code directly in the .tex-file to get the fancy LaTeX typesetting?

Comment only
16 Nov 2011 Florian Knorn

### Florian Knorn (view profile)

@ Axel: Thanks for your kind words. Have a look at the demo-document included in the package. It shows quite clearly how you can get LaTeX to parse fancy stuff in your comments.

Comment only
16 Nov 2011 Axel Lødemel Holene

### Axel Lødemel Holene (view profile)

Dude, this is sweet! Thanks for this awesome package! Showed it to my (lab) partner, and he was extatic!

The only "draw-back" for me here is that LaTeX refuses to typeset my matlab comments in LaTeX style. That is, neither
- %Sets $k_1$
nor
- %Sets k_1
gives out the number as subscript. I'm working with Texshop for mac. Could you (or someone) please help me out? Thanks! =)

21 Oct 2011 David N

### David N (view profile)

12 Oct 2011 Florian Knorn

### Florian Knorn (view profile)

@ Niko: Again, my package isn't doing any work, it just configures the listings package for you. Anyway, I just had a look at the listings documentation for you. I didn't see an option for this directly, but what you can do is add in your case firstnumber=6 to the \lstinputlisting options to have the numbering start with 6. This will do what you want.

Comment only
12 Oct 2011 Niko

### Niko (view profile)

Thanks Florain, this package works really great!

One question, though: I include some code from a .m file like this
\lstinputlisting[firstline=6, lastline=18]{myscript.m}

Your package will display line numbers, but they are always numbered starting with 1. In the example above, it would be great to see the actual line numbers as you would see them in the Matlab editor, i.e. numbered from 6 to 18. Is this possible with your package?

10 Oct 2011 Malcolm Lidierth

### Malcolm Lidierth (view profile)

Worked a treat - even for this LaTeX newbie. It took me a while to click that some of the things I wanted (like controlling whitespace) were available by using options available already in the listings package

ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf

(Should have read the help int the sty file more carefully!)

08 Oct 2011 Thomas Schmidt

### Thomas Schmidt (view profile)

Thanks! This is a very thorough piece of software extension. Works right out of the box, just follow the instructions in the pdf document included.

05 Oct 2011 Ivan Brezani

### Ivan Brezani (view profile)

Great

03 Oct 2011 Rupert Wurnitsch

### Rupert Wurnitsch (view profile)

22 Sep 2011 simmal

### simmal (view profile)

13 Sep 2011 Florian Knorn

### Florian Knorn (view profile)

@ Oscar -- sorry to disappoint you, but the listings package doesn't handle unicode, as far as I know.

Comment only
13 Sep 2011 bcoss COSS

### bcoss COSS (view profile)

I've narrowed my error above down to an issue with adjusting the line spacing in footnotes and figure captions from 1.5 (body) to 1. Hard to replicate, will most MWE if I get one.

Comment only
12 Sep 2011 bcoss COSS

### bcoss COSS (view profile)

This package works great on its own, but I can't get it to work with my thesis. I suspect there must be some kind of conflict with my loaded packages. The error I get is: ./chapter1/source/section1-4.tex:8: LaTeX Error: \begin{document} ended by \end
{figure}. This happens with any figure in the thesis, as long as its the first one. The document compiles fine if I comment out the mcode usepackage and mcode content sections. Any help please?? I am stumped, spent almost 4 hours on this already. Thanks.

I am using the document class and the packages I have loaded are:
\usepackage{graphicx}
\usepackage[center, format=plain,labelsep=period]{caption} %Fixes Figure label, Figure #: to Figure #.
\usepackage{subfig} % multiple figures in a float
\usepackage{xfrac} %better math fractions
\usepackage{mdwlist}
\numberwithin{equation}{section} %number equations according to the sections
\usepackage{float} %float package
\usepackage{epstopdf} %support for eps to pdf with pdflatex
\usepackage{array} %table array support
\usepackage{multirow} %advanced table rows and columns
\usepackage{fixltx2e} %float placement fix
\usepackage[a]{esvect} %vector symbol support
\usepackage{appendix} %appendix support
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\usepackage{amssymb}
\usepackage{amsxtra}
\usepackage{,eepic,amssymb,srcltx}
\usepackage[super, numbers, sort&compress]{natbib}

Comment only
07 Sep 2011 Oscar Edvardsson

### Oscar Edvardsson (view profile)

Having problems with å, ä and ö. They are replaced by a simple space. Any ideas? Using this in my Latex-preamble:
\usepackage[T1]{fontenc}
\usepackage[swedish]{babel}
\usepackage[utf8]{inputenc}

Other than that, works like a charm!

28 Aug 2011 Derek O'Connor

### Derek O'Connor (view profile)

Better still, use

228 {~}{{$\sim\,\,$}}1 % \neg

Comment only
27 Aug 2011 Derek O'Connor

### Derek O'Connor (view profile)

@ Florian: that just gives you a tilde. What is needed is $\sim$.

I made this change in mcode.sty:

226 \lstset{%
227 literate=%
228 {~}{{$\sim$}}1 % \neg

Comment only
10 Aug 2011 Florian Knorn

### Florian Knorn (view profile)

@ Emil: Comment out Line 229.

Comment only
31 Jul 2011 Emil

### Emil (view profile)

A great package Florian! Being a complete beginner to LaTeX I don't dare messing with your .sty file.

Could you - or anyone here - tell me how I need to modify it if I want to display

[~,ind] = max(vec);

properly? The tilde does not show up the way it should...

Thanks a lot

Comment only
01 Jun 2011 Bruno

### Bruno (view profile)

Hi Florian,

Thank you very much for your file! It is really great! I would like to propose a workaround for the proper highlighting of the word "end" as a keyword and not as the last element of an array. My \lstset command looks like this:

\lstset{language=Matlab,
keywords={break,case,catch,classdef,continue,else,elseif,end,for,function,global,if,otherwise,parfor,persistent,return,spmd,switch,try,while},
basicstyle=\ttfamily,
keywordstyle=\color{blue},
stringstyle=\color{PurpleStr},
numbers=left,
numberstyle=\tiny\color{gray},
stepnumber=1,
numbersep=10pt,
backgroundcolor=\color{white},
tabsize=4,
showspaces=false,
showstringspaces=false,
comment=[l]\%,
morecomment=[l]...,
morecomment=[s]{\%\{}{\%\}},
morestring=[m]',
morecomment=[s][\color{black}]{(end}{)},
morecomment=[s][\color{black}]{,end}{)},
morecomment=[s][\color{black}]{(end,}{)},
morecomment=[s][\color{black}]{,end}{,},
morecomment=[s][\color{black}]{\{end}{\}},
morecomment=[s][\color{black}]{,end}{\}},
morecomment=[s][\color{black}]{\{end,}{\}},
}

Notice the last "morecomment" lines. They "cheat" by saying that if you find any of those combinations, then make them black.

Thanks!

02 May 2011 Florian Knorn

### Florian Knorn (view profile)

@Sweidan: So far no; might look into it one day, but have no time for that right now, sorry!

Comment only
29 Apr 2011 Sweidan Omar

### Sweidan Omar (view profile)

Dear Florian!

Is your package available in the TeX catalogue, so I can use it with TeX package manager?

thanks, and keep up the good work

31 Mar 2011 John D'Errico

### John D'Errico (view profile)

Nice

31 Mar 2011 Geoffrey

### Geoffrey (view profile)

Thanks Florian, This is a very useful tool!

15 Mar 2011 Florian Knorn

### Florian Knorn (view profile)

@ Petr: Thanks for the hint. I could have seen this earlier ;-) Anyway, just revised the package to include this (check documentation).

Comment only
15 Mar 2011 Petr Nechaev

### Petr Nechaev (view profile)

Thank you! Great tool!

This patch corrects long code line handling:

--- C:/Program Files (x86)/MiKTeX 2.8/tex/latex/mcode/mcode.old.sty Tue Mar 15 13:16:32 2011
+++ C:/Program Files (x86)/MiKTeX 2.8/tex/latex/mcode/mcode.sty Tue Mar 15 13:33:53 2011
@@ -218,5 +218,10 @@
\fi
\fi

+\lstset{ % deal with long code lines
+ breaklines=true, % break long code lines
+ breakatwhitespace=true % ... only at whitespace
+}
+
\endinput
%% End of file mcode.sty'.
\ No newline at end of file

15 Mar 2011 Hani

### Hani (view profile)

Thanks it is really nice and easy, but it seems to me that if you have a long code line, there will be problems.

23 Feb 2011 Serdar Tombul

### Serdar Tombul (view profile)

Perfectly working. But it would be good to add the wordwrap feature as sometimes the code might be longer than the textwidth.

04 Feb 2011 mathworks2011

### mathworks2011 (view profile)

Excellent!

22 Jan 2011 Sterren

### Sterren (view profile)

Fantastic latex module. I have one issue with the syntax highlighting though; when I clear a variable with the code

clear varname

in Maltab, the variables are in purple. I thought that this might be the package not understanding the method of passing variables in the form of

function 'char_variable' matrix_variable

as opposed to

function('char_variable',matrix_variable)

but when I write the code

It formats the 'data.m' in purple as I would expect. Maybe it's just something curious about the clear keyword.

Great package though! Thanks!

20 Dec 2010 Jerome

### Jerome (view profile)

02 Dec 2010 Florian Knorn

### Florian Knorn (view profile)

@ David: Sorry, try to install the listings package properly; the package requires more than just the .sty file as far as I know...

Comment only
02 Dec 2010 David Hann

### David Hann (view profile)

I can't seem to get even the demo to work.
! Undefined control sequence.
\lst@SelectCharTable ...eveloperSCT \lst@DefRange
\ifx \lst@Backslash \relax...
l.25 ...demo \mcode{for i=1:3, disp('cool'); end;}
uses the \verb|\mcode{}| ...

?
! Undefined control sequence.
\lst@SelectCharTable ...eveloperSCT \lst@DefRange
\ifx \lst@Backslash \relax...
l.29 \begin{lstlisting}

?
! Undefined control sequence.
\lst@SelectCharTable ...eveloperSCT \lst@DefRange
\ifx \lst@Backslash \relax...
l.31 ...font\LaTeX in it: $\mcommentfont\pi x^2$§

?
[1
here's the errors i can find
I'm using MikTex 2.8 on windows 7

Comment only
01 Dec 2010 Nick

### Nick (view profile)

@ Florian: Thanks for the debugging! I will try and set MATLAB to UTF-8 encoding.

Comment only
01 Dec 2010 Florian Knorn

### Florian Knorn (view profile)

@ Nick: Ok, figured it out -- your m-file needs to be in UTF-8 encoding for some reason (the one you sent me was Windows Latin 1). Not 100% sure why this makes a difference (but I suspect it has to do with the way the section sign § is encoded), but this will fix it for you. As an alternative, you could try to use a different math-escape character.

Comment only
01 Dec 2010 Nick

### Nick (view profile)

Ok here's my files. I also put mcode.sty in the .zip file. I suppose it's the same, but you never know.
http://db.tt/rnZVT34

Comment only
01 Dec 2010 Florian Knorn

### Florian Knorn (view profile)

Hm, looks like you do have the most recent version of listings. I've just tried it myself with the demo document, including some external .m file with § \LaTeX § in it and it works fine...

Have you tried including just a very simple m-file? Do you still get the same problem?

It's hard to debug things this way, maybe you can throw together a minimal non-working example so I can see for myself...

Comment only
01 Dec 2010 Nick

### Nick (view profile)

The .pdf that opens (in MiKTeX prog files) says:
The Listings Package
Maintainer: Brooks Moses <bmoses@dpdx.net>
2007/02/22 Version 1.4.
Does that help?
The thing is, when I typ MATLAB code in texworks itself everything works as it should. It is only when I try to load an m-file that things go pear-shaped.

Comment only
01 Dec 2010 Florian Knorn

### Florian Knorn (view profile)

@ Nick: Hm, works find for me... What version of the listings package are you using (try "texdoc listings" in a command line)

Comment only
30 Nov 2010 Nick

### Nick (view profile)

Hey Florian,

Great package, but I have one small problem. Whenever I load my m-file I can't use LaTeX typesetting by using § \latex §. It is nog recognized. Also not in your demo file. And when I set mathescape to true it recognizes math mode, but it typesets the math stuff in black. Help?

Comment only
23 Nov 2010 Marcelo Perlin

### Marcelo Perlin (view profile)

Thanks for sharing it Florian. I just used it in the documentation for a package I've wrote and it looks beautifull.

21 Nov 2010 Mikhail Konnik

### Mikhail Konnik (view profile)

Huge thanks, Florian, it is a great soft! Works smooth like a charm.

01 Nov 2010 Thomas Arildsen

### Thomas Arildsen (view profile)

This looks great - I have not tried it very much yet, however. Maybe you should consider putting this on CTAN.

Comment only
25 Oct 2010 Florian Knorn

### Florian Knorn (view profile)

@ Zhenyu -- glad you like the package. Replacing "delta" by "Δ" is actually intentional. But If you don't want that, simply remove the line "{delta}{{\tiny$\Delta$}}1% \Delta" from the file.

Comment only
24 Oct 2010 Zhenyu He

### Zhenyu He (view profile)

Great package! I have a question about using it. In my mablab code, I have a variable name 'delta', and it becomes a triangle in pdf file. I tried that 'Delta' is fine, 'omega' is fine. But 'delta' is not. How can I solve this problem?

11 Oct 2010 Charlie Matlack

### Charlie Matlack (view profile)

Easy to use and polished output, also saves space over using \verbatim! Thanks very much for writing this!

30 Sep 2010 Dimitri Yatsenko

### Dimitri Yatsenko (view profile)

13 Sep 2010 Ken H

### Ken H (view profile)

Ah hah! As with so many of us, you could have just said "Read my demo, dummy." For some reason your section symbols show up as "Â§" (that is, preceded by a A with a carrot) on my computer and when I left that symbol off, the escape didn't work. Thanks for your patience!

Comment only
13 Sep 2010 Florian Knorn

### Florian Knorn (view profile)

@ Ken H — thanks for your positive comment. I've had a look at your problem, but I can't reproduce it. Using my demo document (see link at the top), leaving mathescape=false as set by default in the document and including the tipa package, I can simply type §\textpipe§ to get the pipe showing up just fine. Not sure why this is not working for you... Try with the demo document, as this is a very barebones document. If it works there, but doesn't with your document, then it's almost sureley some package clash...

Comment only
13 Sep 2010 Ken H

### Ken H (view profile)

13 Sep 2010 Ken H

### Ken H (view profile)

This is just a ridiculously, remarkably useful and streamlined package. Thanks so much for putting it together.

I hate to ask such a simple question, but I've googled my head off and can't get it: the mathescape doesn't work for me! I change the .sty file to say mathescape=true,escapechar=§, but when I type §\textpipe§ within a {lstlisting} environment, I just get "\textpipe" when I compile. If I add dollar signs, I get a pipe that looks right, but if I try to cut and paste if from the .pdf, it doesn't copy correctly into the new document. However, if I use the \textpipe command in the regular latex environment, cut-and-paste works fine.

In fact, even using §x^2§ in the {lstlisting} environment doesn't produce formatted text. Either $x^2$ or §$x^2$§ works, but that solution doesn't work for the symbols I need...

I'm using the tipa package for the \textpipe.

Thanks so much to anyone who can help!

Comment only
08 Sep 2010 Florian Knorn

### Florian Knorn (view profile)

@ Thomas — There's no difference between the operating systems. The easiest way would be to just put it in the same path as your .tex file . . .

Comment only
08 Sep 2010 Thomas Weingarten

### Thomas Weingarten (view profile)

I used mcode.sty on my windows pc and it is brilliant.

But use it on my new mac with texmaker / texliveutility.

Does anybody know how to add this package ?

Comment only
23 Aug 2010 Florian Knorn

### Florian Knorn (view profile)

@ Ariel — How the heck can I know which keyboard you are using? That § symbol is called "Section Sign" (http://en.wikipedia.org/wiki/Section_sign) and on my Mac Keyboard (US layout) I can get it by pressing Alt + 6

Comment only
23 Aug 2010 Ariel Balter

### Ariel Balter (view profile)

Can someone please tell me what the heck that symbol is to escape listing mode and insert LaTeX in my code listing? More importantly, how the heck to I type it?

Comment only
01 Jun 2010 Florian Knorn

### Florian Knorn (view profile)

@ Fritz: Sorry, as far as I know the listings package doesn't support automatic line wrapping... This means, you'll have to manuall edit your code to make it fit into your code "box" (for me that's about 78 characters for instance). A little tip: Over in the MATLAB editor you can set a character per line number after which you can have stuff automatically wrapped, or you can wrap things manually but use the vertical line it produces in the editor as a guide...

Comment only
31 May 2010 Fritz Diorico

### Fritz Diorico (view profile)

Hello! This is really great. Thank you very much for this. I just have a small question. Some of my lines are really quite long and when I compile my tex file, the code is printed out to the borders of this page. How can I wrap the long lines of my source code? Sorry if this is a question due to lack of the LaTeX knowledge. Thanks.

28 Apr 2010 Vamshi Yamsani

### Vamshi Yamsani (view profile)

Thank you so much.

21 Apr 2010 Kamil Wojcicki

### Kamil Wojcicki (view profile)

Works great, thank you.

12 Apr 2010 Florian Knorn

### Florian Knorn (view profile)

@ durg ch: Please see the file description above, at the end of it is a download link to a small demo document (including LaTeX sources) where you can see how the package is used . . .

Comment only
12 Apr 2010 durg ch

### durg ch (view profile)

Hi,Can any of you please tell me how to insert the matlab code into latex file which I have already written.I Have seen the above comments but I ddnt get it clearly.Do I need to install anything else.Please do suggest in a clear manner by considering me as beginner.Thanking you all.

Comment only
13 Mar 2010 Jared

### Jared (view profile)

Forgot my comment. Worked great. Thanks for writing. I found that a floating figure was placed in the middle of the code box so the code was split. Just used \clearpage to prevent the floats overlapping.

06 Mar 2010 Haseeb Chaudhary

### Haseeb Chaudhary (view profile)

Worked perfectly!

25 Feb 2010 Andre Brito

### Andre Brito (view profile)

Hello,
was anyone able to make it work together with MiKTeX 2.8?

Comment only
10 Feb 2010 Jan Fousek

### Jan Fousek (view profile)

Very useful. To be able to use it with beamer class, I had to use [fragile] atribute on the frame containing the lstlisting block.

\begin{frame}[fragile]
...
\begin{lstlisting}
...

01 Feb 2010 Milan Rapai

Great work

26 Jan 2010 Alex

### Alex (view profile)

21 Jan 2010 Kevin

### Kevin (view profile)

17 Jan 2010 viktor

### viktor (view profile)

Heej Florian,

Windows does use backslashes but because ' \ ' js a latex command you have to use '/ ' it is just weird that

\graphicspath{C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/}

works fine. it is able to find the pictures. but when i type

lstlisting{D:/my documents/MATLAB/plotter.m}

it is unable to find the file but when you type

lstlisting{D:/latex/mfilefolder/plotter.m} (so with no spaces) it works fine. Thanks for the help but i will try to ask the listing developers

Comment only
17 Jan 2010 Florian Knorn

### Florian Knorn (view profile)

Hi Viktor, I don't think the listings package has that feature, sorry ...

I don't know how exactly you have to specify the path on Windows systems, but I always thought Windows uses backslashes ( \ ) in the path names, not forward slashes ... Could that be the problem?

Otherwise, I can't really help you, sorry ! Maybe ask the developers of the listings package ...

Comment only
17 Jan 2010 viktor

### viktor (view profile)

Hello i am still using this package with great pleasure but is there a way to specify the path where the package looks for the files? just the same with graphics

\graphicspath{C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/}

forinstance

because if i type the full path name within the lstlistings{...} command it does not work. I think it is due to the fact that the path name has spaces in them. Is there a way to solve this problem in the same way with graphics?

Comment only
14 Jan 2010 Ita Atz

### Ita Atz (view profile)

Hi! where can I get the lastest version of lstlisting?
I read that it would solve the upquote probelm!
I'm writing my thesis and I have to include a lot of matlab code with '...', so I really need it!!

Thanks for helping

Comment only
14 Jan 2010 Florian Knorn

### Florian Knorn (view profile)

Updated the demo document.

@ Rasmus: I've shuffeled things around a little bit in the source code, and added a description to make it really clear how to change the font size.

@ turker: I've modified the "solution" to the end/end problem; you can now use "iend" in the source code to get "end" typeset in black (when used to reference the last element in vector...)

@ All: If you use the LaTeX escape functionality in comments, there's now an easy way of getting that typeset in the same style as the rest of the comment, see the demo document ...

Comment only
14 Jan 2010 Rasmus Kinnerup

### Rasmus Kinnerup (view profile)

Works very well.... I think it would be good to include a guide on how to change the font size. I found it, but it was not that easy.

25 Dec 2009 Cat

### Cat (view profile)

Great thanks so much, I've got it to print smaller. The code is wonderful!

Comment only
10 Dec 2009 Florian Knorn

### Florian Knorn (view profile)

@ Cat: Replace the "\footnotesize" in that line by "\tiny". That's as small as it gets with standard fontsize commands. If you need even smaller fontsize, you'll need to remove this "\footnotesize" and replace the first line in the Config-Section with something like "\def\lstbasicfont{\fontfamily{pcr}\fontsize{3}{4.6}\selectfont}"...

Comment only
08 Dec 2009 viktor

### viktor (view profile)

now i dare to say the file is perfect, it saved me a lot of time!

Comment only
08 Dec 2009 viktor

### viktor (view profile)

The file works great all over the document, also in the appendix. so thank you Florian!

but i have one question, if i only compile a small part of a m-file for instance \lstinputlisting[firstline=5,lastline=8]{plo... is there a way to edit the numbering in the latex file because it always starts with 1 and not the original line number from the m-file

07 Dec 2009 Cat

### Cat (view profile)

Hello,

I was just wondering what change you make in the "% use font and smaller size" comment in the sty file to make the m-file font smaller. Thanks for the great code!

Comment only
25 Nov 2009 Uwe Lelke

### Uwe Lelke (view profile)

@ Daniel: You may wanna take a look at the following programm:

http://www.mathworks.com/matlabcentral/fileexchange/24515-m-code-to-latex-converter

which takes care of the three-dot-, the end-as-index- and several other little problems as well.

Comment only
24 Nov 2009 Florian Knorn

### Florian Knorn (view profile)

@ Lagrange: I've used the package in the appendix many times and it works fine. I suspect (because of the upquote error message) that you have a slightly outdated version of the listings package installed. Make sure you have the latest version.

@ Daniel: I know, I know, I'd love to have this fixed too, but such functionality appears to be quite unique to the Matlab scripting language, and the listings package (which does all the work) does not support such behaviour. I've mailed the package maintainer already, but he never got back to me...

Comment only
23 Nov 2009 Daniel

### Daniel (view profile)

Brilliant, thank you! One issue though - I can't seem to get my "..." in the m-files to show up as blue.

15 Oct 2009 Lagrange

### Lagrange (view profile)

[LaTeX] finished with exit status 1
./mcode.sty:181:Package keyval Error: upquote undefined. }
./Thesis.tex:50:Undefined control sequence. \begin{document}
./Thesis.tex:50:Argument of \caption@make has an extra }. \begin{document}

Comment only
14 Oct 2009 Lagrange

### Lagrange (view profile)

Sorry c&p the wrong error message:
./Thesis.tex:51:Undefined control sequence. \begin{document}

Y is there no "Edit"-function?

Comment only
14 Oct 2009 Lagrange

### Lagrange (view profile)

Hi!
Really great job - looks really fancy and works as long as you use it in a document. I have trouble using the package in the "Appendix":
./appendix.tex:5:Environment lstlisting undefined. \begin{lstlisting}

Comment only
14 Oct 2009 Laurens

### Laurens (view profile)

Thanks for this great LaTeX package!

I wanted to include a m-file which has very long lines, too long to display in a single line in my document. After reading the listings user reference I found how to do this:

\lsinputlisting[breaklines=true]{file.m}

you can also show only a few lines, for example only line 4 to 15 of the file.m file:

\lsinputlisting[firstline=4, lastline=15]{file.m}

20 Aug 2009 Florian Knorn

### Florian Knorn (view profile)

@ Alvin: Sorry, I never got the error you have. But thanks for pointing out a work-around, for those that do not have a "proper" TeX installation, including all fonts and map-files. You're right, the 'textcomp' package is only there for the upquotes. The idea of using that comes straight from the listings documentention (Section 4.7).

@ turker: Thank you very much for sharing your work-around for the 'end'-issue. While it's not too sleek, it works very well! Unfortunately there's little I can do to fix this in a more elegant way. As I said many times, the actual work is done by the listings pacakge, and as far as I can see it doesn't support different colouring of the same keyword...

Comment only
19 Aug 2009 turker

### turker (view profile)

though one solution is replacing end lstlisting environment with:
§\fontfamily{pcr}\selectfont end§

Comment only
19 Aug 2009 turker

### turker (view profile)

as uwe already mentioned, 'end' is not always used to finish a control statement; for example it is also used to indicate the last index of a matrix. in this type of situations m-code still prints the 'end' in blue. either package should be improved or a way should be created by which user becomes able to the change colour manually. (e.g. \textcolor doesn't work in lstlisting environment.)

Comment only
19 Aug 2009 Alvin

### Alvin (view profile)

It seems the textcomp package is the issue. A proper map file has to be found for the font. As a suggestion user cm-super to fix it. Otherwise if you're lazy like me use the courier package and get rid of the textcomp package. This comes with a warning though, since you can no longer use the option 'upquote' to make upright quotes.

Cheers

Comment only
19 Aug 2009 Alvin

### Alvin (view profile)

Hello,

I noticed the m-code package creates a bug in the itemize environment. When I included the m-code pagkage the itemize environment is corrupted. Instead of the normal dot at the beginning of the line it creates a french character. At first I thought it was something to do with my latex doument, but then I i created an itemize on the demo m-code and the same bug exists there too. Is there any way to fix/bypass this?

Comment only
15 Jul 2009 Bernd

### Bernd (view profile)

exactly what I was searching for

22 Jun 2009 Uwe Lelke

### Uwe Lelke (view profile)

not bad, but there are still problems with recognizing keywords and strings correctly

Comment only
16 May 2009 Erik Erikkson

### Erik Erikkson (view profile)

Note: For anyone wanting to include physical units and source code in the same report.

Use of the textcomp package in this file (required for 'upquote') breaks \celsius used by gensymb, siunitx and other unit packages.
Temporary fixes are to either use $^\circ$C in place of \celsius, or to set upquote=false and comment out the following lines in mcode.sty.

\ifx\textquotesingle\undefined%
\RequirePackage{textcomp}%
\fi

This is hopefully a temporary fix until Florian's next revision.

Comment only
13 May 2009 Erik Erikkson

### Erik Erikkson (view profile)

23 Apr 2009 TheFarmer

### TheFarmer (view profile)

A possible workaround for the coloring issue I mentioned above, is to replace all instances of code of the type:
hold on
format rat
legend boxoff
with the equivalent function calls:
hold('on')
format('rat')
legend('boxoff')

This way the code works the same way, and the syntax coloring in LaTeX is perfect.

Comment only
19 Apr 2009 TheFarmer

### TheFarmer (view profile)

Another thing I noticed is when you use ... to split an expression over several lines. In the matlab-editor they are blue, while they appear green in LaTeX.

This is just nitpicking, but it would still be nice!

Hope you can have a look at this issue as well, since I have no idea how to implement it myself.

Comment only
19 Apr 2009 TheFarmer

### TheFarmer (view profile)

Btw the lack of coloring also applies to other things, like
hold off
syms x y z
etc.

Comment only
18 Apr 2009 TheFarmer

### TheFarmer (view profile)

This worked excellent, and the code becomes very nice in LaTeX.

The strings are given the correct, purple color, but when you use the matlab command:
hold on
the 'on' is given the same purple color in the matlab editor, but not in the LaTeX file. Could you please take a look at that?

I would also recommend commenting out the delta sentence from your source code. I think the majority would prefer keeping their original variable names.

06 Apr 2009 Florian Knorn

### Florian Knorn (view profile)

Sorry Andrew, that feature would have to be provided by the listings package...

Comment only
31 Mar 2009 Andrew Wind

### Andrew Wind (view profile)

Excellent tool.

Does anybody have a fix for adding the cell (%%) formatting? I'd like the horizontal rule and the first line bolded as it appears in the editor.

Comment only
25 Mar 2009 Ariel Balter

### Ariel Balter (view profile)

I found out how to add labels, captions:

\lstinputlisting[label=lst:mylable, caption={\mcode{mycode.m}}]{mycode.m}

I am still having trouble with a "p" float. The code runs off the bottom of the page and does not continue. Is there a switch somewhere to help with that?

Comment only
25 Mar 2009 Ariel Balter

### Ariel Balter (view profile)

Thanks so much for making this! Found a small bug -- after using \lstinputlisting{...}, the environment \texttt{...} doesn't work anymore. I overcame it by using \mcode{...} because that was sufficient for what I needed. But perhaps someone can fix the problem.

Does anyone know of a way to have a \label to be able to \ref to a listing, a title for the listing such as:
Listing 1.
....
and a caption?

Thanks, Ariel

27 Feb 2009 Thomas

### Thomas (view profile)

Hi there,
I like this package. However, I use a lot of apostrophes like [lpfb,lpfa]=butter(6,100/fn,'low') in my m-file. If I use normal documentclasses such as book, article or report I do not have any problems in compiling.
BUT, when I am using the documentclass "beamerclass", I cannot include any m-files, if there are any apostrophe (') in my m-file. After removing all apostrophes latex can integrate my m-file.
Do you have the same problem? Does anybody know, how to solve this problem.

I used a simple example:
butter.m containing: [lpfb,lpfa]=butter(6,100/fn,'low')

tex-file:
\documentclass{beamer}
\usepackage[framed,numbered]{mcode}
\begin{document}

\frame{
\lstinputlisting{matlab/butter.m}
}
\end{document}

Help appreciated!
Cheers, Thomas

Comment only
16 Feb 2009 michele pace

### michele pace (view profile)

great work, it saved me a lot of time. Thank you.

Comment only
12 Feb 2009 Stephen Stegall

### Stephen Stegall (view profile)

Beautiful Work. Thank you.

07 Feb 2009 Dennis Birchenough

### Dennis Birchenough (view profile)

07 Feb 2009 Dennis Birchenough

### Dennis Birchenough (view profile)

Nevermind...I just found the "literate" section and commented out the {delta} example. :) Great program!

Comment only
07 Feb 2009 Dennis Birchenough

### Dennis Birchenough (view profile)

I have a parameter called "Ndelta". This program converts everything properly, except for all instances where this parameter is, it shortens it to 2 characters: N and the symbol for Delta. In other words, instead of spelling out delta, it gives the symbol Delta. Otherwise, fantastic program! I love it!!

Comment only
17 Jan 2009 Florian Knorn

### Florian Knorn (view profile)

Thanks for pointing that out, Roland.

As I'm getting a lot of requests as to how to actually use the package, I've now thrown together a small demo-document --- I guess an example says more than a 1000 words ;-)

=> http://www.knorn.org/misc_files/mcode_demo.zip

Comment only
28 Oct 2008 David Legland

### David Legland (view profile)

22 Oct 2008 Roland Kaufmann

A note on its usage: If you save your file in UTF-8 format (default on many Linux systems), then escape-to-LaTeX using the section sign will not work properly. You'll have to use inputenc with e.g. latin1 option in LaTeX and then make sure your files are saved in that charset (%%% coding: latin-1; local variable in Emacs for instance).

Comment only
17 Oct 2008 Jason Moyle

Great. Just what I needed. Easy to use and saved me heaps of time when writing my thesis.
THANKS

14 Oct 2008 Dan Barker

Works great, thanks alot :)

Just a couple of suggestions, (i) words that come after a command that aren't in parenthesis should be highlighted purple, e.g. in "hold off", the off would be purple. (ii) is it possible to add a nice heading that says what the file name is or something?

24 Sep 2008 Al Makedonskiy

AWESOME

29 Aug 2008 Nikola Toljic

Thanks.

24 Jul 2008 Bill McKeeman

Lovely. I had a lot of trouble just getting the dot-sty file into usable shape. After the download I had an email message wrapped around an HMTL file inside of which was a tab-infested STY file with many =D3 and other character renamings. The text spanned 150 columns. It took about an hour to comb all the gratuitous shrek out so I could use it. Thanks. /s/ Bill

17 Jul 2008 Val Schmidt

This works great, except it doesn't respect a \begin{singlespace} directives. Any idea how to include matlab code into a double spaced document, but have the code itself come out single spaced?
Thanks, Val

16 Jun 2008 Alessandro Farini

Very good! Easy to use and very useful

08 May 2008 Mira Mitra
30 Apr 2008 Raynor Vliegendhart

It works great. Only one slight problem though. It doesn't really like dollar signs in comments. (But that can be circumvented by escaping to LaTeX and then escape the dollar sign).

10 Apr 2008 Michel de Jong

Thank you very much. Looks great in my reports ;-)

20 Mar 2008 Ferry K

Great Work! Works like a charm.

08 Mar 2008 Nash Borges

amazing! i was looking everywhere for something simple to include some matlab code into latex and this is perfect!

28 Dec 2007 Florian Knorn

Of course it is, use the \lstinputlisting command... Glad you find it useful :-)

Comment only
27 Dec 2007 Long Chen

Great. You can dig into sty file to change the font and spacing. I like the  in comments a lot.

Is it possible to link to m file directly in latex? Then the comments will be automatically updated if I change the m file.

03 Dec 2007 Florian Knorn

@ Emma: Of course it is, look at the line with the comment "% use font and smaller size"...

Comment only
02 Dec 2007 emma thompson

This was great!! Perfect for my thesis! But is it possible to change the font size??

11 Nov 2007 Florin Stoican

just what I needed

29 Aug 2007 Florian Knorn

@ Sara: just put it into the same path as your tex file. Other than that, do some more reading ;-) Good Luck!

Comment only
26 Aug 2007 Sara Bernardez

Could anybody tell me how to install or whatever this .sty file, please?I'm new with latex and matlab.I get how to use it but I need Latex to reconise the file.Thanks.

Comment only
24 Jul 2007 faraz mirzaei

Thanks. This is great. You just need to put your code between:
\begin{lstlisting}
\end{lstlisting}

or use \mcode{YOUR Code} for inline use.

This is very good and convenient.

22 Jun 2007 fernando falconi

Thanks Florian ..your program is the best!!!

Comment only
14 Jun 2007 Micke M

Just put the file in the same place as the tex-file and it works great. Also keep track of how many caracters that fits in a line...

10 Jun 2007 sebastian swose

working well. thank you a lot!!

09 May 2007 Micke Malmström

Where do you put the file and how do you use it?

Comment only
01 Dec 2006 Julia N

So useful for my thesis - thanks a lot, Florian! My modification is to include both bold/italics AND colors for bw printing of color document. Achieved by upgrading 'else' statement in \bw to have these commands:
keywordstyle=\bfseries\color[rgb]{0,0,1},

16 Nov 2006 Someone Somewhere

I've discovered that the figures in the document will disappear when using
\includepackage{color}
and
\includepackage{doublespace}
at the same time

16 Nov 2006 Someone Somewhere

Use of this package causes all figures in the document to disappear. Anyone know what's up?

02 Aug 2006 Rafael Fuenzalida

Great package for latex!!!!!

19 Oct 2005 Florian Knorn

Thanks Mat Odijk for the tip! I shall mention it in the preamble ;-)

@ User with no name: As I have very little background info on your situation, there's little I can do. But the problem might be that to large mcode-blocks mess up your float-placement. So I would suggest you try to make the mcode-blocks float aswell (there is an option for that in the listings package).

Comment only
18 Oct 2005 not given

Worked well making the matlab look cool but screwed up the pictures the whole way through the document. Probably my lack of Latexing skills but I couldn't work out how to fix this.

Comment only
17 Oct 2005 Mat Odijk

For people who want to insert matlab code files (.m) directly use:
\lstinputlisting{your-file.m}
instead of the command's given in mcode.sty

26 Sep 2005 Daniel Ennis

Excellent. Worked as advertised. Thanks for putting this together.

22 Aug 2005 Doris Jimenez
17 Nov 2005

added a setting to make the appearance cleaner, and also added a little tip by one of the users.

01 Dec 2008 1.1

fixed misconfiguration, further tidied up source code and comments

17 Jan 2009 1.2

I've added the link to a demo-document that shows how to use the package.

04 Mar 2009 1.3

A user pointed out a problem when this package is used together with the beamer package, which is now fixed.

14 Jan 2010 1.4

Tweaked some descriptions, added feature to distinguish formatting between "end" and "end" to either end a clause or access the last element in a vector, updated demo document

01 Feb 2010 1.6

Added MATLAB block-quote support (which a user kindly pointed out to me): %{ block comment %}

21 Mar 2010 1.7

Fixed typo in documentation regarding §...§

04 May 2010 1.9

For some reason, the listings package replaces the normal "minus" by a "math minus", which causes problems when you want to use the source code from the final pdf in an application again . . .

15 Mar 2011 1.11

Now supports automatic line breaking. Added two new options, and now included the demo document in the download.

11 Aug 2011 1.13

Finally "end" and "end" are automatically told apart and coloured correctly.

26 Aug 2011 1.14

Small bugfix

31 Aug 2012 1.15

Added more keywords for syntax highlighting.

05 Nov 2013 1.16

Added the \mcodefn{} function for placing inline code in footnotes.

06 Mar 2014 1.17

Renamed internal variables in order to prevent conflicts with other packages.

16 Apr 2014 1.18

Added support for µ, get a nicer looking tilde character, included Stefan Karlsson's fix for the minus character issue (thanks!!).