# How to make 4 equal paddings (left, right, top, bottom) in a table cell?

I want each cell in a tabular to have 4 equal paddings of exactly 15pt each.

How to make 4 equal paddings (left, right, top, bottom) in a table cell?

\documentclass{article}
\usepackage{array,calc,longtable,ragged2e,pstricks}
\psset{linecolor=red}
\tabcolsep=1pt
\arrayrulewidth=1pt

\newcolumntype{A}[1]{>{\Centering}m{#1\linewidth-2\tabcolsep-1.5\arrayrulewidth}}

\def\pic{\begin{pspicture}(4,3)\psframe*(4,3)\end{pspicture}}

\begin{document}
\begin{longtable}{|*2{A{0.5}|}}\hline
\pic&\pic\tabularnewline\hline
\end{longtable}
\end{document}


Herbert's solution below apparently does not support displayed equations. Please compile with xelatex or latex-dvips-ps2pdf.

\documentclass{article}
\usepackage{array,amsmath,longtable,ragged2e,pst-node,varwidth}
\psset{linecolor=blue}
\tabcolsep=1pt
\arrayrulewidth=1pt
\newsavebox\TBox

\def\picB{\begin{pspicture}(4,3)\psframe*(4,3)\end{pspicture}}

\newenvironment{saveTBox}
{\begin{lrbox}{\TBox}\varwidth{\linewidth}}
{\endvarwidth\end{lrbox}%
\fboxrule=0pt\fboxsep=15pt\fbox{\usebox\TBox}}

\newcolumntype{B}{@{}>{\saveTBox}c<{\endsaveTBox}@{}}

\begin{document}

\begin{longtable}{|B|B|B|}\hline
$\rnode[r]{A}{y=f(x)}$ & \rnode[l]{B}{$\displaystyle y=f(x)$} & \picB
\ncline{A}{B}\tabularnewline\hline
\end{longtable}

\end{document}


-
Just to let you know, I think this is a nicely formatted question; minimal example and helpful small screenshot. – Hendrik Vogt Jan 9 '11 at 8:27

The same with allowing displayed eqiations ...

\documentclass{article}
\usepackage{array,amsmath,longtable,ragged2e,pstricks,varwidth}
\psset{linecolor=red}
\tabcolsep=1pt
\arrayrulewidth=1pt
\newsavebox\TBox

\def\picB{\begin{pspicture}(4,3)\psframe*(4,3)\end{pspicture}}

\newenvironment{saveTBox}
{\begin{lrbox}{\TBox}\varwidth{\linewidth}}
{\endvarwidth\end{lrbox}%
\fboxrule=0pt\fboxsep=15pt\fbox{\usebox\TBox}}

\newcolumntype{B}{@{}>{\saveTBox}c<{\endsaveTBox}@{}}

\begin{document}

\begin{longtable}{|B|B|}\hline
$y=f(x)$ & \picB
\tabularnewline\hline
\end{longtable}

\end{document}

-
thanks for this solution. I will combine it with my real scenario "input-output table" using listings.sty and fancyvrb.sty. – xport Jan 8 '11 at 11:23

You can use the following to enclose horizontal material by a given padding on all sides, without changing its baseline. Using that in a table should give you the desired result, except that you will have to make sure the tabular environment does not insert some additional padding.

\def\padded#1#2{%
\setbox0\hbox{#2}%
\dimen0=\dp0
\setbox2\hbox{\hskip #1\vbox{\vskip #1\box0\vskip#1}\hskip#1}%
\leavevmode\lower\dimen0\box2}


You would use it as \padded{10pt}{Blah pjqy}.

-
Beautiful solution! Thanks. – Alan Munn Jan 7 '11 at 23:30
Not that it makes any difference here, but the % after the \dp0 causes TeX to expand the following token until it reaches something unexpandable (in this case, the \setbox) or until it finds a space token. One place it does matter is at the end of your macro. Try \padded{10pt}{Blah pjqy}3. The fix here is to just leave a space (a newline works) after the 2. Also, you should stick a \leavevmode before \lower, at least if you want to use this to start a paragraph ever. – TH. Jan 8 '11 at 0:49
True. I actually had everything on one line, but when posting I split it and put the %'s there :) – Jan Hlavacek Jan 8 '11 at 1:05
\documentclass{article}
\usepackage{array,calc,longtable,ragged2e,pstricks}
\psset{linecolor=red}
\tabcolsep=1pt
\arrayrulewidth=1pt
\newsavebox\TBox

\def\pic{\begin{pspicture}(-5mm,-5mm)(4.5,3.5)\psframe*(4,3)\end{pspicture}}
\def\picB{\begin{pspicture}(4,3)\psframe*(4,3)\end{pspicture}}

\begin{document}
\begin{longtable}{| c | c|}\hline
\pic&\pic\tabularnewline\hline
\end{longtable}

\fboxrule=0pt\fboxsep=15pt
\begin{longtable}{| c | c|}\hline
\savebox\TBox{$\frac{x^2}{1+2x^2}$}\fbox{\usebox\TBox} & foo \tabularnewline\hline
\end{longtable}

\newenvironment{saveTBox}
{\fboxrule=0pt\fboxsep=15pt%
\begin{lrbox}{\TBox}}
{\end{lrbox}\fbox{\usebox\TBox}}

\newcolumntype{B}{>{\saveTBox}c<{\endsaveTBox}}

\tabcolsep=0pt
\begin{longtable}{| B|B |}\hline
\picB & \picB\tabularnewline\hline
\end{longtable}

\end{document}

-
@Herbert Although this solves the specific problem in the minimal example, it doesn't exactly answer the question: is there a way within tabular to add vertical and horizontal space around the contents of a cell. Your solution just makes the pspicture have a border. Is that the only reasonable solution? Is there a way to do this for arbitrary stuff in a table cell? Using array and \hspace and \rule, within the column definition it's easy to do for text, but for the picture it didn't work. – Alan Munn Jan 7 '11 at 21:02
@Alun: see edited example. In general it makes only sense for columns with a contents of the same width and height. Using \fbox with \fboxrule=0pt is an easy way to get it vertically centered. – Herbert Jan 7 '11 at 21:14
@Herbert: Great. Thanks. The savebox idea is very useful. (BTW you have an extra \newsavebox command in the code, so it' won't compile as is. – Alan Munn Jan 7 '11 at 22:33
@Alan: yes, I shouldn't insert only code snippets without testing it. I edited again the example and modified a bit the environemnt. – Herbert Jan 8 '11 at 7:22
@Herbert, thanks for answering. \fbox does not accept displayed equations. :-) – xport Jan 8 '11 at 10:27

One in Plain with XeTeX:

{
% Those are glues, by the way
\vbox{% Just for the \hrule's
\hrule height 1pt
\halign{&\hfil$\vcenter{\hbox{#}}$\hfil\cr % horizontal/vertical centered
Column with text&\XeTeXpicfile "i-stress-test.jpg" \cr
\XeTeXpicfile "test-pattern.jpg" &\vbox{$$\sum^{\infty}_{i=1} math$$}\cr % added vbox around 
}
\hrule height 1pt
}
}
\bye


-
does it accept true displayed equations? – xport Jan 8 '11 at 11:25
@xport: I updated my example for that, but if all you're doing is displayed maths, then it would make more sense to just redefine the preamble. – morbusg Jan 8 '11 at 14:10