[Tex/LaTex] Create a caption for table using the \csvautolongtable command from the csvsimple package

csvcsvsimplelongtabletables

I successfully included a .csv table using the \csvautolongtable command from the csvsimple package (the table is more than one page, this is why I am using this command). Now I want to add a caption and label for this table, but I cannot figure out how. I appreciate any help. Thanks in advance!

Here is my minimal example

\documentclass[pdftex,a4paper]{scrartcl}
\usepackage[german, english]{babel} 
\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{longtable}

\begin{document}
  %?\caption{There should be a caption somehow}
  %?\label{and a label...}
    \csvautolongtable[respect all]{chemicaltable.csv}
\end{document}

My csv file looks like this:

Known effect,Group
microcystin; DNA damage,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
reactive,A
Estrogenic,B
Estrogenic,B
Estrogenic,B
Estrogenic,B
Estrogenic,B
Estrogenic,B
anticancer,C
anticancer,C
anticancer,C
antirheumaticum,C
calcium pathway,C
gsk inhibitor,C
Antiandrogen,D
Antiandrogen,D
Antiandrogen,D
Antiandrogen,D
sterol pathway,E
sterol pathway,E
antirheumaticum,F
cyclooxygenase,F
low tox; antiinflammatory,F
endogenous,F
Antibiotic,G
Antibiotic,G
serotonin,H
serotonin,H
flame retardant,I
unknown,I
low tox. food,J
low tox. food,J
low tox; antiinflammatory,K
non toxic in fish-embryo,K
unknown,K
unknown,K
antiepileptic,L
antiepileptic; low tox,L
antiepileptic,M
dopamine receptors and others,M
adenosin receptor,N
nephrotox; adenosin,N
opiod receptor,O
ache inhibitor,P
Thyroid,Q
glucocorticoid,R
retinol pathway,x
Hif1 alpha pathway,y
hedgehog pathway,z

Best Answer

You can use table head as done by egreg with some more additions:

\csvautolongtable[
      table head=\caption{some table}\label{tab:some}\\\hline
               \csvlinetotablerow\\\hline
               \endfirsthead\hline
               \csvlinetotablerow\\\hline
               \endhead\hline
               \endfoot,
               respect all
               ]{chemicaltable.csv}

Full code:

\documentclass[pdftex,a4paper]{scrartcl}
\usepackage[german, english]{babel}
\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{caption}
\usepackage{longtable}
\usepackage{filecontents}
\begin{filecontents*}{chemicaltable.csv}
  Known effect,Group
microcystin; DNA damage,A
reactive,A
reactive,A
reactive,A
Estrogenic,B
Estrogenic,B
Estrogenic,B
Estrogenic,B
Estrogenic,B
Estrogenic,B
anticancer,C
anticancer,C
anticancer,C
antirheumaticum,C
calcium pathway,C
gsk inhibitor,C
Antiandrogen,D
Antiandrogen,D
Antiandrogen,D
Antiandrogen,D
sterol pathway,E
sterol pathway,E
antirheumaticum,F
cyclooxygenase,F
low tox; antiinflammatory,F
endogenous,F
Antibiotic,G
\end{filecontents*}

\setlength{\textheight}{7cm}


\begin{document}
  %?\caption{There should be a caption somehow}
  %?\label{and a label...}
    \csvautolongtable[
      table head=\caption{some table}\label{tab:some}\\\hline
               \csvlinetotablerow\\\hline
               \endfirsthead\hline
               \csvlinetotablerow\\\hline
               \endhead\hline
               \endfoot,
               respect all
               ]{chemicaltable.csv}

Here is my table~\ref{tab:some}
\end{document}

enter image description here

enter image description here