Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

So after searching for some hours I'm surprised that there wasn't any useful solution to find.

I import a CSV table using datatool. That table only consists of 3 narrow columns, but I got a lot of rows. I followed the example in the documentation to get two rows in one table row next to each other.

So far that works fine but unfortunately the values read from left to right and not left side top to down and then on the right half top to down.

Let me illustrate my Problem:

|---|---|---|---|
| # |foo| # |foo|
|===|===|===|===|
| 1 |abc| 2 |def|
| 3 |ghi| 4 |jkl|
| 5 |mno| 6 |pqr|
|---|---|---|---|

That's how it is now following the documentation (Example 8, page 47 in datatool.pdf).

What I want to have is

|---|---|---|---|
| # |foo| # |foo|
|===|===|===|===|
| 1 |abc| 4 |jkl|
| 2 |def| 5 |mno|
| 3 |ghi| 6 |pqr|
|---|---|---|---|

Is there any chance to modify that example to give an output like that?

Minimal example

\documentclass[a4paper]{article}
\usepackage{datatool}

\begin{document}

\DTLsetseparator{;}
\DTLloaddb[noheader,keys={id,item}]{test_db}{test.csv}


\begin{table}[htbp]
    \caption{Two database rows per tabular row}
    \centering
    \begin{tabular}{cc|cc}
    \bfseries ID &
    \bfseries Item &
    \bfseries ID &
    \bfseries Item
    \DTLforeach*{test_db}{\id=id,\item=item}{%
        \DTLifoddrow{\\}{&}%
        \id & \item}%
    \end{tabular}
\end{table}
\end{document}

With test.csv filled with the following

1;abc
2;def
3;ghi
4;jkl
5;mno
6;pqr
share|improve this question
    
Hi aaron, Welcome to TeX.sx! Could you place, as a minimal working example, the code that gives you the result you mention? This gives the wizards a starting point they can copy and paste. –  qubyte Mar 13 '12 at 3:57
    
Thanks for the hint, I added a working example. –  aaron Mar 13 '12 at 8:26

2 Answers 2

up vote 6 down vote accepted

enter image description here

\documentclass[a4paper]{article}
\usepackage{datatool}

\begin{document}

\DTLsetseparator{;}
\DTLloaddb[noheader,keys={id,item}]{test_db}{test.csv}



\begin{table}[htbp]
   \count2=\DTLrowcount{test_db}
   \count4 = \count2
   \divide\count4 by 2
   \count2=\count4
   \advance\count2 by 1

    \caption{Two database rows per tabular row}
    \centering
    \begin{tabular}{cc|cc}
    \bfseries ID &
    \bfseries Item &
    \bfseries ID &
    \bfseries Item
    \DTLforeach*[\id<\count2]{test_db}{\id=id,\item=item}{%
       \\
        \id & \item
   \count0=\id
   \advance\count0 by \count4
    \DTLforeach*[\id=\count0]{test_db}{\id=id,\item=item}{%
      &\id &\item}
}
    \end{tabular}
\end{table}
\end{document}
share|improve this answer
    
Thank you! Your answer helped me a lot. I modified your code a little bit to also support an odd number of rows in the CSV file. It probably could be done nicer, but I'm just starting to use LaTeX so I'm happy to just get it to work. –  aaron Mar 13 '12 at 19:35

Table with odd number of rows

With the help of David Carlisle I managed to solve it. I modified his code a little bit to also support an odd number of rows in the CSV file. My modification is probably not the best way to do it, but I'm just getting started with LaTeX and it is so not like what I'm used to when it comes to programming.

\documentclass[a4paper]{article}
\usepackage{datatool}

\begin{document}

\DTLsetseparator{;}
\DTLloaddb[noheader,keys={id,item}]{test_db}{test.csv}

\DTLrowcount{test_db}

\begin{table}[htbp]
   \count2=\DTLrowcount{test_db}
   \count4 = \count2
   \divide\count4 by 2
   \count2=\count4

    %%% modification start
    \count10=\count2
    \advance\count10 by \count4

    \ifnum\count10=\DTLrowcount{test_db}
        %even number of entries 
        \advance\count2 by 1
    \else
        %odd number of entries
        \advance\count2 by 2
        \advance\count4 by 1
    \fi
    %%% modification end

    \caption{Two database rows per tabular row}
    \centering
    \begin{tabular}{cc|cc}
    \bfseries ID &
    \bfseries Item &
    \bfseries ID &
    \bfseries Item
    \DTLforeach*[\id<\count2]{test_db}{\id=id,\item=item}{%
       \\
        \id & \item
   \count0=\id
   \advance\count0 by \count4
    \DTLforeach*[\id=\count0]{test_db}{\id=id,\item=item}{%
      &\id &\item}
}
    \end{tabular}
\end{table}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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