# Generic table definitions

According to the topic How to use commands in table specs, and still support array package? I have tried to intense the way of creating tables, definitions.

\documentclass[fontsize=11pt, % Document font size
paper=a4, % Document paper type
twoside=false, % Shifts odd pages to the left for easier reading when printed, can be changed to oneside
index=totoc,
% paper=landscape,
hyperref]{scrreprt}

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage[utf8]{inputenc} % Uses the utf8 input encoding

\usepackage{graphicx} % Required for including images
\usepackage{booktabs,array,ragged2e,multirow}
\newcolumntype{M}[1]{>{\hspace{0pt}\raggedright\arraybackslash}m{#1}}
\usepackage{xcolor}
\usepackage{longtable} % can be commented out, approach is an very generic method of tablespec
\usepackage{tabu}   % can be commented out, approach is an very generic method of tablespec
\usepackage{xspace}

\begin{document}

\providecommand{\hlttextacs}{3}
\providecommand{\hlttextbcs}{4}
\providecommand{\tablecolumndira}{M}
\providecommand{\tablecolumndirb}{M}

\providecommand{\commandatablecolumnaligna}{M}
\providecommand{\commandatablecolumndesignaa}{!{\color{white}}
\providecommand{\commandatablecolumndesignba}{\vrule width 0.2em}}
\providecommand{\commandatablecolumnalignb}{M}
\providecommand{\commandatablecolumndesignab}{!{\color{white}}
\providecommand{\commandatablecolumndesignbb}{\vrule width 0.2em}}

\def\mytablespec{\commandatablecolumnaligna\commandatablecolumndesignaa\commandatablecolumndesignba\commandatablecolumnalignb\commandatablecolumndesignab\commandatablecolumndesignbb}
\def\mytabletype{longtable}

\expandafter\csname\expandafter\mytabletype\expandafter\endcsname\expandafter{\mytablespec}
1 & 2 \\\hline
3 & 4 \\
\csname end\mytabletype\endcsname

\end{document}


It shout be a more generic way to make tablespecifications during commands.

Maybe it is now more clear whats the approach.

UPDATE:

I have some commands defined like

\callmytable{A,B,C}{3,4,5}{M,M,M}


Currently it will result in commands for 3,4,5 (each) and the header of my table is...

M{\hlttextacs cm}!{\color{white}\vrule width 0.2em}M{\hlttextbcs cm}!{\color{white}\vrule width 0.2em}M{\hlttextccs cm}


With this command inside the document, I can manipulate my table, and I will decide how many columns my table has, but not from the beginning of the document. The definition should be made during this command. Using datatool, my tables load csv files, sometimes I want another alignment on table a, sometimes on table b.

Currently, I have for each csv file structure a separate command.

\twocolumnstable....
\threecolumstable....


If I can handle this table specification, the approach to reduce this commands to one is very near

\myowntablecommand{A,B,C}{2,3,4}{M,M,M}{pathtocsvfile}....


results in nice tables.

A bit long for a comment:

Your example is much too long and contains too many questions. Regarding the posting you linked to I don't understand why all the \expandafter are used. I would simply use \newcolumntype:

\documentclass{article}
\usepackage{array}

\newcolumntype\specA{l}
\newcolumntype\specB{l}
\newcolumntype\mytablespec{\specA|\specB}

\def\mytabletype{tabular}

\begin{document}
\begin{\mytabletype}{\mytablespec}
1 & 2 \\\hline
3 & 4 \\
\end{\mytabletype}
\end{document}


Please be aware that the commands in \newcolumntype are simply "tokens" to hold the definitions, they are not executed.

• This did not work for me, because the definition have to processed dynamically after \begin{document}... – Peter Ebelsberger Oct 16 '14 at 11:16
• There is no problem to move the \newcolumntype after \begin{document}. And there is no problem to redefine a spec by reusing \newcolumntype. array will only issue a warning in such cases. Also columntypes can have arguments (like the p-Type) which you can use e.g. with *{num}. – Ulrike Fischer Oct 16 '14 at 11:46

You can hide the nasty details in macros. Note that your definitions of \commandatablecolumndesignaa and \ commandatablecolumndesignab are wrong: you can't leave an unfinished { in the body of a definition and that you are forgetting the argument to the M columns.

\documentclass[
fontsize=11pt, % Document font size
paper=a4, % Document paper type
twoside=false, % Shifts odd pages to the left for easier reading when printed, can be changed to oneside
index=totoc,
% paper=landscape,
hyperref
]{scrreprt}

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage[utf8]{inputenc} % Uses the utf8 input encoding

\usepackage{graphicx} % Required for including images
\usepackage{booktabs,array,ragged2e,multirow}
\usepackage{xcolor}
\usepackage{longtable} % can be commented out, approach is an very generic method of tablespec
\usepackage{tabu}   % can be commented out, approach is an very generic method of tablespec
\usepackage{xspace}

\newcolumntype{M}[1]{>{\hspace{0pt}\raggedright\arraybackslash}m{#1}}

\makeatletter
\newcommand{\settablespec}[2]{%
\toks@={}%
\@tfor\next:=#2\do{%
\toks@=\expandafter{\the\expandafter\expandafter\expandafter\toks@\next}% \next must be expanded twice
}%
\edef#1{\the\toks@}%
}
\newcommand{\tablebegin}[2]{%
\def\next{\begin{#1}}%
\expandafter\next\expandafter{#2}%
}
\makeatother
\begin{document}

\providecommand{\hlttextacs}{3}
\providecommand{\hlttextbcs}{4}
\providecommand{\tablecolumndira}{M}
\providecommand{\tablecolumndirb}{M}

\providecommand{\commandatablecolumnaligna}{M{4cm}}
\providecommand{\commandatablecolumndesigna}{!{\color{white}\vrule width 0.2em}}
\providecommand{\commandatablecolumnalignb}{M{3cm}}
\providecommand{\commandatablecolumndesignb}{!{\color{white}\vrule width 0.2em}}

\settablespec{\mytablespec}{%
\commandatablecolumnaligna
\commandatablecolumndesigna
\commandatablecolumnalignb
\commandatablecolumndesignb
}
\def\mytabletype{longtable}

\tablebegin{\mytabletype}{\mytablespec}
1 & 2 \\\hline
3 & 4 \\
\end{\mytabletype}

\end{document}


• This answer solved all of my problems. There is still some work to do, to make my tables generic, but with this answer I am on a very good way for tables in a nice way. – Peter Ebelsberger Oct 17 '14 at 10:56