2

In the table given here there is repetition of the columns. Is there a way to loop over them in Latex, so that you don't have to manually write out the columns? That way the table would just load the first 5 columns without the user typing such things as \myhead{\csvcoliv}.

\documentclass{article}
\usepackage{csvsimple,array,filecontents,booktabs}

\begin{filecontents*}{forum_posts_table_1.csv}
    posts per author,sentences per post,quoted sentences per post,EREs per post,mentions per ERE
    3.1,23.83454,2313.27,13453453.8,2464642.43
    4.1,23.83454,2313.27,13453453.8,2464642.43
    5.1,23.83454,2313.27,13453453.8,2464642.43
\end{filecontents*}

\begin{document}

\newcommand\myhead[1]{\parbox[t]{5em}{\centering\bfseries#1\par\kern1mm}}

\csvreader[no head,column count=5,tabular=rrrrr,
  table head=\toprule,
  late after first line=\\\midrule,
  table foot=\bottomrule
  ]%
{forum_posts_table_1.csv}{}{%
  \csviffirstrow{\myhead{\csvcoli} & \myhead{\csvcolii} & \myhead{\csvcoliii}
    & \myhead{\csvcoliv} & \myhead{\csvcolv}
    }{\csvlinetotablerow}
}

\end{document}
1
  • For-loops are used in TeX with the commands \loop and \repeat. Just set an if-statement in front of the \repeat: \loop\StuffToBeDone\ifagain\repeat with \StuffToBeDone the stuff you want to be done in each repetition and \ifagain an if-statement which evaluates to false if the loop should end.
    – Skillmon
    Mar 20, 2017 at 13:46

1 Answer 1

3

After fiddling for a while, I came up with a solution (not using a \loop-\repeat-solution):

\documentclass{article}
\usepackage{csvsimple,array,filecontents,booktabs}
\usepackage{forloop}

\newcount\loopcount
\loopcount=5
\newcounter{loopiti}

\def\continuefortheglory{%
    &\fortheglory%
}

\def\fortheglory{%
    \stepcounter{loopiti}%
    \myhead{%
        \expandafter\csname csvcol\roman{loopiti}\endcsname%
    }%
    \ifnum\loopcount>\value{loopiti}%
    \expandafter\continuefortheglory%
    \fi%
}


\begin{filecontents*}{forum_posts_table_1.csv}
    posts per author,sentences per post,quoted sentences per post,EREs per post,mentions per ERE
    3.1,23.83454,2313.27,13453453.8,2464642.43
    4.1,23.83454,2313.27,13453453.8,2464642.43
    5.1,23.83454,2313.27,13453453.8,2464642.43
\end{filecontents*}

\begin{document}

\newcommand\myhead[1]{\parbox[t]{5em}{\centering\bfseries#1\par\kern1mm}}

\csvreader[no head,column count=5,tabular=rrrrr,
  table head=\toprule,
  late after first line=\\\midrule,
  table foot=\bottomrule
  ]%
{forum_posts_table_1.csv}{}{%
  \csviffirstrow{\myhead{\csvcoli} & \myhead{\csvcolii} & \myhead{\csvcoliii}
    & \myhead{\csvcoliv} & \myhead{\csvcolv}
    }{\csvlinetotablerow}
}

\vskip5mm
using fortheglory:
\vskip5mm

\setcounter{loopiti}{0}
\csvreader[no head,column count=5,tabular=rrrrr,
  table head=\toprule,
  late after first line=\\\midrule,
  table foot=\bottomrule
  ]%
{forum_posts_table_1.csv}{}{%
    \csviffirstrow{\fortheglory}{\csvlinetotablerow}
}


\end{document}

results

EDIT: With this you have to use \loopcount= for every table, that doesn't have the same number of columns as the previous one and have to issue \setcounter{loopiti}{0} before every table, but you could include this in a wrapper command like this:

\newcommand{\glorycol}[1]{\loopcount=#1\setcounter{loopiti}{0}\fortheglory}

Then you just have to use it for a table with, e.g. 5 columns, like this:

\csvreader[no head,column count=5,tabular=rrrrr,
  table head=\toprule,
  late after first line=\\\midrule,
  table foot=\bottomrule
  ]%
{forum_posts_table_1.csv}{}{%
    \csviffirstrow{\glorycol{5}}{\csvlinetotablerow}
}

There might be a better way (for example automaticly determining the number of columns and giving an optional argument just if that fails) but that's out of my skill (at the moment, I hope...).

4
  • I have to say that for-loop constructs are not that well-integrated in TeX as compared to JavaScript+HTML.
    – m33lky
    Mar 20, 2017 at 19:53
  • @m33lky Using for-loops in TeX is not that hard. Problems arise when you want to use them with stuff like & in between (same is true for \if...\fi, making the second \expandafter prior to the \continue...in my code necessary). If this answer is satisfying to your needs, you might accept it, if not feel free to ask stuff.
    – Skillmon
    Mar 20, 2017 at 20:02
  • In your solution you have to manually define the counter loopcount for every table to be imported this way, right?
    – m33lky
    Mar 30, 2017 at 11:51
  • @m33lky please see my edit. (And sorry for weird names of counters and commands)
    – Skillmon
    Mar 30, 2017 at 21:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .