63

I am using a finite element program that outputs data in .txt files. And I want to input these data in my LaTeX code. Is there a way to input the data files so that they appear as the program output format?

Program output data file: enter image description here

LaTeX code:

\documentclass[a4paper,oneside,openany,article]{memoir}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[danish]{babel}
\usepackage{graphicx}
    \graphicspath{{Figures/}}
\usepackage{fullpage}
\usepackage{mathtools}

\begin{document}
\input{Data/shearwall_only_bc_A.txt}
\end{document}

LaTeX output:

enter image description here

4

3 Answers 3

41

In addition to listings, the fancyvrb package is equally suitable for this purpose.

This package provides the \VerbatimInput command (similar to \verbatiminput), which can be customised using various parameters (e.g., reducing font size).

\documentclass{article}

\usepackage[dvipsnames]{xcolor}

\usepackage{fancyvrb}

% redefine \VerbatimInput
\RecustomVerbatimCommand{\VerbatimInput}{VerbatimInput}%
{fontsize=\footnotesize,
 %
 frame=lines,  % top and bottom rule only
 framesep=2em, % separation between frame and text
 rulecolor=\color{Gray},
 %
 label=\fbox{\color{Black}data.txt},
 labelposition=topline,
 %
 commandchars=\|\(\), % escape character and argument delimiters for
                      % commands within the verbatim
 commentchar=*        % comment character
}

\begin{document}

\VerbatimInput{data.txt}

\end{document}

enter image description here

Notes:

  • specifying | and (/) as the escape character and argument delimiters means these symbols cannot appear as part of the verbatim text (or in this case, the contents of data.txt);
  • the line of asterisks in data.txt was removed by specifying * as the comment character (similar to % in LaTeX);
  • the line highlighted in green was obtained by modifying it as follows:

(|color(Green) 8 400.519E-33 -12. 17.1429 27.3494E+03)

3
  • To overwrite the value of previous parameters at any point, use the optional argument of \VerbatimInput, e.g., \VerbatimInput[fontsize=\small]{data.txt}.
    – nnunes
    Dec 2, 2012 at 15:39
  • 1
    if filename is variable then how I do that? I want to show the filename from which I load this.
    – alhelal
    Jul 28, 2019 at 12:23
  • @alhelal According to @nnunes you can change parameters of the verbatim input later on between the \VerbatimInput and the {<filename>.txt}. Accordingly, you can change the file name that is shown on top with: \VerbatimInput[ label=\fbox{\color{Black}somefilenameorganogram.txt}]{../../code/hw2/Diagrams/organogram.txt}. (where most likely somefilenameorganogram.txt would just be the same as the name of the file your trying to display, i.e. organogram.txt.)
    – a.t.
    Jul 19, 2020 at 14:04
37

Thanks to egreg.

Adding

\usepackage{verbatim}

to the preamble, and using

\verbatiminput{<file path>}

to input, does the tick!

2
  • 2
    And the font size can be controlled by just surrounding \verbatiminput like you normally would: {\small \verbatiminput}. So this works out perfectly fine : ) Jun 20, 2018 at 9:07
  • After using \usepackage{verbatim}, I had to use the following capitalization \VerbatimInput{<file path>} to make it work for me.
    – AIpeter
    Aug 14, 2019 at 11:51
3

You can also make the font size smaller in a verbatim environment by making a new environment I call \verbatimprog, which uses the \footnotesize font size.

Place the lines below in the preamble (before \begin{document}):

 \DefineVerbatimEnvironment%
      {verbatimprog}%
      {Verbatim}%
      {fontsize=\footnotesize}%

Then, to use the new environment, say

\begin{verbatimprog}

(your program output here)

\end{verbatimprog}
1
  • 1
    Yes, but Malthe Eisum's question was about reading the contents of a text file automatically with a command like \input, and not copying the contents of the first.
    – nnunes
    Dec 2, 2012 at 18:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .