Tell me more ×
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 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

share|improve this question
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 '12 at 4:42
Dear Alan Munn, Yes. That is what I want. Please give it a try. – Harish Kumar Jan 29 '12 at 6:56
Dear Alan Thank you for helping out the first problem. It works. In second one I want the vertical lines in the date columns to be exactly halfway in each date column. Please help. – Harish Kumar Jan 29 '12 at 7:03
@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 '12 at 14:56
@AlanMunn, Thank you. I did not know this. I will follow. – Harish Kumar Jan 31 '12 at 0:59

2 Answers

up vote 8 down vote accepted

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

share|improve this answer
Dear Alan Munn. Thank you. It works. – Harish Kumar Jan 29 '12 at 23:04
@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 '12 at 23:45
Dear Alan Munn, I clicked already. Thank you. – Harish Kumar Jan 31 '12 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 '12 at 1:00
Oh. I clicked now. Thank you. I noticed one more thing in the table. When we define the new column type C and when the number of entries in the csv files is large (say 30), after the last row (i.e., row numbered 30) one extra blank line is left. I could not make this out. (Please copy and paste the contents of CSV file (except header line) some 6 times one below the other so that it has 30 lines and run the tex file.) Any suggestions? please. – Harish Kumar Jan 31 '12 at 1:07
show 2 more comments

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.

share|improve this answer
Dear sir I have included the sample code now. I could not find any spurious \\ anywhere. Please help me. – Harish Kumar Jan 29 '12 at 1:51

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.