1

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.

enter image description here

  • you can see http://tex.stackexchange.com/questions/75116/what-can-i-use-to-typeset-matlab-code-in-my-document – 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 (oroutput.dat,output.csvetc) and then usepgfplotstableorcsvsimpleto import them. On the other hand, you can use matlab'spublish` feature too. –  Feb 01 '15 at 00:46

2 Answers2

4
\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}

enter image description here

Mike Renfro
  • 20,550
2

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}
darthbith
  • 7,384
Ger
  • 187