2

I've read the answer given here but haven't been able to modify the code to allow me to insert \hline between each row added to my table.

How can the code by modified to insert \hline each time a new row is defined?

So there would be a line between R1C1 | R1C2 and R2C1 | R2C2 just like there's a line between the headers and the main table contents.

Table without bordered rows

1 Answer 1

2

The insertion of \@gobble at the start of \foorows in the linked answer is meant to gobble the first & of the row. You can insert \hline just before that to always insert a horizontal rule with every \addtotable:

enter image description here

\documentclass{article}

\newcommand\foorows{}

\makeatletter
\newcommand\addtotable[1]{%
  \g@addto@macro\foorows{\hline\@gobble}% Insert horizontal rule and gobble first &
  \@for\tmp:=#1\do{%
    \expandafter\g@addto@macro\expandafter\foorows
      \expandafter{\expandafter&\tmp}%
  }%
  \g@addto@macro\foorows{\\}%
}
\makeatother

\begin{document}

\addtotable{R1C1,R1C2,R1C3,R1C4}
\addtotable{R2C1,R2C2,R2C3,R2C4}

\begin{table}[htp]
  \centering
  \begin{tabular}{ |c|c|c|c| }
    \hline
    Header 1 & Header 2 & Header 3 & Header 4 \\ \hline % Header row
    \foorows
    \hline
  \end{tabular}
\end{table}

\end{document}
1
  • Worked perfectly! Thank you for you prompt and informative reply. Mar 9, 2017 at 7:51

You must log in to answer this question.

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