Sourcecode Import – Beautifully Format Text of an External Document

importsourcecode

I have some formatted long text files looking like this:

f=50 k_max=420

 Iteration   Func-count     min f(x)         Procedure
     0            1      7.07212e-09         
     1            2      7.07212e-09         initial simplex
     2            4      7.06369e-09         reflect
     3            6      7.06369e-09         contract outside
     4            8      7.06369e-09         contract inside
     5           10      7.06367e-09         contract inside

Exiting: Maximum number of function evaluations has been exceeded
         - increase MaxFunEvals option.
         Current function value: 0.000000 

m=3.775
f=100 k_max=1009

 Iteration   Func-count     min f(x)         Procedure
     0            1      1.89961e-10         
     1            2      1.89961e-10         initial simplex
     2            4      1.33983e-10         expand
     3            6      8.33243e-11         expand
     4            8      7.98592e-11         contract outside
     5           10      7.98592e-11         contract inside

Exiting: Maximum number of function evaluations has been exceeded
         - increase MaxFunEvals option.
         Current function value: 0.000000 

These files are the command line of MATLAB which are saved using diary command.

Is there a way to import the source file and typeset it beautifully in LaTeX? I love to use minted package or something comparable with colors if possible here.

Best Answer

With the minted package, you can use

\inputminted[<options>]{<language>}{<file>}

Here's an example file code.tex using your sample file and saving it as Mat1.m:

\documentclass{article}
\usepackage{xcolor}
\usepackage{minted}

\begin{document}

\inputminted[bgcolor=gray!10]{matlab}{Mat1.m}

\end{document}

The output, after processing with pdflatex --shell-escape code.tex:

enter image description here

With the listings package, you can use

\lstinputlisting[<options>]{<file>}

A simple example, again with the previous settings

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\lstset{
  basicstyle=\ttfamily,
  backgroundcolor=\color{gray!10},
  keywordstyle=\color{green!40!black},
  columns=flexible
}

\begin{document}

\lstinputlisting[language=matlab]{Mat1.m}

\end{document}

The output:

enter image description here

Related Question