# Including published Matlab code and figures in separate LaTeX document

I'm writing up a homework set in LaTeX. I'm writing the theoretical parts of the homework using LaTeX, and the implementation in Matlab. I want to combine these two easily. My problem is:

How do you include the Matlab published .tex file in a separate LaTeX document?

I know LaTeX has packages which can import formatted Matlab code into a LaTeX document. You can also import Matlab-generated .eps figures. What I want to do is include the Matlab published .tex file (which includes the code and figures placed appropriately) using \include{publishedMfile.tex} in the middle of my LaTeX document. However, the published .tex file is designed to be standalone, so it has a preamble, which prevents me from including it.

Here's a sample Matlab published file: file.tex

-

## migrated from stackoverflow.comOct 15 '11 at 0:19

This question came from our site for professional and enthusiast programmers.

Welcome to TeX.SE. It would be helpful it you provide a sample .tex file produced by Matlab? I think what you want can be done with the standalone package, but would be good to be able to test it. –  Peter Grill Oct 15 '11 at 0:41
You might want to use the matlab-prettifier package; see this answer. –  Jubobs Apr 28 '14 at 15:27

You can simply copy the required packages and commands such as \usepackage{...} and \definecolor{lightgray}{gray}{0.5} or \setlength{\parindent}{0pt} etc. and put it into your documents preamble. After that there is no reason to not to include it in your document. Otherwise please elaborate your question further to narrow down the possibilities.

-
My goal is to use the autogenerated Matlab tex file and include it in my custom document. If I have to cut out the autogenerated preamble every time I change my Matlab code that would get tedious. –  ruslank Oct 15 '11 at 0:51

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);
\draw [blue, thin, ->] (-5,0) -- (5,0) node [right] {$x$};
\draw [blue, thin, ->] (0,-4) -- (0,4) node [above] {$y$};