464

How do you guys go about controlling column padding in tables?

Update

To clarify, I'm looking for a way to control both vertical and horizontal padding. So, if I had a simple table like in the following code snippet, I could pad the columns and make them look a bit less... ugly. :P

\begin{tabular}{|l|l|}
    \hline
    column 1 & column 2 \\ 
    \hline
    I really would like this less & crammed\\
    \hline
\end{tabular}

I tried this by using the command \vspace but it leaves gaps in the vertical lines.

3
  • 1
    Welcome to TeX.sx! Are you now talking about the horizontal distance between columns (which is what "column padding" is for me) or how to get vertical lines with booktabs? If might be easier if you post a minimal working example (MWE) that illustrates your problem. Oct 15, 2011 at 19:03
  • 4
    Note that booktabs doesn't change how tabular environments are typeset, as long as one doesn't use the new commands provided by the package: vertical rules will touch the horizontal ones input as \hline or \cline.
    – egreg
    Oct 15, 2011 at 19:43
  • 1
    After looking for some help about this, my own fast and cheap approach is, just to share, using: \begin{tabular}{| c | c |} \hline \raisebox{8pt}{\phantom{M}}$\vec{p}=m_i\vec{v}$\raisebox{-8pt}{\phantom{M}} & \raisebox{8pt}{\phantom{M}}$\vec{L}=I\vec{\omega}$\raisebox{-8pt}{\phantom{M}}\\\hline \raisebox{8pt}{\phantom{M}}$\vec{F}=\frac{d}{dt}\vec{p}$\raisebox{-8pt}{\phantom{M}} & \raisebox{8pt}{\phantom{M}}$\vec{\tau}=\frac{d}{dt}\vec{L}$\raisebox{-8pt}{\phantom{M}}\\\hline \end{tabular}
    – Andrestand
    Jan 30, 2014 at 11:03

13 Answers 13

365

Use a default tabular environment without package booktabs and add right before and after the environment:

\bgroup
\def\arraystretch{1.5}%  1 is the default, change whatever you need
\begin{tabular}{|c|...}
...
\end{tabular}
\egroup

and also use column type c instead of l, If you want more horizontal space between the columns then use \setlength\tabcolsep{<whatever length>}.

3
  • 31
    This only answers the vertical stretch part of the question, it seems. Jun 19, 2013 at 8:07
  • the \bgroup and \egroup are not strictly necessary, right?
    – Agile Bean
    Jul 13, 2020 at 9:16
  • No, but it prevents the redefinition of \arraystretch from "leaking out" to other tabular environments in the document. It's more cleanup than anything else.
    – Someone
    Jun 28, 2023 at 23:45
366

The following suggestions are applicable to tabular- and array-like structures and for the most past applies to both text and math mode, including *matrix environments.

Vertical padding

Vertical padding is possible in a global way using @Herbert's answer. That is, to redefine the array stretch factor <factor> using

\renewcommand{\arraystretch}{<factor>}

However, as the name suggests, this is a factor and not a length. So, it would be difficult to provide an adequate factor that would add (say) 15pt above/below each row. There are other options available for this.

Vertical padding is also possible in a manual way or on a per-row basis using the optional parameter to end a tabular line; \\[<len>] where <len> is any familiar TeX length. A final alternative is to use the set the length \extrarowheight provided by the array package.

Here's an example showing the above three possibilities:

\documentclass{article}

\usepackage[landscape]{geometry}
\usepackage{array}

\begin{document}

% =========== FACTOR approach ===========
{\renewcommand{\arraystretch}{2}%
\begin{tabular}{|l|l|}
  \hline
  column 1 & column 2 \\
  \hline
  I really would like this less & crammed \\
  \hline
\end{tabular}} \quad
% =========== LENGTH approaches ===========
\begin{tabular}{|l|l|}
  \hline
  column 1 & column 2 \\[4ex]
  \hline
  I really would like this less & crammed \\[5pt]
  \hline
\end{tabular} \quad
{\setlength{\extrarowheight}{20pt}%
\begin{tabular}{|l|l|}
  \hline
  column 1 & column 2 \\
  \hline
  I really would like this less & crammed \\
  \hline
\end{tabular}}

\end{document}

enter image description here

Note how the "factor" approach is more evenly distributed than the "length" approaches. This is to be expected. However, these techniques can also be combined, if needed. Also, the use of \\[<len>] provides "bottom padding", while setting \extrarowheight adds "top padding". Finally, note the grouping within the example: \renewcommand and \setlength are made local by putting is inside {...}. That is, the value/length of \arraystretch/\extrarowheight revert back to the original value before resetting it at the end of the group.


