4

I'm trying to adjust width column of tabular generated by \DTLdisplaydb from datatool package like here for tabular environment. But I didn't come up with any solution.

I tried something pretty bad:

\documentclass[10pt,a4paper,landscape]{report}
\usepackage{datatool}
\usepackage[small,compact]{titlesec}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}  % utf8x causes problem with accents in header columns
\usepackage[frenchb]{babel}
\usepackage[margin=1in]{geometry}

\DTLloaddb{mydb}{db.csv}

\let\oldtabular\tabular
\let\endoldtabular\endtabular
\newcolumntype{M}{>{\begin{varwidth}{8cm}}l<{\end{varwidth}}} %M is for Maximal column
\renewenvironment{tabular}{\tabular[|M|]}{\endtabular}

\begin{document}
  \begin{table}\footnotesize
    %\begin{minipage}{0.9\textwidth} %Useless here
    \DTLdisplaydb{mydb}
    %\end{minipage}
  \end{table}
\end{document}

I got the following error:

! TeX capacity exceeded, sorry [input stack size=5000].
\tabular ->\tabular 
                    [|M|] 
l.67  \DTLdisplaydb{mydb}

Here is a sample DB:

entete,entete,entete,
1,"big string paragraph with a lot of charachter like that. I don't know what to say.. blablablablablablablablablabla... ", "reblabla"
2,"text","text"

Surely, there is another way.

Werner
  • 603,163
Katsu
  • 143
  • It would help to have a complete MWE (include document class and database). You can override the column specifications by redefining \dtlstringalign, \dtlintalign, \dtlrealalign or \dtlcurrencyalign depending on the data type of the column. – Nicola Talbot Oct 19 '13 at 16:30
  • @Nicola Talbot: It's done. Unfortunately these command don't affect width of cells. – Katsu Oct 19 '13 at 16:46
  • For these with the same issue, I found something interesting: \begin{table}\footnotesize \renewcommand*{\dtlstringformat}[1]{\parbox[t][4cm][l]{0.2\textwidth}{#1}} \DTLdisplaydb{mydb} \end{table} Association of \dtlstringformat with \parbox. – Katsu Oct 19 '13 at 17:06
  • The \dtl...align commands should affect the width of the cell if they're set to a column type that adjusts the cell width (such as \renewcommand{\dtlstringalign}{p{2in}} or, in your case, \renewcommand{\dtlstringalign}{M}). – Nicola Talbot Oct 19 '13 at 17:15
  • @Nicola Talbot: +1 It works. But there are something wrong with header row (entete). They seemed misaligned: some are located on the top of cell and other on the bottom. I don't know why. for example the first "entete" (correspond to column with intefer) is located on the top but other entete are located on the bottom. – Katsu Oct 19 '13 at 17:26

1 Answers1

3

Here's a possible solution:

\documentclass[10pt,a4paper,landscape]{report}
\usepackage{datatool}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[frenchb]{babel}
\usepackage[margin=1in]{geometry}

\usepackage{varwidth}
\usepackage{array}

\begin{filecontents*}{db.csv}
entete,entete,entete
1,"big string paragraph with a lot of charachter like that. I don't know what to say.. blablablablablablablablablabla... ", "reblabla"
2,"text","text"
\end{filecontents*}

\DTLloaddb[keys={col1,col2,col3}]{mydb}{db.csv}

\renewcommand{\dtlstringformat}[1]{\begin{varwidth}[t]{8cm}#1\end{varwidth}}

\begin{document}

\DTLdisplaydb{mydb}

\end{document}

The result is:

Image of resulting table

(The first column is right-aligned because it only contains integers.)

Nicola Talbot
  • 41,153
  • I am sorry for asking here, but I don't know if it is worth a question on the main site or not. If I have some raw data with column and row heasers stored in an ASCII file, and I would like to: 1- transpose this data before displaying 2- replace the existing headers in the ASCII file with my own. So, could you please refer me to the respective sections in the guide or should I ask a question? Thanks – Diaa Jan 26 '18 at 16:07
  • 1
    @DiaaAbidou Headers can be set when you load the file, for example \DTLloaddb[keys={col1,col2,col3},headers={Column 1,Column 2,Column 3}]. Alternatively, after the data is loaded you can use \DTLsetheader{mydb}{col1}{Column 1} etc. The only way to transpose would be to iterate over all column and row indexes and use \DTLgetvalue. (Too complicated for a comment, so I suggest you post a question.) – Nicola Talbot Jan 26 '18 at 16:46
  • Many thanks for addressing my comment. In order to be very clear about my inquiry, I asked this question, and I would be grateful if you could consider it. – Diaa Jan 26 '18 at 17:17