GeneralResize a table and align to the top

LaTeX specific issues not fitting into one of the other forums of this category.
krispendyal
Posts: 8
Joined: Wed Nov 21, 2007 8:48 am

Resize a table and align to the top

Post by krispendyal »

I am trying to create some tables using the
table and the tabular environments.To align the tabular to the top I
use the t option after \begin{tabular}.
But if I use resizebox along with this as in

Code: Select all

\resizebox{}{}{\begin{tabular}[t]{}.............
the table doesnt come to the top but sits in the center.does anyone
know how i can get that table to the top even when i am using the
resizebox option???

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Resize a table and align to the top

Post by gmedina »

Please post a minimal working example showing the problem and please describe clearly what you need.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
krispendyal
Posts: 8
Joined: Wed Nov 21, 2007 8:48 am

Resize a table and align to the top

Post by krispendyal »

I have something like this...........

Code: Select all

\documentclass[a4paper,12pt]{article}
 \usepackage{graphicx}
\begin{document}

\begin{table}[!hbt] 
\begin{tabular}{c c} \\ 

\fbox{\resizebox{2.82353cm}{0.6cm}{\begin{tabular}[t]{l l l } 
gfsdf & gsdg & hdgh\\ 
nsg & bsf & bsfb\\ 
sdbs & sgsg & sdgs \\ 
\end{tabular}}}& 

\fbox{\resizebox{6.47059cm}{0.9cm}{\begin{tabular}[t]{l l l l l } 
mhnmf & sdgs & vsdv & vsv & fafa \\
rrq & rqwr & rqrq & qrqqw & fasf \\
rst & gfwq & rfw & fefr & faf \\
sdvs& asdvf & faf & aasd & nfn \\
vsdv & vzsdv & vsv & & \\
\end{tabular}}}\\
\end{tabular}\end{table}

\end{document}


Using the t option seems to stretch the tables and the resize option doesnt seem to be working.
I can remove the t option for the resizing to work but then i need the tables to the top.
balfonsi
Posts: 93
Joined: Wed Mar 14, 2007 12:05 am

Resize a table and align to the top

Post by balfonsi »

Here's a solution, which is not perfect but respects your constraints (top alignment and resizing): use the subfig package with the command
\captionsetup[table]{position=top}. Try this:

Code: Select all

\documentclass[a4paper,12pt]{article}
\usepackage{graphicx}
\usepackage{subfig}
\captionsetup[table]{position =top}%

\begin{document}

\begin{table}[!hbt]
 \subfloat{\fbox{\resizebox{2.82353cm}{0.6cm}{\begin{tabular}{l l l }%[t]
gfsdf & gsdg & hdgh\\
nsg & bsf & bsfb\\
sdbs & sgsg & sdgs \\
\end{tabular}}}}%
\qquad%
\subfloat{\fbox{\resizebox{6.47059cm}{0.9cm}{\begin{tabular}{l l l l l }%
mhnmf & sdgs & vsdv & vsv & fafa \\
rrq & rqwr & rqrq & qrqqw & fasf \\
rst & gfwq & rfw & fefr & faf \\
sdvs& asdvf & faf & aasd & nfn \\
vsdv & vzsdv & vsv & & \\
\end{tabular}}}}%
\end{table}

\end{document}
Another solution would be (I haven't tested it) to use the floatrow package which has
the keyword valign (values: t c b s).

Regards,
B.A.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Resize a table and align to the top

Post by Juanjo »

A different and simpler approach. Try the following code, which self-explains the solution. I attach the pdf file I obtain:

Code: Select all

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{graphicx}

\begin{document}


% -----------------
% Tables are saved in boxes to better handle them in the examples below
\newsavebox{\BoxA}
\newsavebox{\BoxB}

\sbox{\BoxA}{\begin{tabular}[t]{l l l }
gfsdf & gsdg & hdgh\\
nsg & bsf & bsfb\\
sdbs & sgsg & sdgs \\
\end{tabular}}

\sbox{\BoxB}{\begin{tabular}[t]{l l l l l }
mhnmf & sdgs & vsdv & vsv & fafa \\
rrq & rqwr & rqrq & qrqqw & fasf \\
rst & gfwq & rfw & fefr & faf \\
sdvs& asdvf & faf & aasd & nfn \\
vsdv & vzsdv & vsv & & \\
\end{tabular}}
% -----------------

Original tables, perfectly aligned at top:\bigskip\par
\begin{tabular}{c c}
 \fbox{\usebox{\BoxA}} & \fbox{\usebox{\BoxB}}
\end{tabular}
\bigskip

Resized tables. Alignment is lost:\bigskip\par
\begin{tabular}{c c}
 \fbox{\resizebox{2.82353cm}{0.6cm}{\usebox{\BoxA}}} & 
 \fbox{\resizebox{6.47059cm}{0.9cm}{\usebox{\BoxB}}}
\end{tabular}
\bigskip

Possible solution. For both tables, put the same height 
in the second argument of 
\texttt{\textbackslash resizebox}:\bigskip\par
\begin{tabular}{c c}
 \fbox{\resizebox{2.82353cm}{0.6cm}{\usebox{\BoxA}}} & 
 \fbox{\resizebox{6.47059cm}{0.6cm}{\usebox{\BoxB}}}
\end{tabular}
\bigskip

This keeps the aspect ratio of the original tables. There is no distortion:\bigskip\par
\begin{tabular}{c c}
 \fbox{\resizebox{!}{0.3cm}{\usebox{\BoxA}}} & 
 \fbox{\resizebox{!}{0.3cm}{\usebox{\BoxB}}}
\end{tabular}

\end{document}
Attachments
solution.pdf
(43.25 KiB) Downloaded 691 times
krispendyal
Posts: 8
Joined: Wed Nov 21, 2007 8:48 am

Re: Latex Tabular

Post by krispendyal »

I cannot possibly use the same height for all the tables.There can be two tables side by side one with as few as 2 to 3 rows and the other with abt 20 rows.
krispendyal
Posts: 8
Joined: Wed Nov 21, 2007 8:48 am

Re: Latex Tabular

Post by krispendyal »

subfloat works pretty well.thanks a lot

is there any way i can reduce the gap between the captions and the tables???
also can i force section headings in latex like we do with tables.something like !h
Last edited by krispendyal on Fri Nov 23, 2007 12:04 pm, edited 1 time in total.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Resize a table and align to the top

Post by Juanjo »

Have you seen the examples? There are two tables with a different number of rows. However, they are aligned at top, since the boxes containing the tables have the same height, but different total height. Please, note that in this context it is crucial to distinguish between height and total height. The latter is the sum of height and depth.

To be clearer, text is horizontally aligned along an invisible baseline. Most text is only on the baseline. But part of the text also extends below the baseline (look at the letters g or p, for example). The vertical space over the baseline is the height, the vertical space below the baseline is the depth. So, the letter A has height, but no depth. Contrarily, the letter p has both height and depth. A box containing the word Apache has height and also depth (due to the presence of the letter p).

Returning to the tables in the examples, they are written in framed boxes. In order to align their frames at top, what really matters is height: they should extend the same vertical space above the baseline. If the tables have a different number of rows, then they have different depths (i.e. they extend different vertical spaces below the baseline), so having logically different total heights.

Try the examples.
krispendyal
Posts: 8
Joined: Wed Nov 21, 2007 8:48 am

Re: Latex Tabular

Post by krispendyal »

the subfloat seems to be giving me some problems.I am trying to fit in as many tables as possible in a single page.Am not able to do that when i use subfloat(less number of tables come on to a page).
krispendyal
Posts: 8
Joined: Wed Nov 21, 2007 8:48 am

Re: Latex Tabular

Post by krispendyal »

my problem has been solved.thanks a lot for the help guys :)
Post Reply