[Tex/LaTex] Adding styling to LaTeX produced by MATLAB

includeMATLAB

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.

Best Answer

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