2

i would like to make a command to create tables. In a minimal idea the function "autotabell" should be like:

\documentclass{article}
\usepackage{tikz}
\usepackage{csvsimple}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xspace} 
\usepackage{xstring}


\begin{filecontents*}{grad.csv}
name,givenname,matriculation,gender,grade
Maier,Hans,12345,m,1.0
Huber,Anna,23456,f,2.3
Weissback,Werner,34567,m,5.0
Bauer,Maria,19202,f,3.3
\end{filecontents*}




\def\ReplaceStr#1{%
    \IfSubStr{#1}{XY}{%
        \StrSubstitute{#1}{XY}{\textbackslash}}{#1}}

\newcommand{\spaltenZ}[1]{%
    \gdef\spaltenZresult{1=XY1} %
    \foreach \x in {2,...,#1}%
    {%
        \global\edef\spaltenZresult{\spaltenZresult, \x=XY\x}%
    }%
    \gdef\spaltenZresultt{\ReplaceStr{\spaltenZresult}}

}



\newcommand{\spaltenL}[1]{%
    \gdef\spaltenLresult{|} %
    \foreach \x in {1,...,#1}%
    {%
        \global\edef\spaltenLresult{|l \spaltenLresult}%
    }%
}



\newcommand{\autotabell}[2]{

    \spaltenL{#1}
    %\spaltenZ{#2}
    \begin{table}[htb]
        \centering
        \csvreader[tabular=\spaltenLresult]{grad.csv}{1=\1, 2=\2, 3=\3, 4=\4, 5=\5}{\1 & \2 & \3 & \4 & \5}
    \end{table}
}

\begin{document}
    \autotabell{5}{5}

\spaltenZ{5} %just to test it
\spaltenZresultt    %just to test it

\end{document}

as you can see the variable "spaltenZresultt" gives "1=\1, 2=\2,3=\3,4=\4,5=\5" But if i add this variable into the new command instead of "1=\1, 2=\2,3=\3,4=\4,5=\5"

\newcommand{\autotabell}[2]{

    \spaltenL{#1}
    \spaltenZ{#2}
    \begin{table}[htb]
        \centering
        \csvreader[tabular=\spaltenLresult]{grad.csv}{\spaltenZresultt}{\1 & \2 & \3 & \4 & \5}
    \end{table}
}

this will result in an error saying: Undefined control sequence. {\1 & \2 & \3 & \4 & \5}. But in my opinion it should be defined through \spaltenZresultt What am I doing wrong ?

Any help would be appreciated. And thx to all of you since most of the upper part is from here ^^

  • At the time of usage \spaltenZresultt is not defined (and not expandable) – user31729 Oct 22 '17 at 21:23
  • hmm sorry i don't see, why\spaltenZresultt is not defined i can do \begin{document} \spaltenZ{5} \spaltenZresultt \end{document} and it will give me the correct output. – T.Fischer Oct 22 '17 at 22:28
  • Related: tex.stackexchange.com/questions/367979/… – Andrew Oct 22 '17 at 23:09
  • @T.Fischer: Your initial code has %\spaltenZ{5}, in \autotabell, so it is never called, and that's why \spaltenZresultt would fail, but the main reason is still that it is not expandable – user31729 Oct 23 '17 at 15:37

Your Answer

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

Browse other questions tagged or ask your own question.