4

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.

rowman
  • 1,780

2 Answers2

6

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

Gonzalo Medina
  • 505,128
  • Is it possible to selectively set which words to be in color? Note that this is a text output so words like function or min should not be in color like MATLAB language. – rowman May 18 '13 at 16:00
  • @rowman Sure! Using the listings package, you can define your own settings. The package documentation has the details. I am not familiar enough with the minted package, so I wouldn't know how hard would be to do the same. – Gonzalo Medina May 18 '13 at 17:12
3

Here is a link to one of my questions that has the listing environment.

disregard the vbox warning stuff

Or you can use the verbatim package and it will look just like it is in-putted.

The listing environment is just a highspeed form of verbatim.

Here is what the code does but this is just an image of code formatting. Is this the general idea? You can always adjust colors.

enter image description here

Useful links:

This is python based but the ideas are the same:

how to highlight syntax

This one is more general:

code highlighting

dustin
  • 18,617
  • 23
  • 99
  • 204