I'm trying to put in my thesis matlab code but the results are not very good. I would get something like that but I don't know what packages using and how to create this result:
[Tex/LaTex] How to get Matlab code into a LaTeX document
MATLABsourcecode
Related Solutions
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
andgnuplot
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
I personally prefer the
minted
package. It's a little trouble to set up by the output is very neat and tidy -- and it has syntax highlighting.Output:
Code:
You'll have to call
pdflatex
with--shell-escape
though and you will have to install a package that providespygmentize
command.