7

MATLAB has a function called publish which has the option to export a script and its output as LaTeX. It looks quite nice, although there are a few additional things that I would like to do to the output. As I cannot figure out how to make MATLAB emit arbitrary LaTeX when it is publishing, the next best thing is to apply it after the fact.

MATLAB generates a complete file, ready to be fed to an interpreter. It has \documentclass{article} headers and everything; it looks much like the following:

% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}

\sloppy
\definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}

\begin{document}

% generated code here

\end{document}

Is there a way to include this file from another file that defines my own formatting?

As a simple example, I would like to inject \usepackage{fullpage} into its output.

As a last resort, I'm willing to involve sed or similar, but that's almost as bad as hacking the MATLAB generator, which is a solution I've seen recommended elsewhere on the web.

  • a solution I've seen recommended elsewhere on the web. I'm curious, could you add a link? – jub0bs Sep 7 '14 at 23:18
6

I too was unimpressed by the style of the m-code typeset generated by MATLAB's publish function. I wanted publish to use, instead of a verbatim environment, an lstlisting environment in conjunction with one of the listings styles defined in my matlab-prettifier package.

Therefore, as advised by Will in his answer, I created my own stylesheet, called matlab2latex_pretty.xsl, on the basis of the default stylesheet that MATLAB uses. For information, on my Mac, the latter is located at <matlab-installation-folder>/toolbox/matlab/codetools/private/mxdom2latex.xsl.

My stylesheet, along with installation instructions, is available on GitHub: Jubobs/pretty_publish. You could have a look at it, as a starting point, to see how you can modify the native stylesheet to suit your needs.


As an example, here is a screenshot of the final product of

publish('peaks',...                                                       
    struct(...
    'format','latex',...
    'stylesheet','matlab2latex_pretty.xsl'...
))

enter image description here

  • 1
    Very nice, thanks! This does seem like a good way to go. – thirtythreeforty Sep 17 '14 at 23:20
  • Thanks a lot for providing the style file, it is very helpful. I've made an addition to it on github that enables a TOC and multi-level headers. I hope it will prove useful to others. – Saaru Lindestøkke Feb 27 at 10:07
4

If by mean ‘hacking the MATLAB generator’ you mean copying their .xsl convertor and changing it for your application, I actually think that's the best approach in the long run with something in Matlab like:

publish(filename,...
  struct(...
  'format','latex',...
  'stylesheet','matlab2latex.xsl',...
  'evalCode',false...
  ));

E.g., for changing the verbatim environment to lstlisting.

When I looked at this once in the past (I'd like to work with someone to develop a more robust solution) I ended up stripping the output down to omit the preamble, etc., and simply \input-ing the generated LaTeX document in a ‘driver’ file. (This also had the advantage of being able to include multiple .m files into the one LaTeX document.)

An easier way of doing this is to load the docmute package; it allows you to \input a file such as generated LaTeX file from Matlab into another document. For example, you might write

\documentclass{article}
\usepackage{full page}
\usepackage{docmute}
\begin{document}
\title{My Matlab document}
% publish the m-file first:
\input{html/mymatlab.tex}
\end{document}

(untested).

  • docmute looks like exactly what I'm looking for. I'll give it a shot, thanks! A better publish command would be better in the long run though. I'll look into that too. – thirtythreeforty Aug 26 '14 at 16:17
  • 1
    See my answer. One question: what version of MATLAB are you using? Mine (R2013a) doesn't allow the brace style you're using in your code. More specifically, it won't allow me to close braces right after a line break, even if preceded by a line-continuation token. – jub0bs Sep 7 '14 at 14:30
  • @Jukobs — good answer! Regarding brace style, I've been using this sort of form for years and it still seems to work in a trial version of 2014b I have installed. So I'm a bit confused. Note there's no comma before the final ... ? – Will Robertson Sep 8 '14 at 2:29
  • 1
    @Jubobs Definitely! I have a love/hate relationship with the Matlab programming language. Since I was thinking about this yesterday, I noticed that you can have a spurious comma for concatenating using square brackets ['x',...'y',...'z',...] … which I don't understand. – Will Robertson Sep 8 '14 at 23:17
  • 1
    @WillRobertson Yet another inconsistency. I'm with you on the love/hate relationship :) I'm currently gathering my thoughts for a blog post on "my beef with MATLAB"... – jub0bs Sep 8 '14 at 23:23

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.