23

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}
3
  • 2
    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
    Commented Jun 3, 2014 at 14:28
  • 3
    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
    Commented Jun 3, 2014 at 14:33
  • 1
    @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. Commented Jun 3, 2014 at 14:39

2 Answers 2

9

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},
        every head row/.style={
            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.

screenshot

% 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},
        every head row/.style={
            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.

4
  • 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
    Commented Jun 4, 2014 at 0:01
  • @EdyJo could you clarify what you want to do? You want each column to be a centered paragraph style?
    – cmhughes
    Commented Jun 4, 2014 at 4:48
  • I am sorry. I want to create a table centered in the middle of the paper.
    – Edy Jo
    Commented Jun 4, 2014 at 13:48
  • 1
    @EdyJo \begin{table}[!ht]\centering <copy the pgfplotstypeset command here>...\end{table}
    – cmhughes
    Commented Jun 5, 2014 at 0:13
19

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}

Result

5
  • What if it has exactly 10 rows?
    – Manuel
    Commented Jun 3, 2014 at 14:53
  • @Manuel: See updated answer. Commented Jun 3, 2014 at 14:58
  • An updated answer make table without a bottom rule, @Heiko Oberdiek.
    – Edy Jo
    Commented Jun 3, 2014 at 15:58
  • 1
    @EdyJo: Fixed, \bottomrule was commented for a test run. Commented Jun 3, 2014 at 16:25
  • If we're using a long table, could we detect a new page and force the \resetmidruleV on the first row of a new page? Commented Oct 10, 2020 at 15:07

You must log in to answer this question.

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