Tabular with automatic \hline

to make a table line followed by a line the markup is \\\hline That's only 8 characters so it's rather hard to come up with a less intrusive markup, given that you have to mark the end of row manually anyway. Perhaps \def\nl{\\\hline} then you only need the three characters \nl at the end of each row.


How does this TeX code generate automatic \hline's in tabular environments?

Automatic Horizonal Lines in a Nutshell

  • Intent is to keep unmodified syntax in the tabular environment, yet alter formatting globally.
  • This will change the meaning of \\ for all tabular environments (that use \@tabularcr).
  • \@tabularcr is the macro you are looking for. It is what \\ represents in tabular rows except when using the array package, see Array Package Changes below.

    1. Clone it: \let \clone@tabularcr \@tabularcr
    2. Reset it: \def\@tabularcr{\clone@tabularcr\hline}

Note that you might be better off using \midrule from the booktabs package instead of \hline. Also note that redefining \\ may mean that you might run into difficulties trying to use \bottomrule, because you might end up with undesired double lines below the last row. There is a way to detect the last row, but it is finicky. I will add it when I get around to it.

Code

\documentclass{article}
\usepackage{fontspec}
\catcode`@=11 % or \makeatletter to change category code of @ to 11 and temporarily to access kernel macro \@tabularcr
\let \clone@tabularcr \@tabularcr
\def\@tabularcr{\clone@tabularcr\hline}
\catcode`@=12 % or \makeatother to restore category code of @ to 12
\begin{document}
\begin{tabular}{lll}
Meaning & col2 & col3 \\
\meaning\\ & col2 & col3 \\
col1 & col2 & col3 \\
\end{tabular}
\end{document}

Array Package Changes

Note that the array package (and any package that loads array e.g. \RequirePackage{array}) changes the definition of \\ to

\relax \iffalse {\fi \ifnum 0=‘}\fi \@ifstar \@xarraycr \@xarraycr

\@tabularcr becomes \@xarraycr. So just replace \@tabularcr with \@xarraycr in the code above for compatibility with the array package.


Notes

In tabular environments, \\ is redefined in the LaTeX kernel at the beginning/end of the tabular environment.

Relevant Snippets from latex.ltx

See latex.ltx (LaTeX kernel) for details.

\def\@tabular{\leavevmode \hbox \bgroup $\let\@acol\@tabacol
   \let\@classz\@tabclassz
   \let\@classiv\@tabclassiv \let\\\@tabularcr\@tabarray}% sets \\ = \@tabularcr

% defines \@tabularcr to look for starred version
\def\@tabularcr{% 
  {\ifnum0=`}\fi\@ifstar\@xtabularcr\@xtabularcr}

% some starred/non-starred code for dealing with table rows
\def\@xtabularcr{\@ifnextchar[\@argtabularcr{\ifnum0=`{\fi}\cr}}
\def\@argtabularcr[#1]{%
  \ifnum0=`{\fi}%
    \ifdim #1>\z@
      \unskip\@xargarraycr{#1}%
    \else
      \@yargarraycr{#1}%
    \fi}

The tabu package offer an \everyrow{\hline} command.

However be aware, that the tabu package is not yet supported if you want to go to html output via htlatex/tex4ht.

Tags:

Rules

Tables