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.
\begin{filecontents*}{\jobname-data.txt}
something | data | stuff | bacon
something else | data | different stuff | more bacon
3rd | 3rd data | you get the idea | bacon
\end{filecontents*}
\documentclass{article}
\usepackage{xparse,booktabs}
\ExplSyntaxOn
\NewDocumentCommand{\readdata}{O{|} m}
{
\mhag_readdata:nn { #1 } { #2 }
}
\NewDocumentCommand{\DataEntry}{m m m m}% Predetermined number of columns for data
{
#1 & #2 & #3 & #4 \\
}
\ior_new:N \g__mhag_read_stream % an input stream
\seq_new:N \l__mhag_line_seq % a temporary sequence for processing a row
\tl_new:N \l__mhag_body_tl % a container for the table body
\cs_new_protected:Npn \mhag_readdata:nn #1 #2
{
% clear the table body
\tl_clear:N \l__mhag_body_tl
% open the input stream
\ior_open:Nn \g__mhag_read_stream { #2 }
% loop on the file lines
\ior_map_inline:Nn \g__mhag_read_stream
{% process the current line
\__mhag_process:nn { #1 } { ##1 }
}
% the loop has ended, close the stream
\ior_close:N \g__mhag_read_stream
% deliver the table body
\tl_use:N \l__mhag_body_tl
}
\cs_new_protected:Npn \__mhag_process:nn #1 #2
{% split the line at | (or the character specified in the optional argument
\seq_set_split:Nnn \l__mhag_line_seq { #1 } { #2 }
% add a line to the table body
\tl_put_right:Nx \l__mhag_body_tl
{% this will put a in \l__mhag_body_tl the tokens
% \DataEntry{<item 1>}{<item 2>}{<item 3>}{<item 4>}
\DataEntry \seq_map_function:NN \l__mhag_line_seq \__mhag_brace:n
}
}
% the auxiliary function for bracing each item
\cs_new:Npn \__mhag_brace:n #1 { { #1 } }
\ExplSyntaxOff
\begin{document}
\begin{table}[ht]
\centering
\caption{Results}% title of Table
\label{table:nonlin} % is used to refer this table in the text
\medskip
\begin{tabular}{@{}p{2.5cm}p{2.5cm}p{2.5cm}p{2.5cm}@{}}
\toprule
Sample \# & Something & Results & Description \\ % inserts table headings
\midrule
\readdata[|]{\jobname-data.txt}
\bottomrule
\end{tabular}
\end{table}
\end{document}
I've used filecontents*
to keep the example selfcontained. I also used booktabs
to get a better table.
Here's a solution based on Arianna's link :
\documentclass[2pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning,calc}
\colorlet{lightgray}{gray!20}
\begin{document}
\begin{tikzpicture}[relation/.style={rectangle split, rectangle split parts=#1, rectangle split part align=base, draw, anchor=center, align=center, text height=3mm, text centered}]\hspace*{-0.3cm}
% RELATIONS
\node (countrytitle) {\textbf{COUNTRY}};
\node [relation=3, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of countrytitle.west, anchor=west] (country)
{\underline{ISO\_code}%
\nodepart{two} Name
\nodepart{three} Description};
\node [below=1.3cm of country.west, anchor=west] (awardtitle) {\textbf{AWARD}};
\node [relation=5, rectangle split horizontal, rectangle split part fill={lightgray!50}, below=0.6cm of awardtitle.west, anchor=west] (award)
{\underline{URI}%
\nodepart{two} Name
\nodepart{three} Description
\nodepart{four} Date
\nodepart{five} Country\_ISO\_code};
\node [below=1.1cm of award.west, anchor=west] (booktitle) {\textbf{BOOK}};
\node [relation=5, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of booktitle.west, anchor=west] (book)
{\underline{URI}%
\nodepart{two} Name
\nodepart{three} Description
\nodepart{four} First\_publication\_date
\nodepart{five} Original\_language};
\node [below=1.4cm of book.west, anchor=west] (genretitle) {\textbf{GENRE}};
\node [relation=3, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of genretitle.west, anchor=west] (genre)
{\underline{URI}%
\nodepart{two} Name
\nodepart{three} Description};
\node [below=1.5cm of genre.west, anchor=west] (ell1) {...};
\node [below=1.0cm of ell1.west, anchor=west] (winsawardtitle) {\textbf{WINS\_AWARD}};
\node [relation=3, rectangle split horizontal, rectangle split part fill={lightgray!50}, anchor=north west, below=0.6cm of winsawardtitle.west, anchor=west] (winsaward)
{\underline{Award\_URI}%
\nodepart{two} \underline{Book\_URI}
\nodepart{three} \underline{Genre\_URI}};
% FOREIGN KEYS
\draw[-latex] (award.five south) -- ++(0,-0.2) -| ($(award.five south) + (4,0)$) |- ($(country.one south) + (0.25,-0.50)$) -| ($(country.one south) + (0.25,0)$);
\draw[-latex] (winsaward.one south) -- ++(0,-0.4) -| ($(winsaward.one south) + (11,0)$) |- ($(award.one south) + (0,-0.35)$) -| ($(award.one south) + (0,0)$);
\draw[-latex] ($(winsaward.two south) + (0.00,0)$) |- ++(0,-0.60) -| ($(winsaward.two south) + (5,0)$) |- ($(book.one south) + (0.00,-0.40)$) -| ($(book.one south) + (0.00,0)$);
\draw[-latex] (winsaward.three south) -- ++(0,-0.2) -| ($(winsaward.three south) + (2.63,0)$) |- ($(genre.one south) + (-0.00,-0.35)$) -| ($(genre.one south) + (-0.0,0)$);
\end{tikzpicture}
\end{document}
Which gives :
Best Answer
Let's assume you want this output:
If you use LuaTeX, the most innovative way is to include the luasql bindings in your program.
First create a simple database (
createdb.txt
):and fill it with
sqlite3 luasql-test < createdb.txt
The next step is to create a simple LuaLaTeX document and read the file into the table:
(The example is taken from the luasql home page.)
I have skipped the worst part: compiling and installing. This is highly system dependent, but I will list some steps/pitfalls.
sqlite3.so
from luasql directly, because theluatex
binary misses some symbols. So you need to compile your own non-stripped luatex binary (./build.sh --nostrip
) and use that instead. It needs to be placed at the "original" version in the tex tree. If you have done that, you can usesqlite3.so
from the luasql project. The same might be true on other unix systems. See the entry 666 in the bug tracker.luasql
in your document directory and place the generated library there.