This was a quick project started and finished at a Super Happy Dev House. The gist is, that I (used) to do a bunch of problem sets which required a combination of computation and explanation. If I can do something on a computer, I will, so I end up writing most of my problem sets in LaTeX. I thought it would be great if I could combine the two into one process, minimizing the amount of copying and pasting required.

PHP's inlining into HTML is quite convenient for quick and dirty jobs (as much as I hate actually coding in PHP), so I thought I would come up with a similar scheme for Matlab and LaTeX and MatTex was born. Here's a contrived, but illustrative example:

\documentclass{article}
\begin{document}
    Hello World.  Recently I found out that 1 + 1 =
<?ml
1 + 1
?>
\end{document}

MatTex Example 1

A particularly handy function in Matlab is “latex”, which formats symbolic expressions in LaTeX. One example of this is printing a matrix:

\documentclass{article}
\begin{document}
    \begin{displaymath}
<?ml
latex(sym([1,2,3,4;5,6,7,8;9,10,11,12]))
?>
    \end{displaymath}
\end{document}

Matrix Example

For anyone who’s done matrices by hand in LaTeX, this is a lot less painful.

It's all quite a hack - It’s more or less a bash script which runs some perl regular expressions to parse out the Matlab, runs the Matlab, uses python to paste the Matlab back into the LaTeX (I would use perl, but my knowledge does not extend beyond regex one-liners), then runs your LaTeX distro of choice (I use TeXShop on my MacBook).

If you want to take a look yourself, I’ve put the code up at github under an MIT License. A word of warning, I coded it amidst heavy socializing all afternoon, so its not exactly my best work. At the moment it requires that the ‘<?ml’ and ‘?>’ reside on their own lines. You’re welcome to fix it though and commit said fix to github!

The syntax is as follows:

./mattex.sh [your tex file]