TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

migrated from stackoverflow.com Oct 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
up vote 7 down vote accepted

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:


Here is the graph of an ellipse:


For this test, I used the following as the publishedMfile (which is obviously not from Matlib) file:



\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$};

\draw [red, ultra thick]% Graph Ellipse
    (EllipseOrigin) ellipse [x radius=\XRadius,y radius=\YRadius];

share|improve this answer
Thanks, it looks like the standalone package works well for this. – ruslank Oct 15 '11 at 1:13

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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.