2

There is a CSV file, which has 1-6 records, I need to load it and display on the letter page into two columns (each row in a column is block with all text from CSV row), right column appears only for 4-6 records, it means that at first I need to fill first column then go two the second, so using simple table is not an option (may be I'm wrong here).

CSV:

field_name1, field_name2,field_name3
smth1, smth2,smth3
smthelse1,smthelse2,smthelse3
rec1,rec2,rec3
abc1,abc2,abc3
...,...,...
...,...,...

will be displayed as

smth1         abc1
smth2         abc2
smth3         abc3

smthelse1     ...
smthelse2     ...
smthelse3     ...

rec1          ...
rec2          ...
rec3          ...

I've tried to use \fboxes and minipages but I get some mess as I didn't know how to count rows (to use \DTLifeq(current_row<=3) to filling left part for example). What is the proper way to make \DTLforeach properly produce such a structure?

migrated from stackoverflow.com Jun 7 '13 at 18:51

This question came from our site for professional and enthusiast programmers.

  • Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Werner Jun 7 '13 at 18:52
2

You can just use two tabular environments next to each other like this:

\documentclass{article}

\usepackage{datatool}

\begin{filecontents*}{test.csv}
field_name1, field_name2,field_name3
smth1, smth2,smth3
smthelse1,smthelse2,smthelse3
rec1,rec2,rec3
abc1,abc2,abc3
...,...,...
...,...,...
\end{filecontents*}

\DTLloaddb{mydata}{test.csv}

\begin{document}

\begin{tabular}[t]{l}
\DTLforeach*{mydata}
 {\Fieldi=field_name1,\Fieldii=field_name2,\Fieldiii=field_name3}
 {%
    \ifthenelse{\value{DTLrowi}<4}
    {%
      \Fieldi\\\Fieldii\\\Fieldiii\\\\%
    }%
    {}%
 }
\end{tabular}
\begin{tabular}[t]{l}
\DTLforeach*{mydata}
 {\Fieldi=field_name1,\Fieldii=field_name2,\Fieldiii=field_name3}
 {%
    \ifthenelse{\value{DTLrowi}>3}
    {%
      \Fieldi\\\Fieldii\\\Fieldiii\\\\%
    }%
    {}%
 }
\end{tabular}

\end{document}

Result:

Image of result

Here's a more generic method for an arbitrary number of columns:

\documentclass{article}

\usepackage{datatool}

\begin{filecontents*}{test.csv}
field_name1, field_name2,field_name3
smth1, smth2,smth3
smthelse1,smthelse2,smthelse3
rec1,rec2,rec3
abc1,abc2,abc3
...,...,...
...,...,...
\end{filecontents*}

\DTLloaddb{mydata}{test.csv}

\begin{document}

\begin{tabular}[t]{l}
  \dtlgforint\dtlrownum=1\to3\step 1\do
  {%
    \dtlgetrow{mydata}{\dtlrownum}%
    \global\dtlcurrentrow=\dtlcurrentrow
    \dtlgforint\dtlcolumnnum=1\to\DTLcolumncount{mydata}\step 1\do
    {%
      \dtlgetentryfromcurrentrow{\thisvalue}{\dtlcolumnnum}%
      \thisvalue\\
    }%
    \\
  }%
\end{tabular}
\begin{tabular}[t]{l}
  \dtlgforint\dtlrownum=4\to6\step 1\do
  {%
    \dtlgetrow{mydata}{\dtlrownum}%
    \global\dtlcurrentrow=\dtlcurrentrow
    \dtlgforint\dtlcolumnnum=1\to\DTLcolumncount{mydata}\step 1\do
    {%
      \dtlgetentryfromcurrentrow{\thisvalue}{\dtlcolumnnum}%
      \thisvalue\\
    }%
    \\
  }%
\end{tabular}

\end{document}

Your Answer

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