3

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 ;).

  • 1
    One approach would be to renew the tabular environment and set the large extrarowheight so it was confined to that environment only. – Thruston Nov 20 '13 at 13:58
  • Without seeing one of your tables it's hard to guess though. – Thruston Nov 20 '13 at 13:59
  • @Thruston See the updated question with an example of a table. – rainer Nov 20 '13 at 14:07
  • Redefinition would be tricky since cases uses array. However you can consider using mathtools' cases* which does not rely on array – Francis Nov 20 '13 at 14:09
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.

  • 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
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 :-) – user11232 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 9 '13 at 14:56

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.