# MATLAB output into tex [duplicate]

Is there any way to import raw MATLAB output into .tex documents?

I have already seen verbatim package as a solution, but it does not meet my demands for 2 reasons:

1. Matrix elements are not aligned, especially when some elements have decimal digits and others don't.
2. It is not quite convenient, in case MATLAB generates a large output.

Also, I have tested matrix2latex.m but it seems useful only for matrices.

As I said before, it would be convenient to me if I could just import the raw MATLAB output into my .tex file, keeping the format as the one in MATLAB command window.

Thank you!

Edit: Added output generated by an .m script.

## marked as duplicate by percusse, Zarko, Romain Picot, Svend Tveskæg, Paul GaboritMay 18 '16 at 15:12

• you can see tex.stackexchange.com/questions/75116/… – touhami Jan 31 '15 at 19:13
• In case I am not quite clear, I don't want to include any MATLAB code, i.e. the content of any .m file. To be more specific, I want to import only the output generated by the .m script in my .tex file. – thanasissdr Jan 31 '15 at 19:23
• If you use a monospaced font (like Courier) in your command window, and if you also use a monospaced font (like \ttfamily) in your document with a verbatim or similar environment, everything should line up consistently. If you're having some other problem, please construct a MWE with the smallest MATLAB output that shows the problem. – Mike Renfro Jan 31 '15 at 19:37
• If it is only the output and you don't want them as is but in latex format, save the output as say output.txt (or output.dat ,output.csv etc) and then use pgfplotstable or csvsimple to import them. On the other hand, you can use matlab's publish feature too. – user11232 Feb 1 '15 at 0:46

\documentclass[10pt]{article}
\begin{filecontents*}{\jobname.txt}
A =

0.6555    0.7060    0.2769    0.0971    0.6948    0.9502    0.4387
0.1712    0.0318    0.0462    0.8235    0.3171    0.0344    0.3816

v =

0.7655    0.7952    0.1869    0.4898    0.4456    0.6463    0.7094

blah blah blah
0.7547    0.6551
0.2760    0.1626
0.6797    0.1190
\end{filecontents*}
\usepackage{verbatim} % http://ctan.org/pkg/verbatim
\begin{document}
In a simple document, the code
{\small \begin{verbatim}
A=rand(2,7), v=rand(1,7), fprintf('blah blah blah\n'); disp(rand(3,2));
\end{verbatim}}
printed the following to the screen:
% Without the verbatim package, you may need to add a \noindent between
% and after verbatim enviironments to prevent unwanted paragraph indentation.
{\small \begin{verbatim}
A =

0.6555    0.7060    0.2769    0.0971    0.6948    0.9502    0.4387
0.1712    0.0318    0.0462    0.8235    0.3171    0.0344    0.3816

v =

0.7655    0.7952    0.1869    0.4898    0.4456    0.6463    0.7094

blah blah blah
0.7547    0.6551
0.2760    0.1626
0.6797    0.1190
\end{verbatim}}
Alternatively, if the screen output were written to a file, and if the verbatim
package were added, it could be inserted as:
\verbatiminput{\jobname.txt}
\end{document}


• Although I think it is useful (that's the reason I upvoted and accepted the answer), I was wondering if there is a more automated - elegant way (e.g. \include or sth), rather than copy - paste the data. – thanasissdr Feb 1 '15 at 6:36
• In that case, try fancyvrb and verbatiminput. – Mike Renfro Feb 1 '15 at 12:09
• Or the verbatim package. I've edited the answer accordingly. – Mike Renfro Feb 1 '15 at 14:00

I know it's quite late for replies, but I will like to suggest another solution:

Using the command diary in Matlab you can export all the command windows print to a specific file and later use one part of @Mike Renfro to import that txt file. I never try that, but it should do the job, and maybe more "elegant", in the meaning you dont have to write the output in the latex file.

%In Matlab script
diary('commandoutput.txt');
diary on ;
enter code here you want to evaluate
diary off ;%to avoid print other commands.

%In latex file (from @Mike Renfro solution)
\verbatiminput{\jobname.txt}
`