Horizontal padding

Similar approach to horizontal padding of columns exist. The use of tabularx or tabulary might be considered factor-based, as well as using \extracolsep{\fill}. However, these all pertain to fixed-width tables, with the first being addressed in @cmhughes' answer. Here is a description of tabulary usage, taken from the TeX FAQ entry on Fixed-width tables:

The tabulary package ... provides a way of "balancing" the space taken by the columns of a table. The package defines column specifications C, L, R and J, giving, respectively, centred, left, right and fully-justified versions of space-sharing columns. The package examines how long each column would be "naturally" (i.e., on a piece of paper of unlimited width), and allocates space to each column accordingly.

A length-based approach could include a per-column addition of a separate length using the @{...} "column specifier". Also, modifying the length \tabcolsep (or \arraycolsep if you're working with an array) would do this for all columns, and is therefore more generic. Finally, the array package also provides a means for insert stuff before a column entry and after it using >{<before>} and <{<after>}. Here are some examples:

\documentclass{article}

\usepackage[landscape]{geometry}
\usepackage{array}

\begin{document}

% =========== FACTOR approach ===========
\begin{tabular*}{500pt}{@{\extracolsep{\fill}}|l|l|}
  \hline
  column 1 & column 2 \\
  \hline
  I really would like this less & crammed \\
  \hline
\end{tabular*}

\bigskip

% =========== LENGTH approaches ===========
\begin{tabular}{|@{\hspace{2em}}l@{}|l@{\qquad}|}
  \hline
  column 1 & column 2 \\
  \hline
  I really would like this less & crammed \\
  \hline
\end{tabular} \quad
{\setlength{\tabcolsep}{2em}
\begin{tabular}{|l|l|}
  \hline
  column 1 & column 2 \\
  \hline
  I really would like this less & crammed \\
  \hline
\end{tabular}}

\medskip

\begin{tabular}{|>{\hspace{1pc}}l|l<{\hspace{-2pt}}|}
  \hline
  column 1 & column 2 \\
  \hline
  I really would like this less & crammed \\
  \hline
\end{tabular}

\end{document}

enter image description here

Of course, if all columns should have the same specifier, using the "multiple column specifier" *{<num>}{<col spec>} is a better choice.

In the above examples, geometry was loaded to adjust for a possibly wide display.


Alternative padding approaches

Another way of regulating vertical padding would be to insert so-called (vertical) struts in the form of a zero-width rule (say). For example, using \rule{0pt}{2em}stuff inserts a 2em strut before stuff, thereby increasing the vertical height of the cell containing stuff. Similarly, padding below a cell could be achieved using \rule[-1em]{0pt}{1em}stuff which drops the strut 1em below the baseline.

The same goes for horizontal padding via zero-height struts.

2
  • 10
    Thanks for mentioning \rule[]{}{}stuff; I find it the most helpful of all.
    – kavadias
    Jun 13, 2017 at 23:15
  • 1
    I would say, a disadvantage of \arraystretch: the cell content is not exactly vertically centered anymore :-( as in example below tex.stackexchange.com/questions/31672/…
    – escalator
    May 21, 2021 at 8:07
122

This is an old question, but I've run into the same problem, and all these solutions seemed too complex for my needs, namely in respect to horizontal padding. Looking for a rapid solution similar to the one proposed above for vertical padding (\arraystretch), I've found \setlength{tabcolsep} to be a good candidate.

Applying it on the example, it would be:

\setlength{\tabcolsep}{0.5em} % for the horizontal padding
{\renewcommand{\arraystretch}{1.2}% for the vertical padding
\begin{tabular}{|l|l|}
    \hline
    column 1 & column 2 \\ 
    \hline
    I really would like this less & crammed\\
    \hline
\end{tabular}
}

and the following is the difference:

enter image description here

6
  • 2
    I just saw that @Herbert and Werner talked about this possibility too (after Ctrl-F). I haven't caught it when I first looked at the page. At this point, this is just a shorter, more direct answer, for people in a hurry like me. :)
    – s1l3n0
    Mar 13, 2015 at 11:23
  • 2
    Welcome to TeX.SX! This doesn't fully answer the question asked here. We have other questions for column sep lengths. Furthermore, the spacing shown in the first table of your screenshot is not the standard column sep, so something else in your document is affecting it. Mar 13, 2015 at 11:24
  • 2
    You're right, @PaulGessler. In my case, the problem is caused by the LNCS style, which I've forced to have better-looking tables. However, I found this page looking for 'padding' in the same sense it is used for CSS (~ internal margin within the div). I wrote my answer interpreting the question in these terms. Has it a different meaning in typography?
    – s1l3n0
    Mar 13, 2015 at 11:31
  • I like the simplicity of this answer Apr 6, 2019 at 14:23
  • Best answer until now!
    – Felipe
    Nov 17, 2022 at 14:30
40

Vertical padding There is one drawback using \arraystretch: the content of the cell is no more vertically centred, and for each table you have to determine the optimal value of \arraystretch. Another solution, not mentioned in the above link, is given by the cellspace package that ensures an adjustable minimal distance between the top of a row and the above row (\cellspacetoplimit) and between its bottom and the below row (\cellspacebottomlimit). You then have to prefix the columns format with the letter S (Sl, Sc, Sr, S{p{width}}, &c.). Choose the option [math] if you want it to work also in with the various matrix environments.

Edit:

In case you load the siunitx package, it defines the S column type for numbers aligned on the decimal dot, and takes care of the problem with cellspace, changing the prefix letter to the letter C. Furthermore, recent versions of cellspace let you choose the prefix letter as an option, in the following way: \usepackage[column= some letter]{cellspace}.

Here is an example which shows it works the same whatever be the height of the cell.

    \documentclass{article}
            
            \usepackage[utf8]{inputenc} 
            \usepackage[textheight = 24cm]{geometry} 
            \usepackage[font = sf, justification=raggedright]{caption} 
            \usepackage{array}
            \usepackage{hhline} 
            \usepackage[math]{cellspace}
            \cellspacetoplimit 4pt
            \cellspacebottomlimit 4pt

            \begin{document}

          \begin{table}
           \begin{minipage}[b]{0.35\textwidth}
           \mbox{}\par            
           \caption{Cellspace solution\hfill\vspace{2.5\baselineskip}}
            \begin{tabular}[b]{ >{\bfseries}l|*{6}{| >{$}Sc <{$}}}
             0 & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6}\\
            \hhline{=::======}
              1  & \frac{1}{1} & \frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6} \\
            \hhline{-||------}
              2  & \frac{2}{1} & \frac{2}{2} & \frac{2}{3} & \frac{2}{4} & \frac{2}{5} & \frac{2}{6} \\
            \hhline{-||------}
              3  & \frac{3}{1} & \frac{3}{2} & \frac{3}{3} & \frac{3}{4} & \frac{3}{5} & \frac{3}{6} \\
            \hhline{-||------}
              4  & \frac{4}{1} & \frac{4}{2} & \frac{4}{3} & \frac{4}{4} & \frac{4}{5} & \frac{4}{6} \\
           \hhline{-||------}
              5  & \frac{5}{1} & \frac{5}{2} & \frac{5}{3} & \frac{5}{4} & \frac{5}{5} & \frac{5}{6} \\
            \hhline{-||------}
              6  & \frac{6}{1} & \frac{6}{2} & \frac{6}{3} & \frac{6}{4} & \frac{6}{5} & \frac{6}{6} \\
            \end{tabular}
          \end{minipage}
           \everymath{\displaystyle}
            \begin{tabular}[b]{ >{\bfseries}l|*{6}{| >{$}Sc <{$}}}
             0 & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6}\\
            \hhline{=::======}
              1  & \frac{1}{1} & \frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6} \\
            \hhline{-||------}
              2  & \frac{2}{1} & \frac{2}{2} & \frac{2}{3} & \frac{2}{4} & \frac{2}{5} & \frac{2}{6} \\
            \hhline{-||------}
              3  & \frac{3}{1} & \frac{3}{2} & \frac{3}{3} & \frac{3}{4} & \frac{3}{5} & \frac{3}{6} \\
            \hhline{-||------}
              4  & \frac{4}{1} & \frac{4}{2} & \frac{4}{3} & \frac{4}{4} & \frac{4}{5} & \frac{4}{6} \\
           \hhline{-||------}
              5  & \frac{5}{1} & \frac{5}{2} & \frac{5}{3} & \frac{5}{4} & \frac{5}{5} & \frac{5}{6} \\
            \hhline{-||------}
              6  & \frac{6}{1} & \frac{6}{2} & \frac{6}{3} & \frac{6}{4} & \frac{6}{5} & \frac{6}{6} \\
            \end{tabular}
            \end{table}
