7

I have a CSV file that has many rows. I want to typeset these rows into a table using longtable and datatool package.

  1. However just before before the "lastfoot" (which says concluded) I am getting an empty row which I do not want. How to remove that extra row?
  2. The columns are not equally spaced under the dates. Somebody please help me to make them equally spaced.

I am giving the sample code. The name of my CSV file is namelist.csv and it looks like this.

    number,degree,Name
    1,Dr,Abdul Ali
    2,Mrs,Francesca Joestar
    3,Mr,Chan Ker Mei
    4,Dr,Hikaru Yagami
    5,Dr,Harish Kumar

My LaTeX code is below.

\documentclass[12pt]{article}
\usepackage{setspace}
\usepackage{bookman}
\usepackage{longtable}
\usepackage{pdflscape}
\usepackage[a4paper,left=1in,right=0.8in,top=0.5in,bottom=0.5in]{geometry}
\usepackage{tgschola}
\usepackage{microtype}
\usepackage{datatool}
\DTLloaddb{names}{namelist.csv}
%==================================================================
\begin{document}
%
\begin{landscape}
%
\doublespacing
%
\begin{longtable}{|l|l|c|c|r|r|r|c|c|c|c|c|c|c|c|c|c|c|}\hline
%
No.&\multicolumn{1}{c|}{Name}& \multicolumn{2}{c|}{13.12.2010}&  
\multicolumn{2}{c|}{14.12.2010}&\multicolumn{2}{c|}{15.12.2010}&
\multicolumn{2}{c|}{16.12.2010}
&\multicolumn{2}{c|}{17.12.2010}&\multicolumn{2}{c|}{18.12.2010}
&\multicolumn{2}{c|}{19.12.2010} &\multicolumn{2}{c|}{20.12.2010}\\ \hline\hline
\endfirsthead
%
\multicolumn{18}{c}%
{{\bfseries  Continued from previous page}} \\
\hline
%
No.&\multicolumn{1}{c|}{Name}& \multicolumn{2}{c|}{13.12.2010}&
\multicolumn{2}{c|}{14.12.2010}&\multicolumn{2}{c|}{15.12.2010}& 
\multicolumn{2}{c|}{16.12.2010}
&\multicolumn{2}{c|}{17.12.2010}&\multicolumn{2}{c|}{18.12.2010}
&\multicolumn{2}{c|}{19.12.2010} &\multicolumn{2}{c|}{20.12.2010}\\ \hline\hline
\endhead
%
\hline \multicolumn{18}{|r|}{{Continued on next page}} \\ \hline
\endfoot
%
\hline
\multicolumn{18}{|r|}{{Concluded}} \\ \hline
\endlastfoot
%
\DTLforeach{names}{
\no=number, \dg=degree, \name=Name}{
\no& \dg. \name &  & &  & &  & &  &
&  &  &  &  &  &  &  & \\ \hline
}
\end{longtable}%
%
\end{landscape}
%
\end{document}

output of code

3
  • I've solved your first problem, but I don't understand what you mean by the columns not being equally spaced (your second problem). Do you want the vertical lines in the date columns to be exactly halfway in each date column?
    – Alan Munn
    Jan 29, 2012 at 4:42
  • @Dr.Harishkumar I've updated my answer. P.S. if you prefix comments with @ (e.g. @Alan) the person you are addressing the comment to will be notified.
    – Alan Munn
    Jan 29, 2012 at 14:56
  • @AlanMunn, Thank you. I did not know this. I will follow.
    – user11232
    Jan 31, 2012 at 0:59

3 Answers 3

10
+100

When using \DTLforeach with \hline it's generally better to use the following schematic structure which puts the \\ and the \hline at the beginning of each non-initial row instead of at the end of each row.

\DTLforeach{<database>}{<assignments>}{%
\DTLiffirstrow{}{\\\hline}

 rest of table with no final \\
}

For your second problem, there may be different solutions, but the simplest may be to use a fixed column width. If the cells will always be blank, just a p{2em} column would be fine; if you will subsequently be inserting centred text in the cells, then you need a slightly more complicated version, which is what I've use in the example.

Using the array package, I've defined a C columnn:

\newcolumntype{C}{>{\centering\arraybackslash}p{2em}}

and used this as the basis for the main c columns.

