Graphics, Figures & TablesUsing Math to Format Tables

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
Rishahnu
Posts: 3
Joined: Sun Apr 05, 2009 7:41 pm

Using Math to Format Tables

Post by Rishahnu »

Hi Folks,

I would like to be able to determine table column width based on the text width. Something like the following:

Code: Select all

\begin{table*}[ht] % Table I
			\caption{Microgrid ``Functions''}
			\centering
			\begin{tabular}{p{\textwidth\4}|p{\textwidth/4}|p{\textwidth/2}}
						Actor Name															& Actor Type							& Description											\\
Where I am trying to divide the text width by 2 or 4 to get column width.

Of course it doesn't work, but there must be a way to do this?

Thanks!
Greg

Recommended reading 2024:

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

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

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Using Math to Format Tables

Post by Stefan Kottwitz »

Hi Greg,

you could use coefficients:

Code: Select all

\begin{tabular}{p{.25\textwidth}} ...
Perhaps also have a look the tabularx package.


Stefan
LaTeX.org admin
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Using Math to Format Tables

Post by frabjous »

Along the lines of Stefan's suggestion, I was going to suggest multipliers like 0.25 and 0.50, but:

Code: Select all

\begin{tabular}{p{0.25\textwidth}|p{0.25\textwidth|p{0.5\textwidth}}
Will not work as expected. Why not? Well, it doesn't take into account the padding between the cells (a width of \tabcolsep -- there are six of these, one on each side of each cell), and the width of the lines (a width of \arrayrulewidth -- with your settings there are two). So I recommend defining a width that is calculated by subtracting these from \textwidth:

Code: Select all

\documentclass{article}
\usepackage{lipsum}% for generating dummy text
\begin{document}
\lipsum[1]% a paragraph of dummy text for comparison
\begin{table*}[ht] % Table I
\caption{Microgrid ``Functions''}
\centering
\newlength{\tablewidth}%
\setlength{\tablewidth}{\textwidth}%
\addtolength{\tablewidth}{-6\tabcolsep}%
\addtolength{\tablewidth}{-2\arrayrulewidth}%
\begin{tabular}{p{0.25\tablewidth}|p{0.25\tablewidth}|p{0.5\tablewidth}}
Actor Name  & Actor Type & Description \\ \hline
A & B & C
\end{tabular}
\end{table*}
\end{document}
table.png
table.png (17.97 KiB) Viewed 2365 times
User avatar
Rishahnu
Posts: 3
Joined: Sun Apr 05, 2009 7:41 pm

Re: Using Math to Format Tables

Post by Rishahnu »

Thanks folks! Your suggestions worked!

Cheers,
Greg
Post Reply