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.

I'm putting many tables in many files generated by csv files. I'm using datatools (opposed to say pgfplotstable) because it is much more forgiving in terms of the csv input format (e.g. it ignores quotes around text). However, I would like to make my tables "stripy" with a grey color every other row.

To put it plainly, I would like to use \DTLifoddrow{\rowcolor{grey}}{} in the preamble of the document. That way I can save myself from manually creating every tabular environment and can apply the same style to all tables.

Unfortunately \DTLifoddrow must be inside \DTLforeach. Therefore I might have to redefine \DTLforeach or \DTLdisplaydb to automatically add my \DTLifoddrow. I'm not sure which path to take or how to go about redefining these commands.

share|improve this question
    
Note: I do not know how many rows or columns there are (or their labels). The input data changes for every table. The idea here is to make "stripy tables" a global thing. –  bkanuka May 2 '13 at 14:20
1  
Welcome to TeX.sx! It would be great if you could provide a minimal working example (MWE). This helps us to provide an answer for you. –  mafp May 2 '13 at 14:39
    
You're probably right - I should have. But I just found the answer to my question! Unfortunately, can't answer my own question with so little rep. I had to use \usepackage[table]{xcolor} and \rowcolors{1}{green}{blue} (or whatever). Both of these can go in the preamble and will be applied 'globally'. –  bkanuka May 2 '13 at 14:43
    
That's great! Are you sure you can not answer? I thought this is possible with rep 1. –  mafp May 2 '13 at 14:45
    
I have to wait 8 hours before answering my own question –  bkanuka May 2 '13 at 14:57
add comment

1 Answer

up vote 2 down vote accepted

Since asking, I've figured out how to do a lot of fun things with datatool:

Short Answer

\usepackage[table]{xcolor}
\usepackage{datatool}

\definecolor{light-gray}{gray}{0.9}
\let\oldtabular\tabular
\let\endoldtabular\endtabular
\renewenvironment{tabular}{\rowcolors{0}{white}{light-gray}\oldtabular}{\endoldtabular}

Every tabular environment (including those created automatically by DTL) is now striped. To draw an tabular not striped use \begin{oldtabular} and \end{oldtabular}.

Fun Stuff

Automatically Use Booktabs

Booktabs are (is?) wonderful. Use it always.

\usepackage{booktabs}
% start every dtl table with \toprule from booktabs
\renewcommand{\dtldisplaystarttab}{\toprule}

% likewise for \midrule and \bottomrule from booktabs 
\renewcommand{\dtldisplayafterhead}{\midrule}
\renewcommand{\dtldisplayendtab}{\\\bottomrule}

Now all datatool tables look nice!

Formatting Numbers

My CSV files tend to look like 4525,25463135,2346234,4643 etc. (i.e. ugly). There's no need for datatool to display them poorly also. Use package siunitx!

Now I must admit, I do not know all the power of siunitx, so I recommend reading docs, but I'll tell you what I do know.

We're going to use it to format long numbers to have commas at the thousands places (i.e. 1,234 instead of 1234). For very long numbers I think siunitx will put the number in scientific notation. With some tweaks, it is also possible to display (or round to) any number of decimal places. Again, please read the docs.

In order for siunitx to work it's magic, every number must be wrapped in \num{ }. We do this in the following way:

% package to automatically format numbers with thousands separator
\usepackage[group-separator={,},group-minimum-digits=3]{siunitx}
\renewcommand*{\dtlrealformat}[1]{\num{#1}}

This should correctly format anything that appears to be a number (according to datatool).

Easy CSV Inserting

We define two commands: \csvtable and \widecsvtable:

\newcommand{\csvtable}[3][]{%
    \DTLloadrawdb[#1]{#2}{#2}
    \begin{table}[h!]
        \centering
        \caption{#3}
        \DTLdisplaydb{#2}
    \end{table}
}

\newcommand{\widecsvtable}[3][]{%
    \DTLloadrawdb[#1]{#2}{#2}
    \begin{table}[h!]
        \centering
        \caption{#3}
        \begin{adjustbox}{center}
            {\footnotesize \DTLdisplaydb{#2}}
        \end{adjustbox}
    \end{table}
}

\csvtable takes the form \csvtable[options]{filename}{caption} were options are the same options you would supply to \DTLloadrawdb. For example, column labels in the csv file must be unique and cannot contain any LaTeX special symbols. To set column labels without any restrictions use e.g.:

\csvtable[headers={repeat header,repeat header,\$ymbol}]{file.csv}{caption}

where headers= a comma separated list of column labels. If the csv file does not contain labels as the first row, use option noheader e.g.:

\csvtable[noheader,headers={*header list*}]{file.csv}{caption}

\widecsvtable is used the same way and simply shrinks the font of the table and centers it on the page. This gives you room for an extra column or two. To use this we need to \usepackage{adjustbox}.

Multi-Line Column Headers

In general (not just in tables), words can be stacked on top of each other with \shortstack{row 1\\row 2}. This is useful for multi line column headers e.g.:

\csvtable[headers={\shortstack{line 1\\line 2},\shortstack{line 1\\line 2}}]{file.csv}{caption}

is a two column table with (possibly long) two-line column labels.

Long Answer

Putting it all together, you'll end up with a preamble that looks something like this:

% import xcolor and (optionally) booktabs and adjustbox
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{adjustbox}

% package to automatically format numbers with thousands separator
\usepackage[group-separator={,},group-minimum-digits=3]{siunitx}

% datatool
\usepackage{datatool}

% wrap all real numbers with \num from siunitx so that they're
% displayed nicely
\renewcommand*{\dtlrealformat}[1]{\num{#1}}

% start every dtl table with \toprule from booktabs
\renewcommand{\dtldisplaystarttab}{\toprule}

% likewise for \midrule and \botomrule from booktabs 
% (take this out if you dont like booktabs, but it is _highly_ recommended)
\renewcommand{\dtldisplayafterhead}{\midrule}
\renewcommand{\dtldisplayendtab}{\\\bottomrule}

% here's where the magic happens
\definecolor{light-gray}{gray}{0.9}
\let\oldtabular\tabular
\let\endoldtabular\endtabular
\renewenvironment{tabular}{\rowcolors{0}{white}{light-gray}\oldtabular}{\endoldtabular}



\newcommand{\csvtable}[3][]{%
    \DTLloadrawdb[#1]{#2}{#2}
    \begin{table}[h!]
        \centering
        \caption{#3}
        \DTLdisplaydb{#2}
    \end{table}
}

\newcommand{\widecsvtable}[3][]{%
    \DTLloadrawdb[#1]{#2}{#2}
    \begin{table}[h!]
        \centering
        \caption{#3}
        \begin{adjustbox}{center}
            {\footnotesize \DTLdisplaydb{#2}}
        \end{adjustbox}
    \end{table}
}
share|improve this answer
add comment

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.