4

I'm using \extrarowheight (in combination with booktabs) to make the tables in my thesis more readable. However, this also affects the layout of the cases environment, which now look strange; in the following example, the X is placed too far down now.

\documentclass{article}

\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{booktabs}

\setlength{\extrarowheight}{4pt}
\setlength{\belowrulesep}{0pt}

\begin{document}
    \[
    a = \begin{cases}
            X \\
            Y
        \end{cases}
    \]

    \begin{tabular}{llll}\toprule
    $a^b$ & $a_b$ & $a$ & $b$ \\ \midrule
    a & b & c & d \\
    \bottomrule 
    \end{tabular}
\end{document}

enter image description here

Is there any way to ignore the \extrarowheight in the cases environment?

I guess there is some \renewcommand magic that can do this, but I'm not if that's the right way to go, let alone where I would start... I also realize that I could reset \extrarowheight to 0 before the \cases and set it again afterwards, but my DRY alarm tells me not to ;).

rainer
  • 2,077

2 Answers2

5

You can define \extrarowheight and \belowrulesep only inside the tabular environment using etoolbox. It provides a hook \AtBeginEnvironment. You have to add these lines in your preamble:

\AtBeginEnvironment{tabular}{%
\setlength{\extrarowheight}{4pt}
\setlength{\belowrulesep}{0pt}
}%

without anything more.

Full code:

\documentclass{article}

\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{etoolbox}
\AtBeginEnvironment{tabular}{%
    \setlength{\extrarowheight}{4pt}
    \setlength{\belowrulesep}{0pt}
    }%

\begin{document}
    \[
    a = \begin{cases}
            X \\ 
            Y
        \end{cases}
    \]

    \begin{tabular}{llll}\toprule
    $a^b$ & $a_b$ & $a$ & $b$ \\ \midrule
    a & b & c & d \\
    \bottomrule
    \end{tabular}
\end{document}

enter image description here

  • @Harish Kumar, that's a much better way with etoolbox. Do you want me to edit these into a single answer? Is there some way to accept yours rather than mine? – Thruston Nov 20 '13 at 20:47
  • @Thruston I think they can stay as separate answers as they are totally different in approaches (it will improve readability). And regarding accepting, it is up to OP. It is OK to have your answer as accepted :-) –  Nov 20 '13 at 22:18
  • @HarishKumar just as an heads-up (because I just ran into it), you should comment the newlines in the \AtBeginEnvironment, otherwise there will be some spaces before the tabular. – rainer Dec 09 '13 at 14:56
3

Here's an answer with a redefined tabular.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{booktabs}
\newenvironment{airytabular}{\setlength{\extrarowheight}{4pt}\setlength{\belowrulesep}{0pt}%
  \begin{tabular}}{\end{tabular}}
\begin{document}
\[
a = \begin{cases}
        X \\
        Y
    \end{cases}
\]

\begin{airytabular}{llll}\toprule
$a^b$ & $a_b$ & $a$ & $b$ \\ \midrule
a & b & c & d \\
\bottomrule 
\end{airytabular}
\end{document}

enter image description here

Setting up a new environment like this is in general the best approach, because other packages, and indeed the standard class files may use tabular expecting the normal behaviour. However if you really want to redefine it, you can, but you have to capture the previous behaviour to avoid a regression into a loop. Here's one way to do that.

\let\ltxtab\tabular
\let\ltxendtab\endtabular
\renewenvironment{tabular}{\setlength{\extrarowheight}{4pt}\setlength{\belowrulesep}{0pt}%
\ltxtab}{\ltxendtab}

With this re-definition, your tabular environments will have the changed space, but nothing else will be affected, as you desired.

This question has come up before by the way.

Thruston
  • 42,268
  • Thanks, that looks good! Just one nit-pick: is it possible to redefine tabular so that I don't have to go through the whole document and replace all occurrences? – rainer Nov 20 '13 at 14:26
  • Yes... but that's slightly trickier as you have to be careful not to get into a loop. On the whole, I'd recommend using a different name, in case you want to use the normal tabular some where. Does your editor not do global find and replace ? :-) – Thruston Nov 20 '13 at 14:30
  • That's the point, to make sure the layout is consistent throughout the document I don't want to be able to use the normal tabular (at least not without additional effort) ;-).

    So this is more interesting from an best-practices point of view than for the concrete problem, where search and replace will of course work perfectly well (sed FTW ;-) ).

    – rainer Nov 20 '13 at 14:59
  • OK, we can do that. I've updated the answer to show you how. – Thruston Nov 20 '13 at 15:54