I have a list of ~1000 words in a text file. The words are listed in a text file "my_words.txt"
with no breaklines as follows:
word1, word2, word3, etc.
I would like to display these words in a figure/table with vertical alignment (and central alignment within each column), i.e.
word1 word2 word3 word4
word5 word6 word7 word7
word8
where either I specify the number of rows or columns, or LaTeX figures them out from the width that I specify for the table.
I thought the package datatool could help with this problem, but I can't get it to work. I tried:
\usepackage{datatool}
% ...
\begin{document}
% ...
\DTLsetseparator{,}
\DTLloaddb[noheader,keys={source,package,target,brokenpkg,impactset,brokensource}]{mytable}{full_concept_list.csv}
\begin{table}[htbp]
\centering
\DTLdisplaydb{mytable}
\caption{CSV Table test}
\end{table}
but I get the error:
Missing $ inserted
\DTLdisplaydb{mytable}
Why is datatool not working? Are there other tools more appropiate for this task?
Best Answer
Here's a way with
expl3
and Heiko Oberdiek'scatchfile
:The
\storefile
stores the contents of the file under a symbolic name (in this casewords
). Then\printtable
can print the table using the data.We first define an
expl3
equivalent to\CatchFileDef
for being able to generate variants for it. Then the\storefile
command is defined: it takes a symbolic name for the word list and the file name as arguments. It stores the entire file in a token list variable (underscores will be absorbed as printable characters).The
\printtable
gets the desired number of columns and the symbolic name of the list as arguments. It's, as usual, simply translated into a function call.The function splits the token list corresponding to the symbolic name at commas and does a mapping on the sequence thus obtained. At each step we increment a counter and add the word to a temporary token list; if the counter's value is not the number of columns we add also a
&
, otherwise we add\\
and reset the counter to zero.Finally, the entire list is printed inside a
tabular
. You might want to loadlongtable
and changetabular
intolongtable
if the word list is long.For choosing between "row" and "column" order, you can modify it like this: