2

I searched the site and fount solution to have same width for cells in a table but I need the table has same width and height. for example the following example shows that the width and height depends on the cell's content: table

\begin{document}

\begin{table}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|c|c|}
\hline

$x^2$&$x_2$\\\hline
 $\overleftarrow{ba}$ & $\alpha$ \\\hline
 $\overbrace{ab}$ & $\Gamma$ \\\hline
$\underbrace{ab}$& $\beta$ \\\hline
$\Longleftrightarrow $ &$\gamma$\\\hline

\end{tabular}
\end{table}
\end{document}

or

\begin{tabular}{|c|c|}
\hline
$x^2$&$x_2$\\\hline
 $\overleftarrow{ba}$ & $\alpha$ \\\hline
 $\overbrace{ab}$ & $\Gamma$ \\\hline
$\underbrace{ab}$& $\beta$ \\\hline
$\Longleftrightarrow $ &$\gamma$\\\hline

\end{tabular}
gusbrs
  • 13,740
mac
  • 123

2 Answers2

4

Adapted from my answer at raster graphics, here using .85cm grid size with .5pt, black border thickness.

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\usepackage{xcolor, graphicx}
\newsavebox{\Bbox}\newsavebox{\Gbox}
\def\thk{.5pt}                   % RULE THICKNESS
\def\gsize{.85cm}                % GRID SIZE
\def\bordercolor{black}          % GRID BORDER COLOR
\def\coresize{\dimexpr\gsize-2\dimexpr\thk}
\setstackgap{S}{-\thk}
\def\Grid#1{\kern-\thk\fboxsep=0pt\relax\fboxrule=\thk\relax%
  \textcolor{\bordercolor}{\fbox{\textcolor{#1}{\rule[-.5\coresize]{\coresize}{\coresize}}}}}
\newcommand\Cgeneric[2][]{\stackinset{c}{.0pt}{c}{-.4pt}{$#1$}{\Grid{#2}}}
\newcommand\Cw[1][]{\Cgeneric[#1]{white}\ignorespaces}
\begin{document}
\begin{table}
\centering
\caption{My caption}\smallskip
\Shortstack{%
      \Cw[x^2]                 \Cw[x_2]\\
      \Cw[\overleftarrow{ba}]  \Cw[\alpha]\\
      \Cw[\overbrace{ab}]      \Cw[\Gamma]\\
      \Cw[\underbrace{ab}]     \Cw[\beta]\\
      \Cw[\Longleftrightarrow] \Cw[\gamma]
}
\end{table}
\end{document}

enter image description here

By using the color facility of the cited answer, with a cell size of 1cm and broadening the border thickness to .75pt with a color of black!50...

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\usepackage{xcolor, graphicx}
\newsavebox{\Bbox}\newsavebox{\Gbox}
\def\thk{.75pt}                   % RULE THICKNESS
\def\gsize{1cm}                % GRID SIZE
\def\bordercolor{black!50}          % GRID BORDER COLOR
\def\coresize{\dimexpr\gsize-2\dimexpr\thk}
\setstackgap{S}{-\thk}
\def\Grid#1{\kern-\thk\fboxsep=0pt\relax\fboxrule=\thk\relax%
  \textcolor{\bordercolor}{\fbox{\textcolor{#1}{\rule[-.5\coresize]{\coresize}{\coresize}}}}}
\newcommand\Cgeneric[2][]{\stackinset{c}{.0pt}{c}{-.4pt}{$#1$}{\Grid{#2}}}
\newcommand\Cw[1][]{\Cgeneric[#1]{white}\ignorespaces}
\newcommand\Ct[1][]{\Cgeneric[#1]{brown!40!gray!40}\ignorespaces}
\newcommand\Cb[1][]{\Cgeneric[#1]{cyan!30}\ignorespaces}
\begin{document}
\begin{table}
\centering
\caption{My caption}\smallskip
\Shortstack{%
      \Cb[x^2]                 \Cb[x_2]\\
      \Cw[\overleftarrow{ba}]  \Ct[\alpha]\\
      \Cw[\overbrace{ab}]      \Ct[\Gamma]\\
      \Cw[\underbrace{ab}]     \Ct[\beta]\\
      \Cw[\Longleftrightarrow] \Ct[\gamma]
}
\end{table}
\end{document}

enter image description here

SUPPLEMENT

At the OP's request, here is a version that allows independent setting of the cell height and width, via \gheight and \gwidth:

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\usepackage{xcolor, graphicx}
\newsavebox{\Bbox}\newsavebox{\Gbox}
\def\thk{.75pt}                   % RULE THICKNESS
\def\gheight{0.65cm}                % GRID SIZE
\def\gwidth{1cm}                % GRID SIZE
\def\bordercolor{black!50}          % GRID BORDER COLOR
\def\coreheight{\dimexpr\gheight-2\dimexpr\thk}
\def\corewidth{\dimexpr\gwidth-2\dimexpr\thk}
\setstackgap{S}{-\thk}
\def\Grid#1{\kern-\thk\fboxsep=0pt\relax\fboxrule=\thk\relax%
  \textcolor{\bordercolor}{\fbox{\textcolor{#1}{\rule[-.5\coreheight]{\corewidth}{\coreheight}}}}}
\newcommand\Cgeneric[2][]{\stackinset{c}{.0pt}{c}{-.4pt}{$#1$}{\Grid{#2}}}
\newcommand\Cw[1][]{\Cgeneric[#1]{white}\ignorespaces}
\newcommand\Ct[1][]{\Cgeneric[#1]{brown!40!gray!40}\ignorespaces}
\newcommand\Cb[1][]{\Cgeneric[#1]{cyan!30}\ignorespaces}
\begin{document}
\begin{table}
\centering
\caption{My caption}\smallskip
\Shortstack{%
      \Cb[x^2]                 \Cb[x_2]\\
      \Cw[\overleftarrow{ba}]  \Ct[\alpha]\\
      \Cw[\overbrace{ab}]      \Ct[\Gamma]\\
      \Cw[\underbrace{ab}]     \Ct[\beta]\\
      \Cw[\Longleftrightarrow] \Ct[\gamma]
}
\end{table}
\end{document}

enter image description here

2

Alain Matthes' answer to a related question, seems to be an interesting approach to this problem. Adapting it, you could try:

\documentclass{article}

\usepackage{array}

\newcolumntype{C}[1]{%
 >{\vbox to 4.5ex\bgroup\vfill\centering}%
 p{#1}%
 <{\egroup}}

\begin{document}

\begin{table}
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|C{5ex}|C{5ex}|}
\hline
$x^2$&$x_2$ \tabularnewline \hline
$\overleftarrow{ba}$ & $\alpha$ \tabularnewline \hline
$\overbrace{ab}$ & $\Gamma$ \tabularnewline \hline
$\underbrace{ab}$& $\beta$ \tabularnewline \hline
$\Longleftrightarrow $ &$\gamma$ \tabularnewline \hline

\end{tabular}
\end{table}
\end{document}

enter image description here

This relies on more standard tabular tools (including the array package), and allows you to set width and height independently too.

gusbrs
  • 13,740