2

I am new in Latex and I have some issues. I have succeeded in reading the .csv file:

,w5,w6,w7,w8,w9,w10,w11,w12,w13,w14,w15,w16,w17,w18,w19,w20,w21,w22,w23,w24
,25,23,13,14,15,39,36,4,37,11,26,13,34,39,40,22,33,32,9,5
,56,31,45,31,38,41,59,26,,44,52,33,,42,,28,51,41,28,36

and using How to use datatool package to read a CSV files so that they can be used in tables? got this output:

Image 1

But now I have to format the table like this:

Image 2

and I don't know how, because first of all I don't know how to set the margins that the table can fit into the paper(I've used the geometry package to make the page landscape, but it is not enough).

Then each cell contains time of departure as hour followed by minute, both as two digits, but sometimes I don't have two digits,just one so I have to switch eg. from 12 4 -> 12 04.

Finally each data field means the minute of departure within the particular hour, e.g. number 25 in column w5 means time of departure 5:25, I don't know how to format this way. I was trying like this but I don't think this is the correct way:

\ifthenelse{\equal{\insertbyname{w5}}{}}{\insertbyname{w5}&}{{\bf 05} \insertbyname{w5} &}

If someone can help me I will be more than happy.

1 Answers1

2

Since your table is very wide, the only real choice is to scale it to fit the full width of the page via \resizebox{\linewidth}{!}{\DTLdisplaydb{myDB}}. I won't include the full image as it is quite unreadable, but here is the left and right hand side of the table, with the frame drawn using \usepackage[showframe]{geometry}:

enter image description here ... enter image description here


As far as accessing the database, you need to use the key to locate the particular value. So, suppose you want to locate the value in row 1 of the w5 column, and the value of row 2 of the w6 column you use:

\GetValueOfColumnAtRow{\myValue}{w5}{1}
\GetValueOfColumnAtRow{\myValue}{w6}{2}

and the value gets stored in \myValue. So with the code below, we obtain:

enter image description here


To step through each row of the database, you can use a \DTLforeach. Below is the code showing how to process the first three columns of the data to yield:

enter image description here

Notes:

  • As this question is more about accessing the data from the database, I have not addressed the specific issues of formatting the table. Those should be in a separate question as they are not unrelated to database access.

Code:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{datatool}
\usepackage{graphicx}

\newcommand*{\GetValueOfColumnAtRow}[3]{% % #1 = csname of where to store the result % #2 = key for column % #3 = row number \DTLgetvalue{#1}{myDB}{#3}{\dtlcolumnindex{myDB}{#2}}% }%

\begin{document} \DTLloaddb{myDB}{MyData.csv} \noindent \resizebox{\linewidth}{!}{\DTLdisplaydb{myDB}}

\bigskip\noindent
\GetValueOfColumnAtRow{\myValue}{w5}{1}
The value of w5 in row 1 is \myValue.
\par\noindent
\GetValueOfColumnAtRow{\myValue}{w6}{2}
The value of w6 in row 2 is \myValue.

\bigskip
\begin{tabular}{l l l l l}
        5 to & 6 to & 7 to \\
        6am  & 7am  & 8am  \\\hline
\DTLforeach{myDB}{%
    \WFive=w5,
    \WSix=w6,
    \WSeven=w7%
    }{%
        5:\WFive &
        6:\WSix &
        7:\WSeven &
        \\
}%
\end{tabular}

\end{document}

Peter Grill
  • 223,288
  • Thank you for the tips, but actually I don't know the number of lines in my .cvs file, so I need somehow to build the table automatically and more over how can I create the table to look like the one that I posted above? I can't use the structure of a table? – Ion Morozan May 17 '12 at 09:32
  • @IonMorozan: Then use a DTLforeach to loop thru each row of your data. – Peter Grill May 17 '12 at 16:37
  • @IonMorozan: See updated solution showing how to step through each row. – Peter Grill May 17 '12 at 22:40
  • Thank you so much for your time! I really appreciate it! I was trying your code, but I get this error: ! Undefined control sequence}(near }% - last bracket). I tried to do some modifications but it doesn't work. Any ideas? – Ion Morozan May 17 '12 at 23:48
  • @IonMorozan: I just checked and the code as is compiles for me (am using the latest version of TeXLive2011). Without more details I can not tell. Did you modify the code? – Peter Grill May 17 '12 at 23:55
  • No I just copied into a new latex file and erased this section: \bigskip\noindent \GetValueOfColumnAtRow{\myValue}{w5}{1} The value of w5 in row 1 is \myValue. \par\noindent \GetValueOfColumnAtRow{\myValue}{w6}{2} The value of w6 in row 2 is \myValue. – Ion Morozan May 17 '12 at 23:57
  • Did the code compile before you made any changes? – Peter Grill May 17 '12 at 23:59
  • Yes, I am using Kyle as an "IDE". Now I copied again your code and it seems that \GetValueOfColumnAtRow{\myValue}{w5}{1} gives me this error too: Undefined control sequence. \GetValueOfColumnAtRow{\myValue}{w5}{1},Missing number, treated as zero.\GetValueOfColumnAtRow{\myValue}{w5}{1} ... and so on! – Ion Morozan May 18 '12 at 00:02
  • I don't know. Do you have an up to date distribution? Are you perhaps on Debian which ships with a really old version? Otherwise you need to post a new question. – Peter Grill May 18 '12 at 00:06