Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
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 Jun 3 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 Jun 3 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. –  Steven B. Segletes Jun 3 at 14:39

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
    
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 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 at 4:48
    
I am sorry. I want to create a table centered in the middle of the paper. –  Edy Jo Jun 4 at 13:48
1  
@EdyJo \begin{table}[!ht]\centering <copy the pgfplotstypeset command here>...\end{table} –  cmhughes Jun 5 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}

Result

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.