So for your code specifically:

\documentclass[12pt]{article}
\usepackage{setspace}
\usepackage{longtable,array}
\usepackage{pdflscape}
\usepackage[a4paper,left=1in,right=0.8in,top=0.5in,bottom=0.5in]{geometry}
\usepackage{datatool}
\DTLloaddb{names}{namelist.csv}
%==================================================================
\begin{document}
%
\begin{landscape}
%
\doublespacing
%
\newcolumntype{C}{>{\centering\arraybackslash}p{2em}} % THIS COMMAND ADDED
\begin{longtable}{|r|l|C|C|C|C|C|C|C|C|C|C|C|C|C|C|C|C|} % THIS COMMAND CHANGED
\hline
%
No.&\multicolumn{1}{c|}{Name}& \multicolumn{2}{c|}{13.12.2010}&  
\multicolumn{2}{c|}{14.12.2010}&\multicolumn{2}{c|}{15.12.2010}&
\multicolumn{2}{c|}{16.12.2010}
&\multicolumn{2}{c|}{17.12.2010}&\multicolumn{2}{c|}{18.12.2010}
&\multicolumn{2}{c|}{19.12.2010} &\multicolumn{2}{c|}{20.12.2010}\\ \hline\hline
\endfirsthead
%
\multicolumn{18}{c}%
{{\bfseries  Continued from previous page}} \\
\hline
%
No.&\multicolumn{1}{c|}{Name}& \multicolumn{2}{c|}{13.12.2010}&
\multicolumn{2}{c|}{14.12.2010}&\multicolumn{2}{c|}{15.12.2010}& 
\multicolumn{2}{c|}{16.12.2010}
&\multicolumn{2}{c|}{17.12.2010}&\multicolumn{2}{c|}{18.12.2010}
&\multicolumn{2}{c|}{19.12.2010} &\multicolumn{2}{c|}{20.12.2010}\\ \hline\hline
\endhead
%
\hline \multicolumn{18}{|r|}{{Continued on next page}} \\ \hline
\endfoot
%
\hline
\multicolumn{18}{|r|}{{Concluded}} \\ \hline
\endlastfoot
%
\DTLforeach{names}{%
\no=number, \dg=degree, \name=Name}{%
\DTLiffirstrow{}{\\\hline}  % THIS LINE HAS BEEN ADDED
\no& \dg. \name & & &  & &  & &  &
&  &  &  &  &  &  &  & % THIS LINE HAS BEEN CHANGED
}
\end{longtable}%
%
\end{landscape}
%
\end{document} 

output of code

7
  • @Dr.Harishkumar If you think that the answer has solved your problem, it's helpful to click on the green check mark next to it to "accept" the answer. This helps others who may encounter the same problem to find answers that work.
    – Alan Munn
    Jan 29, 2012 at 23:45
  • Dear Alan Munn, I clicked already. Thank you.
    – user11232
    Jan 31, 2012 at 0:57
  • @Dr.Harishkumar There is a difference between voting on an answer and accepting it. To accept it you need to additionally click on the green check mark beside the answer.
    – Alan Munn
    Jan 31, 2012 at 1:00
  • Thanks. I checked that before I posted my answer. With the file I've posted, I get no extra lines with any number of entries in the file.
    – Alan Munn
    Jan 31, 2012 at 1:18
  • Thank you. Some thing went wrong in my code. Now it is OK. Thanks a lot for sparing your valuable time for me.
    – user11232
    Jan 31, 2012 at 1:45
3

A blank line in LT is most likely from a spurious \\ somewhere. From your description it isn't clear if it is in the foot as an empty first row, or in the body of the table as an empty last row, these can look the same in the output, but of course are completely separate in the source.

0
0

Do not forget to add an % in this line. There is a difference from the following line

\DTLiffirstrow{}{\\\hline}  

to

\DTLiffirstrow{}{\\\hline}  % THIS LINE HAS BEEN ADDED

If you add the first line to your table sometimes an unexpected behavior will occure. The first entry of the database will ad a space on column 1, row 1.

If you add the second line (especially you should add at least the % sign) than this behaviour will be prevented.

It's only an additional info that I found out because of this case. I remembered this entry in case of this command and want to submit this info to the community.

You must log in to answer this question.