Another file question from Latex; this time I'm trying to read in data from a single text file and put it directly into a table.
The file reads something like:
something | data | stuff | bacon
something else | data | different stuff | more bacon
3rd | 3rd data | you get the idea | bacon
To do this I could just use a simple \input but then I'd have to have all the table setup inside the text file. Since I'm not the one going to make the .txt files I'd prefer it stay nice and simple.
My solution was to use expl3 and xparse. However I'm having a rough time figuring out how to make Latex spit out the file into a table. The closest I've come uses the following code and makes something like the picture.
This code I did not make myself. I found it from Arbitrary text parsing from a separate file via egreg: https://tex.stackexchange.com/users/4427/egreg . His response there was very helpful and I looked into expl3 and am beginning to get a better understanding. Unfortunately I don't understand this code too much; merely the syntax (still a bit shaky there too). My general understanding is that it makes a function within a function, etc. And then reads in a file. This code will read all the lines of a file, and I'm not sure how that loops or if it's recursive. I also have no idea what \taylor means. (Google was helpful in figuring most of it out, but no luck finding \taylor).
Any explanations of the code would also be much appreciated.
\NewDocumentCommand{\readdata}{O{|} m}
{
\readdata:nn { #1 } { #2 }%
}
\cs_new_protected:Npn \readdata:nn #1 #2%
{
\ior_open:Nn \g__read_ior { #2 }%
\ior_map_inline:Nn \g__read_ior%
{%
\__process:nn { #1 } { ##1 }%
}%
\ior_close:N \g__read_ior%
}
\cs_new_protected:Npn \__process:nn #1 #2%
{
\seq_set_split:Nnn \l__line_seq { #1 } { #2 }%
\use:x%
{%
\exp_not:N \DataEntry%
\seq_map_function:NN \l__line_seq \__brace:n%
}%
}
\cs_new:Npn \__brace:n #1 { { #1 } }%
\ExplSyntaxOff
\newcommand{\DataEntry}[4] % Predetermined number of columns for data
{
#1 & #2 & #3 & #4 \\%
\hline
}
...later...
\begin{table}[ht]
\head{\caption{Results}}% title of Table
\centering
\begin{tabular}{|p{2.5cm}|p{2.5cm}|p{2.5cm}|p{3.5cm}|}
I'd prefer to use |c| to have them centered, but that doesn't seem to work yet...
\hline\hline
Sample \# & Something & Results & Description \\ [0.5ex] % inserts table headings
\hline
\readdata[|]{data6.txt}
\label{table:nonlin} % is used to refer this table in the text
\end{tabular}
\end{table}
Best Answer
The problem is that you're trying to add more lines to a tabular, but this can't be done with a loop starting in one cell and ending in another one. The loop must be performed completely, by storing the various table rows in a token list variable and eventually delivering this one.
I've used
filecontents*
to keep the example selfcontained. I also usedbooktabs
to get a better table.