# Add \midrule Every 5 Lines Automatically

I wrote a table listing scores of 40 students using booktabs package. How to write midrule command automatically every 5 rows?

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{rclc}
\toprule
Number  & Student Id    & Name  & Score \\
\midrule % adding automatically
1       & 14-001    & Andy  & 75 \\
2       &           &       &    \\
3       &           &       &    \\
4       &           &       &    \\
5       &           &       &    \\
\midrule % adding automatically
6       &           &       &    \\
7       &           &       &    \\
8       &           &       &    \\
9       &           &       &    \\
10      &           &       &    \\
\midrule % adding automatically
11      &           &       &    \\
\bottomrule
\end{tabular}
\caption{A List of Student Scores}
\label{}
\end{table}
\end{document}

• Option 0: redefine \\ so that it increases the value of a counter each time it's used, and then every 5 numbers add a midrule. But I hope there is a better option. – Manuel Jun 3 '14 at 14:28
• You may find that the code to add this automatically would match (or exceed) the code to add 8 \midrules... But that's not the point, right. – Werner Jun 3 '14 at 14:33
• @Werner I agree with you; however, one instance where it makes sense to do it automatically is if you are constantly editing the table by adding and/or removing rows... a weekly roster that is updated, for example. – Steven B. Segletes Jun 3 '14 at 14:39

Here's an option using the pgfplotstable package.

The idea is to read the data into a macro (called edytable below) and then typeset it using the command

    \pgfplotstabletypeset[
columns/Student Id/.style={string type},
columns/Name/.style={string type},
before row={%
\toprule
},
after row=\midrule},
every last row/.style={after row=\bottomrule},
every nth row={5}{before row=\midrule},
]{\edytable}


The particularly relevant part to your question is the line

        every nth row={5}{before row=\midrule},


which instructs pgfplotstable to insert a \midrule on every 5th row.

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}

\pgfplotstableread[col sep = &, row sep = \\]{%
Number  & Student Id    & Name  & Score \\
1       & 14-001    & Andy  & 75 \\
2       &           &       &    \\
3       &           &       &    \\
4       &           &       &    \\
5       &           &       &    \\
6       &           &       &    \\
7       &           &       &    \\
8       &           &       &    \\
9       &           &       &    \\
10      &           &       &    \\
11      &           &       &    \\
}\edytable
\pgfplotstabletypeset[
columns/Student Id/.style={string type},
columns/Name/.style={string type},
before row={%
\toprule
},
after row=\midrule},
every last row/.style={after row=\bottomrule},
every nth row={5}{before row=\midrule},
]{\edytable}

\end{document}


There are lots of different ways that you could read in the data into edytable - I used col sep = &, row sep =\\ simply because that used the code available.

• to center the table, I added this command: \newcolumntype{C}{>{\centering\arraybackslash}p{6mm}}% a centered fixed-width-column from page 17 of PGFPlotTable manual <pgfplots.sourceforge.net/pgfplotstable.pdf>, but doesn't work. – Edy Jo Jun 4 '14 at 0:01
• @EdyJo could you clarify what you want to do? You want each column to be a centered paragraph style? – cmhughes Jun 4 '14 at 4:48
• I am sorry. I want to create a table centered in the middle of the paper. – Edy Jo Jun 4 '14 at 13:48
• @EdyJo \begin{table}[!ht]\centering <copy the pgfplotstypeset command here>...\end{table} – cmhughes Jun 5 '14 at 0:13

The following macro \midruleV sets a \midrule every five rows. Counter midruleV is used to count the rows inside a block of five rows. Therefore the counter is reset before the table and after the header, because the header does not count.

The larger problem is finding a place to put \midruleV. The example puts it after the last cell by escaping the current group twice. Therefore the rows should not end early by omitting cells.

Before the end of the last row \vadjust{\resetmidruleV} prevents that the rule is inserted after a multiple of five rows and the table closes with \midrule and \bottomrule.

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}

\newcounter{midruleV}
\newcommand*{\midruleV}{%
\aftergroup\aftergroup\aftergroup\midruleVaux
}
\newif\ifmidruleV
\makeatletter
\newcommand*{\midruleVaux}{%
\noalign{%
\stepcounter{midruleV}%
\ifnum\value{midruleV}=5 %
\global\midruleVtrue
\setcounter{midruleV}{0}%
\else
\global\midruleVfalse
\fi
}
\ifmidruleV\midrule\fi
}
\newcommand*{\resetmidruleV}{\setcounter{midruleV}{0}}
\makeatother

\begin{document}
\begin{table}[h]
\centering
\resetmidruleV
\begin{tabular}{rclc<{\midruleV}}
\toprule
Number  & Student Id    & Name  & Score \\
\noalign{\resetmidruleV}%
\midrule % adding automatically
1       & 14-001    & Andy  & 75 \\
2       &           &       &    \\
3       &           &       &    \\
4       &           &       &    \\
5       &           &       &    \\
6       &           &       &    \\
7       &           &       &    \\
%8       &           &       &    \\
9       &           &       &    \\
10      &           &       &    \\
11      &           &       &    \vadjust{\resetmidruleV}\\
\bottomrule
\end{tabular}
\caption{A List of Student Scores}
\label{}
\end{table}
\end{document}


• What if it has exactly 10 rows? – Manuel Jun 3 '14 at 14:53
• @Manuel: See updated answer. – Heiko Oberdiek Jun 3 '14 at 14:58
• An updated answer make table without a bottom rule, @Heiko Oberdiek. – Edy Jo Jun 3 '14 at 15:58
• @EdyJo: Fixed, \bottomrule was commented for a test run. – Heiko Oberdiek Jun 3 '14 at 16:25