3

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.

bcoss
  • 183
  • 7
    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 00:48
  • 2
    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 00:51
  • I've narrowed it down to this particular snippet. This piece shrinks the font line spacing in the float captions: http://pastebin.com/C5mXNhVZ – bcoss Sep 13 '11 at 00:56
  • Its far more complicated than that snippet...!! arggg. I can give a working example but it will include most of the dissertation.sty file and a good chunk of the main document. When I attempt to reconstruct the problem it goes away. Something very subtle is going on here! – bcoss Sep 13 '11 at 01:21
  • 2
    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 01:42
  • Thanks for the suggestion. I can confirm that listings does work, and was considering rewriting my own custom mcode from the listing package and mcode.sty.

    Its not any specific figure thats causing the problem, any figure with a caption will cause the glitch. As far as I can tell its a problem with the caption spacing, the snippet above is a tweak to reset baselinestrech inside footnotes and figure captions to 1. I still can not create a MWE, but will upload the whole dissertation.sty and small exmaple.tex to illustrate.

    – bcoss Sep 13 '11 at 18:58
  • I have put a link to a "MWE" above. Its not very minimal but its a working example. Sorry I wasn't able to pin it down more precisely, but I am still new to Latex. Thanks for any suggestions. – bcoss Sep 13 '11 at 19:16
  • 3
    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. – jub0bs Feb 06 '14 at 01:58
  • Even zip file link is broken. bcoss: I am voting to close now, Please update the Q by including MWE with details inside Q and making it standalone, if you wish it could be opened again. – texenthusiast Feb 06 '14 at 03:27
  • @texenthusiast The OP hasn't visited the site since Nov. 11 :s S/he seemed to have already solved the problem, anyway. I think it's fair to close the question... – jub0bs Feb 06 '14 at 03:28

1 Answers1

4

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
       \multiply\@tempcnta\@xxxii\advance\@tempcnta\sixt@@n
       \@tfor \@tempa :=#2\do
                        {\if\@tempa h\advance\@tempcnta \@ne\fi
                         \if\@tempa t\advance\@tempcnta \tw@\fi
                         \if\@tempa b\advance\@tempcnta 4\relax\fi
                         \if\@tempa p\advance\@tempcnta 8\relax\fi
         }\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
%       \multiply\@tempcnta\@xxxii\advance\@tempcnta\sixt@@n
%       \@tfor \@tempa :=#2\do
%                        {\if\@tempa h\advance\@tempcnta \@ne\fi
%                         \if\@tempa t\advance\@tempcnta \tw@\fi
%                         \if\@tempa b\advance\@tempcnta 4\relax\fi
%                         \if\@tempa p\advance\@tempcnta 8\relax\fi
%         }\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!

Torbjørn T.
  • 206,688
bcoss
  • 183