15

I have a table resembling a matrix with a extra lines for emphasis. This table may also contain frac{} items which do fit into the cell but sort of "hit" the boundaries.

Trouble is, that I have quite a few of those tables. Is there an easy way to increase the padding for all of those tables? Possibly without touching every single table?

A picture illustrating the issue: Notice how eg (x5|x3) looks odd

The source for this looks like this:

\begin{tabular}{c||c|c|c||c|c|c|c||c|c}
        & $x_1$ & $x_2$ & $x_3$            & $x_4$           & $x_5$ & $x_6$ & $x_7$ & RS     & Q     \\ \hline \hline
  $x_2$ & 0     & 1     & $\frac{1}{3}$    & $\frac{1}{3}$   & 0     & 0     & 0     & 50     & 150   \\ \hline
  $x_5$ & 0     & 0     & $\frac{5}{6}$    & $-\frac{2}{3}$  & 1     & 0     & -0.5  & 20     & 24    \\ \hline
  $x_6$ & 0     & 0     & $\frac{1}{12}$   & $-\frac{2}{3}$  & 0     & 1     & -0.25 & 25     & 300   \\ \hline
  $x_1$ & 1     & 0     & 0.25             & 0               & 0     & 0     & 0.25  & 25     & 100   \\ \hline \hline
  $-z$  & 0     & 0     & $\frac{79}{120}$ & $\frac{23}{30}$ & 0     & 0     & -0.575& -172.5 &
\end{tabular}
16

The possibility of changing the multiplicative parameter \arraystretch has already been mentioned in another answer. For the sake of completeness, I think it's useful to also mention the additive parameter \extrarowheight, which one would use as follows:

\usepackage{array}
\setlength\extrarowheight{2pt} % or whatever amount is appropriate

If you include these commands in the document's preamble, all array and tabular-like environments (such as tabular, tabular*, tabularx, longtable, etc.) will be affected by them.

I can't help but comment on some additional aspects of your table. First, all those horizontal and vertical lines make it look very busy; howevr, the multitude of lines probably doesn't add all that much to the table's readability. I'd therefore suggest getting rid of all single-vertical and single-horizontal lines and changing all double lines to single lines. Second, when you have numerical information in the table, it's really important to align the numbers in ways that makes reading them easy. For instance, numbers with decimal points should be aligned on the decimal point; and if some numbers are negative and some are positive, it's helpful to align them in ways that emphasizes that fact.

Here, then, is how I'd suggest you render the table:

enter image description here

\documentclass{article}
\usepackage{dcolumn} % automatically loads the 'array' package
\newcolumntype{d}[1]{D{.}{.}{#1}}
\setlength{\extrarowheight}{2pt}

\begin{document}
$ \begin{array}{c | c c c | r c c d{2.3} | d{4.1} r @{}}
    & x_1 & x_2 & x_3 & x_4 & x_5 & x_6
    & \multicolumn{1}{c|}{x_7} & \multicolumn{1}{c}{RS} & Q \\ 
\hline
x_2 & 0 & 1 & \frac{1}{3}  &  \frac{1}{3} & 0 & 0 &  0    & 50 & 150\\
x_5 & 0 & 0 & \frac{5}{6}  & -\frac{2}{3} & 1 & 0 & -0.5  & 20 & 24 \\
x_6 & 0 & 0 & \frac{1}{12} & -\frac{2}{3} & 0 & 1 & -0.25 & 25 & 300\\
x_1 & 1 & 0 & 0.25         & 0          & 0 & 0 &  0.25 & 25 & 100\\ 
\hline
-z  & 0 & 0 & \frac{79}{120} & \frac{23}{30} & 0 & 0 & -0.575 & -172.5 &
\end{array} $
\end{document}
15

If you use the \arraystretch parameter in the preamble then all tables are going to obey that setting as follows

\documentclass{article}
\renewcommand{\arraystretch}{1.5}
\begin{document}
\begin{table}%
\centering
\begin{tabular}{c||c|c|c||c|c|c|c||c|c}
        & $x_1$ & $x_2$ & $x_3$            & $x_4$           & $x_5$ & $x_6$ & $x_7$ & RS     & Q     \\ \hline \hline
  $x_2$ & 0     & 1     & $\frac{1}{3}$    & $\frac{1}{3}$   & 0     & 0     & 0     & 50     & 150   \\ \hline
  $x_5$ & 0     & 0     & $\frac{5}{6}$    & $-\frac{2}{3}$  & 1     & 0     & -0.5  & 20     & 24    \\ \hline
  $x_6$ & 0     & 0     & $\frac{1}{12}$   & $-\frac{2}{3}$  & 0     & 1     & -0.25 & 25     & 300   \\ \hline
  $x_1$ & 1     & 0     & 0.25             & 0               & 0     & 0     & 0.25  & 25     & 100   \\ \hline \hline
  $-z$  & 0     & 0     & $\frac{79}{120}$ & $\frac{23}{30}$ & 0     & 0     & -0.575& -172.5 &
\end{tabular}
\caption{Some table}
\label{tab:1}
\end{table}
\end{document}

enter image description here

To keep this change local, you can simply place it in the table environment instead of the preamble, say, just after the \centering command. This would cause a temporary change in the global settings.

5

As mentioned in a related answer, arraystretch introduces problems with vertical alignment of cell contents.

The solution with cellspace is to:

  1. Put the following in the preamble:

    % Add vertical padding within each tabular cell.
    \usepackage[math]{cellspace}
        \cellspacetoplimit 4pt
        \cellspacebottomlimit 4pt
    
  2. Use column specifiers Sl, Sc, Sr, Sp{...} instead of l, c, r, p.

    (Unless you are using the siunitx package, in which case use Cl, Cc, Cr, Cp{...}. This is because the siunitx package has its own S column type, and takes care to rename cellspace's column types to C. Though something seems to be wrong with the Cp{...}, it seems.)

Compare:

enter image description here

Code for the above:

\documentclass{article}

% Add vertical padding within each tabular cell.
\usepackage[math]{cellspace}
    \cellspacetoplimit 4pt
    \cellspacebottomlimit 4pt

\begin{document}
Default:

    \begin{tabular}{|l|l|} \hline
    $\Upsilon_3$    & Description [Units] \\\hline
    $\Pi_i$         & Description [Units] \\\hline
    \end{tabular}

    \vspace{2em}

Arraystretch:

    {\renewcommand{\arraystretch}{2}
    \begin{tabular}{|l|l|} \hline
    $\Upsilon_3$    & Description [Units] \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{1em} \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{2em} \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{3em} \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{4em} \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{5em} \\\hline
    \end{tabular}}


With \verb|cellspace| (don't forget the \verb|Sl| cell specifiers):

    \begin{tabular}{|Sl|Sl|} \hline
    $\Upsilon_3$    & Description [Units] \\\hline
    $\Pi_i$         & Description [Units] \\\hline
    \end{tabular}

    \vspace{2em}

    \begin{tabular}{|Sl|Sl|} \hline
    $\Upsilon_3$    & Description [Units] \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{1em} \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{2em} \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{3em} \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{4em} \\\hline
    $\Pi_i$         & Description [Units] \rule{1em}{5em} \\\hline
    \end{tabular}
\end{document}
4

http://www.tex.ac.uk/cgi-bin/texfaq2html?label=struttab discusses the problem.

it mentions booktabs.sty, cellspace.sty, makecell.sty and tabls.sty

you do of course make the problem look "super-bad" by having all those horizontal rules. the docs of booktabs explain why those aren't as good a thing as Lamport seems to imagine in the latex book.

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.