11

Is there a package or simple command that will insert an \hline after every line in a tabular environment? It is rather annoying to do it all manually, and it seems like there should be a simple way of doing this (it seems to be a relatively common thing to want to do).

Is there something that does this?

Edit: In case it does not go without saying, I would also like a line above the first row as well.

Clarification: I currently have something that looks like this:

\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{amsmath,amsthm}
\usepackage{amsfonts}

\begin{document}
\begin{tabular}{l}
\hline
a\\
\hline
a\\
\hline
a\\
\hline
a\\
\hline
a\\
\hline
\end{tabular}

\end{document}

I would like to have something that would remove the need for me to type \hline so many times.

10

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.

  • 1
    I am not sure if this is what I am going for. I updated the question to clarify. – soandos May 1 '13 at 9:23
  • @soandos Why isn't it what you need? (even though it is not what you asked for) If you are happy to use \\ at the end of a row for an unlined row, surely typing \nl at the end of a row for a lined row is no more of a burden? – David Carlisle May 1 '13 at 9:45
  • I originally misunderstood. It does do what I want, but I was just expecting something else (an environment that did this for me and I just had to use the `\\` ). – soandos May 1 '13 at 10:00
  • There is an answer that does (almost) exactly what OP requested in a pre-TeX.SE answer over in StackOverflow make LaTeX draw a \hline between each line in tabular without using \hline?. (I don't understand how it works, though, and will post a question in TeX.SE asking for an explanation.) – Mars Aug 28 '13 at 22:26
7

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}
  • This doesn't seem to work with the array package. Is there another way to do this that does? – Peter Flynn Nov 14 '16 at 11:33
  • @PeterFlynn See edit. – Jonathan Komar Nov 14 '16 at 12:22
3

If you are used to vim and vim-latex you could add this to your ~/.vim/ftplugin/tex.vim

:call IMAP('ETA', "\\begin{table}[<+htbp+>]\<CR>\\centering\<CR>\\begin{tabular}{<+dimensions+>}\<CR>\\hline\<CR><++>\<CR>\\end{tabular}\<CR>\\caption{<+Caption text+>}\<CR>\\label{tab:<+label+>}\<CR>\\end{table}<++>", 'tex')
:call IMAP('LHL', "\\\\ \\hline\<CR>", 'tex')

Then, if you type ETA (like Environment TAble) while you are in the insertion mode, it will be automaticaly replaced by :

\begin{table}[<+htbp+>]
  \centering
  \begin{tabular}{<+dimensions+>}
    \hline
    <++>
  \end{tabular}
  \caption{<+Caption text+>}
  \label{tab:<+label+>}
\end{table}<++>

And if you type LHL (like Line HLine) it will automatically add \\ \hline at the end of the current line and begin a new line.

  • I'm not using vim... I would like a LaTeX solution, not a vim one – soandos May 1 '13 at 6:27
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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