Graphics, Figures & TablesHow To Resize The Height of Cell Aligned Center

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

How To Resize The Height of Cell Aligned Center

Post by fractal.generator »

Dear all,

The objective is to have a tabular with the following properties

1. the cells are aligned both vertically and horizontally.

2. the cell height can be changed without affecting the content size.

I have tried to solve this problem using rule of zero width as follows

Code: Select all

\begin{tabular}{m{0pt}*{3}{|>{\centering\arraybackslash$}m{3cm}<{$}}|}\hline
\rule{0pt}{1cm}	&a	& x         & \sin x 			\\\hline
\rule{0pt}{1cm}	&b	& y	 & \cos y 	              \\\hline
\rule{0pt}{1cm}	&c	& z	 & \frac{x}{y}                     \\\hline
\end{tabular}
The first column (as an auxilary column) is inserted to manipulate the cell height.
The formated tabular, unfortunately, shows this column that I actually want to hide.


Is there any trick to solve it?


Thank you in advance.



Hayashi

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

How To Resize The Height of Cell Aligned Center

Post by localghost »

A version with booktabs.

Code: Select all

\begin{table}[!ht]
  \begin{tabular}{*{3}{>{\centering\arraybackslash$}m{3cm}<{$}}}\toprule
    a & x & \sin x \\ \midrule
    b & y & \cos y \\ \midrule
    c & z & \frac{x}{y} \\ \bottomrule
  \end{tabular}
  \caption{Table}\label{tab:table}
\end{table}
Another version with stretched height.

Code: Select all

\begin{table}[!ht]
  \setlength{\arraystretch}{1.3}
  \begin{tabular}{*{3}{|>{\centering\arraybackslash$}m{3cm}<{$}}|}\hline
    a & x & \sin x \\ \hline
    b & y & \cos y \\ \hline
    c & z & \frac{x}{y} \\ \hline
  \end{tabular}
  \caption{Table}\label{tab:table}
\end{table}

Best regards
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

How To Resize The Height of Cell Aligned Center

Post by fractal.generator »

@localghost,

Thanks very much for replying.

1. booktabs package does not suits my need because I want to change its height manually instead of automatically adjusted.

2. \arraystretch does NOT make the content vertically aligned.
Correction : \arraystretch should be modified using \renewcommand rather than \setlength. ;)

I tried the following trick that suits what I want.
\newcolumntype is used here only to make the column specifier easy to read.

Code: Select all

\documentclass{book}
\usepackage{array}
\pagestyle{empty}
\begin{document}

%Defines math mode cell aligned horizontally and vertically.
%The cell width is specified by m's argument, e.g., 2cm.
\newcolumntype{C}{>{\centering\arraybackslash$}m{2cm}<{$}}

%Defines zero-width "helper" cell.
%Its intercolumn spaces at both sides are suppressed by @{}.
%\rule{0pt}{15mm} create zero-width content of height, for example, 15mm.
\newcolumntype{H}{@{}>{\rule{0pt}{15mm}}m{0pt}@{}}

\centering
\begin{tabular}{H*{3}{|C}|}	\hline
 &a  	& x   	& \sin x     	\\\hline
 &b   & y    	& \cos y     	\\\hline
 &c   & z    	& \frac{x}{y}    	\\\hline
\end{tabular}
 
\end{document}
Here is the screenshot :

Image


If there is any more efficient trick, please let me know.

Thank you for your attention.


Hayashi.
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

How To Resize The Height of Cell Aligned Center

Post by fractal.generator »

My previous code :

Code: Select all

\documentclass{book}
\usepackage{array}
\pagestyle{empty}
\begin{document}

%Defines math mode cell aligned horizontally and vertically.
%The cell width is specified by m's argument, e.g., 2cm.
\newcolumntype{C}{>{\centering\arraybackslash$}m{2cm}<{$}}

%Defines zero-width "helper" cell.
%Its intercolumn spaces at both sides are suppressed by @{}.
%\rule{0pt}{15mm} create zero-width content of height, for example, 15mm.
\newcolumntype{H}{@{}>{\rule{0pt}{15mm}}m{0pt}@{}}

\centering
\begin{tabular}{H*{3}{|C}|}	\hline
 &a  	& x   	& \sin x     	\\\hline
 &b   & y    	& \cos y     	\\\hline
 &c   & z    	& \frac{x}{y}    	\\\hline
\end{tabular}
 
\end{document}
Here is the screenshot :

Image








Unfortunately, when I merged some rows, the merged items are no longer vertically centered.
Here is the code and the corresponding output:

Code: Select all

\documentclass{book}
\usepackage{array,multirow}
\pagestyle{empty}
\begin{document}

%Defines math mode cell aligned horizontally and vertically.
%The cell width is specified by m's argument, e.g., 2cm.
\newcolumntype{C}{>{\centering\arraybackslash$}m{2cm}<{$}}

%Defines zero-width "helper" cell.
%Its intercolumn spaces at both sides are suppressed by @{}.
%\rule{0pt}{15mm} create zero-width content of height, for example, 15mm.
\newcolumntype{H}{@{}>{\rule{0pt}{15mm}}m{0pt}@{}}

\centering
\begin{tabular}{H*{3}{|C}|}   \hline
&\multirow{2}{*}{a-b} 	& x      	& \sin x        \\ \cline{3-4}
& 											& y       & \cos y        \\ \hline
&c   										& z       & \frac{x}{y}   \\ \hline
\end{tabular}

\end{document}
Image


Anyone knows how to solve it?

Thanks in advance.


regards,

Hayashi
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

How To Resize The Height of Cell Aligned Center

Post by Juanjo »

Try this:

Code: Select all

\documentclass{article}
\usepackage{array,multirow}
   
\begin{document}

\newcolumntype{M}{>{\centering\arraybackslash$\displaystyle}p{2cm}<{$}}
\newcommand{\ST}{\hspace{\tabcolsep}\rule[-20pt]{0pt}{46pt}}

\begin{tabular}{|@{\ST}*{3}{M|}}   
   \hline
   \multirow{2}{*}[-16pt]{$a-b$} & x & \sin x \\ \cline{2-3}
       & y  & \cos y       \\ \hline
    c  & z  & \frac{x}{y}  \\ \hline
\end{tabular}
\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

Re: How To Resize The Height of Cell Aligned Center

Post by fractal.generator »

@Juanjo,

Thanks for replying.

May I know how to get "the magic number" -20, 46, -16?

Thank you in advance.


regards,

Hayashi
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

How To Resize The Height of Cell Aligned Center

Post by Juanjo »

fractal.generator wrote: May I know how to get "the magic number" -20, 46, -16?
The command \rule[-20pt]{0pt}{46pt} prints an invisible rule (since it is 0pt wide) with a total height of 46 pt which starts 20 pt below the baseline. Characters in cells (except the fraction) are about 6 pt high. Adding 20pt above and 20pt below gives 46pt in total. Concerning -16pt, it is just a matter of trial and error.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

Re: How To Resize The Height of Cell Aligned Center

Post by fractal.generator »

Thanks Mr. Juanjo for replying.

regards,

Hayashi
Post Reply