5

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
3

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

2

With longtable the caption should be part of the table, usually in the “first header”; you can insert material before the contents of the csv file is read in with table head:

\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*}

\documentclass[a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[german, english]{babel}

\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{caption}
\usepackage{longtable}

\begin{document}

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

\end{document}

enter image description here

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.