# How to create template longtable/datatool code?

Consider the MWE below (TexLive '15, pdflatex). I have multiple datafiles which need to be displayed in different tables. Currently I have copied table code like MWE multiple times.

Q1: But, what is the best (least side effects) way so I can write (here we pass the filename, caption, label):

\createMyTable(dbset1,'data1.csv',The Caption,The caption (continued),tab:thelabel1)


Q2: Now even more advanced. Assume I have now a file with a different structure (data3.csv in the MWE below with 2 columns), is it even possible to parameterize the columns (eg Latex code as string, interpreted on the fly), the assignments and output? So:

\createMyTable(dbset1,'data3.csv',p{1cm}p{1cm},\textbf{Col1}&\textbf{Col2},"Col1=col1,Col2=col2","\Col1&\Col2", Caption,The caption (continued),tab:thelabel1)


MWE:

\begin{filecontents}{data1.csv}
col1
1
2
\end{filecontents}
\begin{filecontents}{data2.csv}
col1
3
4
\end{filecontents}
\begin{filecontents}{data3.csv}
col1,col2
5,6
7,8
\end{filecontents}

\documentclass{article}
\usepackage{datatool}
\usepackage{geometry}
\usepackage{afterpage}
\usepackage{longtable}
\usepackage{lscape}
\usepackage{setspace}
\usepackage{booktabs}
\begin{document}

% START FUNCTION createMyTable(dbset1,'data1.csv',The Caption,The caption (continued),tab:thelabel1)

\afterpage{%
\newgeometry{twoside=false,vmargin={1cm,1cm},hmargin={1cm,3cm}}
\begin{landscape}% Landscape page
\pagestyle{empty}
\centering % Center table
\vspace{0pt}
\begin{singlespacing}
\begin{longtable}[c]{p{1cm}}
\caption{The Caption}
\label{tab:thelabel}\tabularnewline
\textbf{Col1}\tabularnewline
\midrule
\caption{The Caption (Continued)}\tabularnewline
\textbf{Col1}\tabularnewline
\midrule
\bottomrule
\multicolumn{1}{r}{\emph{Continued on next page...}}
\endfoot
\bottomrule
\endlastfoot
\DTLforeach*{dbset1}{\TheCol=col1}%
{\DTLiffirstrow{}{\\}\TheCol}%
\end{longtable}
\end{singlespacing}
\end{landscape}
\clearpage
}
\restoregeometry

% END FUNCTION

\end{document}

• Have a look at pgfplotstable. – Johannes_B Sep 26 '16 at 8:02
• Even if it is possible, it is a bad idea to try to save time by hiing everything in macros. Though you might not have noticed it yet, about two thirds of your tables are special and can't be dealt with the interface provided. You will want to change stuff, making the macro more and more complicated, which will lead to a huge loss of time. Eperience speaking. – Johannes_B Sep 26 '16 at 8:04