2

Suppose I have a really long, really boring list of data in - let's say CSV, but converting to other formats is an option, when this can be done automatically - and I want to prepare a section in my LaTeX document using it as a data source.

csvautotabular isn't quite what I'm after, I'd like to be able to do it in a custom way - something like:

foo,bar,baz;
zip,boom,bang;

for input and getting in the output (aka: in the .dvi) something that would look like:

\begin{someenvironment}
\command1{foo} \command2{bar} \command3{baz}
\somecommand % end record 1 
\command1{zip} \command2{boom} \command3{bang}
\somecommand % end record 2
\end{someenvironment}

\someenvironment may or may not be a tabular environment and command1 are commands and aliases that I or others have already been defined.

I'd like to avoid preparing a script in some language that reads my data and outputs the above (admittedly, easy), because I'm working with others and I would rather not have them running the script on the data every time they need to compile their LaTeX file.

Any hint, however small, is appreciated.

Thanks!

  • Any hint, however small? → datatool and/or pgfplotstable – user11232 Aug 22 '14 at 6:08
  • Thanks, Harish, I'm having a look at both - however, they seem to be mainly converned about tables and plotting, according to e.g. ctan.mackichan.com/macros/latex/contrib/datatool/…, while a table is exactly what I'm not after. – Tobia Tesan Aug 22 '14 at 6:11
  • datatool can be used outside table also. – user11232 Aug 22 '14 at 6:19
2

Please, use csvsimple version 1.12 (2014/07/14) (or newer).

In my code, I made a helper macro \commandThreeX which is used to get rid of the semicolon at the end of the input lines. The rest is quite straigtforward using \csvreader:

\begin{filecontents*}{example.csv}
foo,bar,baz;
zip,boom,bang;
\end{filecontents*}

\documentclass{article}
\usepackage{csvsimple}

%%%% Your Macros %%%%
\newenvironment{someenvironment}{BEGINENV\par}{\par ENDENV}

\newcommand\commandOne[1]{ONE(#1)}
\newcommand\commandTwo[1]{TWO(#1)}
\newcommand\commandThree[1]{THREE(#1)}
\newcommand\somecommand{ENDRECORD\par}

%%%% Auxiliary %%%%
\def\commandThreeX#1;{\commandThree{#1}}


\begin{document}

\begin{someenvironment}
\csvreader[no head]{example.csv}{}
  {%
    \commandOne{\csvcoli}%
    \commandTwo{\csvcolii}%
    \begingroup\edef\x{\endgroup\noexpand\commandThreeX\csvcoliii}\x%
    \somecommand%
  }
\end{someenvironment}

\end{document}

The result is:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.