I am not sure exactly what the Matlib .tex file looks like, but from your description it is a complete .tex file that can be compiled by itself. If that is the case, you should be able to use the standalone
package to input that file into your LaTeX document as such. So, assuming that the file is called publishedMfile.tex, then your main file would just input it as such:
\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\begin{document}
Here is the graph of an ellipse:
\input{publishedMfile}
\end{document}
For this test, I used the following as the publishedMfile (which is obviously not from Matlib) file:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (EllipseOrigin) at (0,0);
\newcommand*{\XRadius}{4.0}
\newcommand*{\YRadius}{3.0}
\draw [blue, thin, ->] (-5,0) -- (5,0) node [right] {$x$};
\draw [blue, thin, ->] (0,-4) -- (0,4) node [above] {$y$};
\draw [red, ultra thick]% Graph Ellipse
(EllipseOrigin) ellipse [x radius=\XRadius,y radius=\YRadius];
\end{tikzpicture}
\end{document}
There are three ways of doing this:
Do the weaving at the application (Matlab, Mathematica, R) end. That is, the application should be aware of the TeX format and ignore everything other than \begin{code}
... \end{code}
snippets. This is how Sweave and literate haskell work.
Do the weaving at the TeX end, that is, let TeX call the external application (with appropriate switches) and then display the result. This the approach that the R
and gnuplot
modules in ConTeXt follow.
Use a general purpose literate programming tool, like noweb (or those targeted to a specific language like Ocamweb).
For the second approach, I have written a ConTeXt module, filter, that allows you to pass the content of a program to an external program and read back the results. For example, you can replicate the functionality of sweave using:
\usemodule[filter]
\defineexternalfilter
[R]
[filtercommand={R CMD BATCH -q --save --restore \externalfilterinputfile\space \externalfilteroutputfile},
output=\externalfilterbasefile.out,
readcommand=\typefile,
continue=yes]
Then, using
\startR
...
\stopR
will execute the resultant code using R
and show the output generated by R
. Using
\startR[read=no]
....
\stopR
will execute the code using R
but not show the output. The same approach will work for Matlab or Mathematica by replacing the filtercommand
by the appropriate call to Matlab/Mathematica. This approach can be used for other purposes as well
Best Answer
Background
There is no easy way to do this, but there are a 3 ways to hack it. To understand how to control the MATLAB LaTeX interpreter, you need to understand how it works. When MATLAB processes a LaTeX string it calls
tex.m
. The MATLABtex
function appendsand prepends
to the string. MATLAB then calls a closed source compiled function
texmex
which appears to call atex
binary. MATLAB comes with a very limited TeX installation, so even if you could load a package file, you would need to tell MATLAB where to look.MATLAB has a built in feature that lets you control where to look. You can control this with
where the variable
PackagePath
contains the full path to the package. You can do this from the MATLAB command line or in a script or function.3 ways
There are 3 ways that you can get MATLAB to load a package. Two require you to modify/overload MATLAB functions and are complete solutions. The final one doesn't require any modifications of MATLAB functions, but won't work in all cases.
The first way is to load a package via
\input
instead of\usepackage
. This is because you cannot use\usepackage
after\begin{document}
. This may not work and I don't know if you can use\input
inside an\hbox
.The second way is to modify
mwarticle.cls
to load the packages you want. This will load the package for all MATLAB LaTeX strings.The third way is to modify
tex.m
to conditionally load packages through a mechanism likethis requires knowing MATLAB to make the changes and could theoretically break something. If you go this way, you may also want to modify
tex.m
to call your locallatex
binary and not the potentially out of date MATLAB one.