\mbox{}
          \begin{table}
           \begin{minipage}[b]{0.3\textwidth}
           \mbox{}\par
          \caption{Arraystretch solution\hfill\vspace{6pt}}
          \renewcommand{\arraystretch}{1.667}
            \begin{tabular}[b]{ >{\bfseries}l|*{6}{| >{$}c <{$}}}
             0 & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6}\\
            \hhline{=::======}
              1  & \frac{1}{1} & \frac{1}{2} & \frac{1}{3} & \frac{1}{4} & \frac{1}{5} & \frac{1}{6} \\
            \hhline{-||------}
              2  & \frac{2}{1} & \frac{2}{2} & \frac{2}{3} & \frac{2}{4} & \frac{2}{5} & \frac{2}{6} \\
            \hhline{-||------}
              3  & \frac{3}{1} & \frac{3}{2} & \frac{3}{3} & \frac{3}{4} & \frac{3}{5} & \frac{3}{6} \\
            \hhline{-||------}
              4  & \frac{4}{1} & \frac{4}{2} & \frac{4}{3} & \frac{4}{4} & \frac{4}{5} & \frac{4}{6} \\
           \hhline{-||------}
              5  & \frac{5}{1} & \frac{5}{2} & \frac{5}{3} & \frac{5}{4} & \frac{5}{5} & \frac{5}{6} \\
            \hhline{-||------}
              6  & \frac{6}{1} & \frac{6}{2} & \frac{6}{3} & \frac{6}{4} & \frac{6}{5} & \frac{6}{6} \\
            \end{tabular}

          \end{minipage}
            \end{table}
            \end{document}

