5

I have a table that spans several pages I will be including in an appendix that I would like to import into my LaTex document from a csv. I found the long table answer post very helpful: Importing CSV file as a table in Latex but file too long, but I get a random extra cell and/or row underneath the first column header that throws off the rest of the table, and sometimes repeats throughout. Here is a shortened example of my data and the commands I used. If you uncomment the color code, it is easier to see what appears to be an extra cell that is throwing everything off (also, see the interrupted table border on the right side). Does anyone have any suggestions? Thanks.

%SE_example.csv looks like this:
Policy 1,2000-2010,Text here,a
Policy 2,1999-2005,Text here,b
Policy 3,1995-2006,Text here,c

%LaTex Code:
\documentclass[12pt, english]{article} 
\usepackage{csvsimple}
\usepackage{longtable}
\usepackage{caption}
\usepackage{setspace}
\usepackage{booktabs}
\usepackage{array}
\usepackage[hmargin=2.54cm, vmargin=2.54cm]{geometry}

%\usepackage[table]{xcolor}
%\definecolor{lightgray}{gray}{0.9}

%\let\oldlongtable\longtable
%\let\endoldlongtable\endlongtable
%\renewenvironment{longtable}{\rowcolors{2}{white}{lightgray}\oldlongtable} {
%\endoldlongtable}

\begin{document} 

\renewcommand*{\arraystretch}{1.6}
\scriptsize
\captionsetup{font=small}
\begingroup
\obeylines%
\catcode`\,=4%
\def^^M{\\}%
\makeatletter%
\begin{longtable}{|p{6cm} p{1.3cm} p{7.5cm} p{1cm}|}%
\caption{Example} \label{table:policies}
\textbf{Name} & \textbf{Years}&\textbf{Description}&\textbf{Source}\endhead %
\hline
\@@input SE_example.csv 
\end{longtable}%
\endgroup%

\end{document}

my output

  • Why aren't you using csvsimple features? – egreg Jan 26 '15 at 21:11
5

I'm not sure why you're loading csvsimple and don't use its features:

%SE_example.csv looks like this:
\begin{filecontents*}{\jobname.csv}
Policy 1,2000-2010,Text here,a
Policy 2,1999-2005,Text here,b
Policy 3,1995-2006,Text here,c
\end{filecontents*}

%LaTex Code:
\documentclass[12pt]{article} 
\usepackage{csvsimple}
\usepackage{longtable}

\usepackage[table]{xcolor}

\begin{document} 

\rowcolors{2}{black!10}{white}
\csvreader[
  head=false,
  longtable=|llll|,
  table head= \rowcolor{white}\caption{Example}\label{table:policies} \\ \hline
              \rowcolor{white}%
              \textbf{Name} & \textbf{Years}&\textbf{Description}&\textbf{Source} \\ \hline
              \endfirsthead
              \rowcolor{white}\caption*{Example (continued)} \\ \hline
              \rowcolor{white}%
              \textbf{Name} & \textbf{Years}&\textbf{Description}&\textbf{Source} \\ \hline
              \endhead,
  late after line = \\\hline
]{\jobname.csv}{}
 {\csvcoli & \csvcolii & \csvcoliii & \csvcoliv}

\end{document}

The filecontents* environment is used just for making the example self-contained.

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.