I am trying to do the following:
-
read a csv (or other similar format) external file with numerical and string data into an object;
-
reference/get an specific value from such object to use into an specific cell of a table or in a figure.
That means, I do not want to convert a csv file or such into a table. I do not want to use packages to build a table.
So, for instance, suppose that I have the following code to generate a very simple table:
\documentclass[]{article}
\begin{document}
\begin{table}[]
\centering
\caption{My example}
\begin{tabular}{lllll}
1 & 2 & 3 & 4 & 5 \\
6 & 7 & 8 & xyz & 9 \\
10 & 11 & 12 & 13 & 14
\end{tabular}
\end{table}
\end{document}
Where it is written "xyz", I want to actually write a command that references/gets the value of a specific row/column of a csv or similar file. Is there a way to accomplish such a thing?
Best Answer
This uses
lua
function call to read the specific field in the CVS file. For example, given this CVS file in the directoryThen to insert, say the field located at row 2 and column 5, which is
-299217.14285715
, and the field at row 1 and column 3, which isPoty city
in the Latex table, then the code isWhich generates the table:
The full code is below. I googled lua code to parse CVS files, there are few on the net, I found one that worked and used it. There does not seem to be a build in API in lua itself for this. The CVS file is read automatically. The cvs file name is hardcoded in the code below. The latex file needs to be compiled with
lualatex
notpdflatex
Update
I figured why stop at just reading field? Why not add a Latex command to load the whole CVS file and convert it to a Latex table? This will be much more useful also. So here is a new implementation that loads a CVS file and makes a Latex table, all automatically. This will be useful for making reports and such.
The Latex command is called
getCVS{"fileName"}
and used as followsUsing the above CVS file, the table generated is
The full code is below