As mentioned in zuggg's comment, you can convert MATLAB figures to TikZ by using the matlab2tikz
script. What this does is collect the data points and then uses the PGFPlots \addplot table
to recreate the plot. The author wrote the script so that it also works in GNU Octave, but the MATLAB Central license does not allow use of the shared software in non-MathWorks products.
Octave, on the other hand, has built-in export capabilities via the print -dtikz
command. When using the FLTK graphics toolkit, it will generate lower-level PGF commands, and when using Gnuplot, it will generate higher-level TikZ commands. In both cases, the plot is built from basic graphical building blocks (tiny rectangles in some cases) and look inferior to the PGFPlots approach, especially when zoomed in.
Another advantage to the matlab2tikz
script is that it provides key-value arguments which allow you to do this:
matlab2tikz('width', '.8\textwidth')
This also shows that you can specify the plot size in terms of TeX sizes. I didn't find a way to do this with the Octave print
command, and the resulting figures were either too small or too big.
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'...
))
Best Answer
You pretty much have already outlined the work flow. I am assuming you mean the output that appears in the command window.
\input
command, although, I would recommand treating the MATLAB generated text no different as you would treat source code. There is plenty of choice for including source files into latex, e.g. the listings package.Here is a minimal example. Place both files in the same directory, run the MATLAB script - works also with octave - and then run latex.
You may notice, that only the output generated between
diary on
anddiary off
ends up in the log-file, which is later included into the latex document. This gives you control over what portions of the generated output to put into your document.Make MATLAB update the latex file:
If you add e.g.
system('latexmk --pdf matlabLog')
to the end of your MATLAB script, then MATLAB will execute the command specified in the string, which is passed to thesystem
command. In this example I use thelatexmk
command, can be found here. Alternatively, you could also execute a shell script or any other build magic.Here is some minimal MATLAB code:
And some minimal latex code: