# Include MATLAB code via package mcode [closed]

I am trying to include MATLAB code in my thesis using a very handy package called mcode.sty, http://www.mathworks.com/matlabcentral/fileexchange/8015-m-code-latex-package

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}.


Please try it with this MWE I have created, you can download it from my dropbox here: http://dl.dropbox.com/u/6275106/MWE.zip

In the dissertation-bec-mwe.sty file is a section I have identified as related to the problem. Simply commenting the section on lines 783-819 allows the document to compile, although my figure caption spacings are then doubled.

EDIT: I have discovered that the xcolor package conflicting with the definition made for xfloat in the sty file to be the root cause. Xcolor is required by mcode, enabling only this package without the mcode package results in the above behavior. This is why the plain listings package works but not mcode. Still trying to figure out how to get the two (xfloat and xcolor) to play nice.

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.

-

## closed as off-topic by Jubobs, Jesse, texenthusiast, Werner, cmhughesFeb 6 at 4:55

• This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

May I suggest you turn your code sample into a full compilable minimal example? That makes it easier for others to help you. Also, you should try to come up with a more descriptive question title - this one doesn't really help to get the right people to read the question. –  Jake Sep 12 '11 at 23:39
Thank you for your suggestions. The problem is too complicated I think to provide the minimum working example, but I am trying to narrow it down so I can provide something to chew on. –  bcoss Sep 13 '11 at 0:48
Without a full minimal working example (MWE) that illustrates your problem it is more or less impossible to help you. You need to do the ground work to narrow it down first. May I also suggest to simple switch to the listings package. It has m-code highlighting support. –  Martin Scharrer Sep 13 '11 at 0:51
The mcode package loads only listings and conditionally xcolor and textcomp. Consequently, it defines listings settings that are used to duplicate the traditional Matlab environment and even requires the user to use the lstlistings environment. If it is not possible to duplicate the problem, then switch to listings outright, and transfer the settings. Alternatively, at least provide your entire first figure causing this problem. –  Werner Sep 13 '11 at 1:42
This question appears to be off-topic because it doesn't contain a much needed MWE; the user does link some .zip file allegedly containing an MWE, but extracting zip files from untrusted sources is ill-advised. –  Jubobs Feb 6 at 1:58

So I solved the issue and wanted to document it in case anyone else has the same problem.

The key revelation came from this forum posting with a similar issue: http://www.latex-community.org/forum/viewtopic.php?f=45&t=4766&start=10

The issue stems from my dissertation.sty sheet, xcolor and xfloat. The .sty sheet defines a custom line spacing in float and footnotes, single, as opposed to the baseline of 2 using xfloat. It did so in a particularly bad way, it redefined xfloat globally as best as I can tell.

The solution was to preserve the kernel xfloat, and create a new xfloat called myxfloat for use in figures. This allows the spacing commands to work as intended while preserving compatibility with xcolor. Here is the original xfloat definition,

\def\@xfloat#1[#2]{\ifhmode \@bsphack\@floatpenalty -\@Mii\else
\@floatpenalty-\@Miii\fi\def\@captype{#1}\ifinner
\@parmoderr\@floatpenalty\z@
\else\@next\@currbox\@freelist{\@tempcnta\csname ftype@#1\endcsname
\@tfor \@tempa :=#2\do
}\global\count\@currbox\@tempcnta}\@fltovf\fi
\global\setbox\@currbox\vbox\bgroup
\def\baselinestretch{1}
\ifx\undefined\selectfont
\small\normalsize
\else
\@newbaseline
\fi
\boxmaxdepth\z@
\hsize\columnwidth \@parboxrestore}
\long\def\@footnotetext#1{\insert\footins{\def\baselinestretch{1}\footnotesize
\interlinepenalty\interfootnotelinepenalty
\splittopskip\footnotesep
\splitmaxdepth \dp\strutbox \floatingpenalty \@MM
\hsize\columnwidth \@parboxrestore
\edef\@currentlabel{\csname p@footnote\endcsname\@thefnmark}\@makefntext
{\rule{\z@}{\footnotesep}\ignorespaces
#1\strut}}}


And here is the new definition:

% Save LaTeX kernel version of \@xfloat
\makeatletter
\let\my@xfloat\@xfloat
\makeatother
% Create a modified copy of \@xfloat using the kernel definition
\makeatletter
\def\@xfloat#1[#2]{
\my@xfloat#1[#2]%
\def\baselinestretch{1}%
% I COMMENTED THIS PART BELOW.
%   \@normalsize \normalsize
%}
%\makeatother
%
%%%
%%% Reset baselinestretch within footnotes and floats. Originally stolen
%%% from Stanford thesis style.
%%%
%\def\@xfloat#1[#2]{\ifhmode \@bsphack\@floatpenalty -\@Mii\else
%   \@floatpenalty-\@Miii\fi\def\@captype{#1}\ifinner
%      \@parmoderr\@floatpenalty\z@
%    \else\@next\@currbox\@freelist{\@tempcnta\csname ftype@#1\endcsname
%       \@tfor \@tempa :=#2\do
%         }\global\count\@currbox\@tempcnta}\@fltovf\fi
%    \global\setbox\@currbox\vbox\bgroup
%    \def\baselinestretch{1}
%       OLD SPACING TWEAK CONTINUES BELOW
\ifx\undefined\selectfont
\small\normalsize
\else
\@newbaseline
\fi
\boxmaxdepth\z@
\hsize\columnwidth \@parboxrestore}
\long\def\@footnotetext#1{\insert\footins{\def\baselinestretch{1}\footnotesize
\interlinepenalty\interfootnotelinepenalty
\splittopskip\footnotesep
\splitmaxdepth \dp\strutbox \floatingpenalty \@MM
\hsize\columnwidth \@parboxrestore
\edef\@currentlabel{\csname p@footnote\endcsname\@thefnmark}\@makefntext
{\rule{\z@}{\footnotesep}\ignorespaces
#1\strut}}}


Hope this helps someone else out there, I know it sure had me puzzled for a few days. And god bless all the programming my physics undergrad had me do, I can't even understand TeX but I sure can mend together some random samples off the interwebs!

-