enter image description here

24

The tabularx might also be useful to you. From the documentation

A new environment, tabularx, is defined, which takes the same arguments as tabular*, but modifies the widths of certain columns, rather than the inter column space, to set a table with the requested total width.

Below is a MWE- note that the different width specifications, 250pt and \textwidth, and the results.

\documentclass{report}

\usepackage{tabularx}

\begin{document}

\begin{table}
 \centering
\begin{tabularx}{250pt}{|c|X|c|X|}
 \hline
 \multicolumn{2}{|c|}{Multicolumn entry}                 &   THREE   &   FOUR    \\\hline
    one                 &  The width of 
                            this column depends     
                            on the width of the table   &  three    &           \\\hline
\end{tabularx}
\end{table}

\begin{table}
 \centering
\begin{tabularx}{\textwidth}{|c|X|c|X|}
 \hline
 \multicolumn{2}{|c|}{Multicolumn entry}                 &   THREE   &   FOUR    \\\hline
    one                 &  The width of 
                            this column depends     
                            on the width of the table   &  three    &           \\\hline
\end{tabularx}
\end{table}

\end{document}

enter image description here

12

I needed it for only one specific table and instead of changing how it works through the entirety of document I found that doing the following suited my needs:

\begin{table}
\centering
{\def\arraystretch{1.3}
    \begin{tabularx}{0.75\textwidth}{clX}
        \textbf{\#} & \textbf{User} & \textbf{Occupation}  \\ \hline 
        1 & Test Person 1 & Student \\
        2 & Test Person 2 & Student \\
        3 & Test Person 3 & Something \\ \hline
    \end{tabularx}  
}
\caption{Test Participants for the early Prototype}
\label{tab:testparticipants}
\end{table}

By using the {\def\arraystretch{1.3} ... something ...} you enclose that event within the curly brackets. Hope this helps someone else out there who doesn't need a complicated solution ;)

1
  • 1
    This is exactly the same as Herbert's answer. In your instance (since you've wrapped the tabularx inside a table, you don't need the scope-limiting {...}.
    – Werner
    Jun 2, 2016 at 14:34
11

Vertical spacing inside tabular cells...

...seemed to be a secret to me, but now I found the following:

The vertical size of a tabular cell is at least \baselineskip. This is (by default) composed of:

  1. height = 0.7\baselineskip and
  2. depth = 0.3\baselineskip.

If the content of the cell exceeds these minimum height or depth, that tabular line gets increased vertical size. To know this behaviour was very essential to me when working with so-called vertical struts (see "alternative padding approaches" in this answer above).

An \hline command at the beginning of a (leftmost) cell places a horizontal line with height \arrayrulewidth above the cell. An \hline command in the (leftmost) cell of the next line is places below the cell. So \hline commands increase the total height of tabulars.

1
  • 1
    Knowing this, one might increase cell height. But to decrease it, the implicit strut placed by TeX/LaTeX needs to be chopped. This can either be done via an \arraystretch factor below 1 or by setting the \strutbox manually, which allows for specialized height+depth - see this answer from Heiko Oberdiek!
    – tueftl
    Apr 3, 2013 at 12:19
8

You might want to look at my cals tables package, which supports padding in cells and widths of border lines. See the demo PDF: http://mirrors.ctan.org/macros/latex/contrib/cals/examples/demo.pdf

8

In standard latex tables I use

\noalign{\smallskip}

in front of the row that I want to have more space above. For example, I use this in lines that contain superscripts.

This answer is for row padding only. Other answers cover column padding as well.

2
  • 2
    This is the simplest, cleanest, no dependency answer that fits my needs. The end result for me is: \hline \noalign{\smallskip}.
    – mcp
    Feb 27, 2022 at 19:31
  • Unfortunately it messes up cell boundaries. (It leaves spaces .)
    – Jason D.
    Dec 9, 2023 at 2:13
8

It’s easy peasy to set column and row padding in tables with tblr environment of the new LaTeX3 package tabularray:

\documentclass{article}
\usepackage{tabularray}
\begin{document}
\SetTblrInner{rowsep=2pt,colsep=2pt}
\begin{tblr}{|l|l|l|l|}
\hline
 Alpha   & Beta  & Gamma  & Delta \\
\hline
 Epsilon & Zeta  & Eta    & Theta \\
\hline
 Iota    & Kappa & Lambda & Mu    \\
\hline
\end{tblr}
\end{document}

enter image description here

Have a look at the manual of the package if you want to set padding for only one column or row.

3

My method is probably more of a trick which seems to work, rather than the actual generalised method explained by the experts earlier.

\begin{document}
    \begin{tabular}{|l l|}
        \hline
        \multicolumn{2}{|c|}{ }\\[-0.2cm]
        column 1 & column 2 \\[0.2cm] 
        \hline
        \multicolumn{2}{|c|}{ }\\[-0.2cm]
        I really would like this less & crammed\\[0.2cm]
        \hline
    \end{tabular}
\end{document}

enter image description here

Similar method for the table with vertical line divisions:

\begin{document}
    \begin{tabular}{|l|l|}
        \hline
        \multicolumn{1}{|c|}{ }&\multicolumn{1}{|c|}{ }\\[-0.2cm]
        column 1 & column 2 \\[0.2cm] 
        \hline
        \multicolumn{1}{|c|}{ }&\multicolumn{1}{|c|}{ }\\[-0.2cm]
        I really would like this less & crammed\\[0.2cm]
        \hline
    \end{tabular}
\end{document}

enter image description here

I would say it's just a quick-fix to put in an empty line rather than a permanent solution, but it works. If you want you can make a quick command like

\newcommand{\needspace}{\multicolumn{2}{|c|}{ }\\[-0.2cm]}

to save time, but i think it's a personal choice. Hope it helps.

2

I simply needed a bit of extra whitespace in the left-margins of a two-by-two table. After trying a number of these more complicated approaches and encountering problems, I settled on using the force space option — backslash space or \ — at the start of each new line. It's quick and dirty and wouldn't be appropriate for a complicated table but it did the trick for this little table and spares me the complications of new packages, new commands, potential conflicts, etc.

1
  • You could have added this extra space in the column specification. By default, the white space next to a column is \tabcolsep (resulting in 2\tabcolsep between two columns). You can change the default using @{} next to that column. To add a space to the white space you could use @{\ \hspace{\tabcolsep}}.
    – Skillmon
    Apr 21, 2018 at 21:12
1

Here is my solution for vertical padding based on \vphantom. It is not particularly efficient to use, but at least it produces predictable results and allows me to get what I need on print:

\newcommand{\padedvphantom}[3]{%
  \vtop{%
    \vbox{%
      \vspace*{#2}%
      \hbox{\vphantom{#1}}%
    }%
    \vspace*{#3}%
  }%
}

\begin{tabular}{|l|l|}
    \hline
    column 1 & column 2 \\ 
    \hline
    I really would like this less & crammed\padedvphantom{I}{1ex}{1ex}\\
    \hline
\end{tabular}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .