[texhax] Tables from Files

Uwe Lück uwe.lueck at web.de
Tue Jul 11 22:45:26 CEST 2006


At 09:30 11.07.06, Ulf Markwardt wrote:

>I am looking for a package that makes it possible to directly input data from
>a file into a table like \tabularfromfile{filename.csv}[formatoptions].
>
>Currently I am using a simple Perl script to generate \begin{tabular}...
>\end{tabular} from a CSV table, put this into a file, and use the \input
>command - but this is *far* from elegance.

OK, I have read the thread (Martin, Steve) until 17:44 (German time).
They seem to overlook that Ulf already has a very elegant solution
but still wants a more elegant one. He uses applications X and TeX
and will not be satisfied by the proposal to use Y, ... TeX instead.
He wants to use /one/ application TeX, with a TeX macro package.

I do not know such a macro package, but it should be very very
simple to make one. Note that the TeX primitive \read reads
one line of an input file, and note TeX's parsing abilities according
to TeXbook pp. 202..., e.g., that have been exercised several
times on this list in other threads. At my few touches with
computer science, trying some Pascal, I was surprised to see
that TeX's parsing really is great in processing input data lists.
(Admittedly, I have not studied what certain libraries provide.)

Assuming one certain format of input lines, you may open
a (LaTeX) table environment and then execute a loop as follows ...

   \loop \ifeof... \else
     \read... to\readLine
     \expandafter \makeTableRow \readLine \endReadLine
   \repeat

Assuming, e.g., that each row has 9 entries separated by
one blank space each time, use the following definition
of \makeTableRow in the preamble:

   \def\makeTableRow #1 #2 #3 #4 #5 #6 #7 #8 #9\endReadline{%
     #1&#2&#3&#4&#5&#6&#7&#8&#9\\}

Of course, the opening of the table must declare what to do
with 9 table entries in one row (alignment left--right--centered...).

And of course the input file may have "exceptional" (e.g.
comment) lines --- that \makeTableRow should be able to
recognize and account for.

It may also be interesting here that some package having
been mentioned on this list recently (perltex or so)
emulates Perl under TeX.

Hope this helps,

Uwe.




More information about the texhax mailing list