For Programmers: Free Programming Magazines  


Home > Archive > Tex > February 2006 > table width variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author table width variable
ash

2006-02-23, 7:06 pm

Is there a variable providing the width of a table? I have searched but
cannot find it. This would be useful in the following code:

\begin{table}
\begin{minipage}{\textwidth}
\centering
.....
\end{minipage}
\end{table}

Instead of \textwidth, I would like to use \tablewidth if there was
such a command. This is because I use footnotes in my table and they
don't align properly when the minipage's width is \textwidth. (I know
the ctable package is supposed to do these things for you, but it seems
more complicated than what I need.)

Thank you.

edl

2006-02-23, 7:06 pm

How about this:
\begin{table}
\newcommand{\tablewidth}{0.5\textwidth}
\begin{center} \begin{minipage}{\tablewidth}
\begin{center}
Hello, world!\par
\ldots\par
Good-bye, world!
\end{center}
\end{minipage} \end{center}
\end{table}


-- Create a command to store the desired size of your table.
-- Start the table, and then turn on centering to center the minipage.
-- Within the minipage turn on centering to center text inside the
minipage.

Does that work for you?

Robin Fairbairns

2006-02-23, 7:06 pm

"ash" <Ashish_Agarwal@cal.berkeley.edu> writes:
>Is there a variable providing the width of a table? I have searched but
>cannot find it. This would be useful in the following code:
>
>\begin{table}
>\begin{minipage}{\textwidth}
>\centering
>....
>\end{minipage}
>\end{table}
>
>Instead of \textwidth, I would like to use \tablewidth if there was
>such a command. This is because I use footnotes in my table and they
>don't align properly when the minipage's width is \textwidth. (I know
>the ctable package is supposed to do these things for you, but it seems
>more complicated than what I need.)


"table" environments don't really have a "width". one presumes you're
thinking about the width of the tabular environment you omitted when
you typed your ellipses?

if so, you can do:

\newsavebox\foo
....

\centering
\begin{tabular}{xyzp}
...
...
\end{tabular}
\setbox\foo\lastbox
\edef\tabularwidth{\the\wd\foo}
\box\foo
\begin{minipage}{\tabularwidth}
<stuff restricted to same width>
\end{minipage}

this come (at second remove) from donald a's threeparttable package.
--
Robin Fairbairns, Cambridge
ash

2006-02-23, 7:06 pm

This solution is certainly better. Thanks. Actually, it led me to try
the following: Instead of centering within the minipage, I just center
the minipage. Unfortunately, centering seems not to affect minipages.

\begin{table}
\begin{center}
\begin{minipage}
\begin{tabular}
....
\end{tabular}
\end{minipage}\\
a second line
\end{center}
\end{table}

The second line is centered, but the first (the minipage) is not.
Centering apparently only works on lines, and I guess a minipage is not
a line. Is that true? Any workaround to this?

Thank you.

Enrico Gregorio

2006-02-23, 7:06 pm

ash <Ashish_Agarwal@cal.berkeley.edu> wrote:

> Is there a variable providing the width of a table? I have searched but
> cannot find it. This would be useful in the following code:
>
> \begin{table}
> \begin{minipage}{\textwidth}
> \centering
> ....
> \end{minipage}
> \end{table}
>
> Instead of \textwidth, I would like to use \tablewidth if there was
> such a command. This is because I use footnotes in my table and they
> don't align properly when the minipage's width is \textwidth. (I know
> the ctable package is supposed to do these things for you, but it seems
> more complicated than what I need.)


Robin's answer covers well the case when there are no footnotes
in the tabular; since you are looking for these, there is another one.

Measure first your table, by putting it into a command.

%%% Define a length parameter
\newlength{\tablewidth}
%%% Define a counter to mimick the footnote counter in minipage
\newcounter{afootnote}
%%% A hack to provide the footnotes
\newcommand{\afootnote}[1]{\stepcounter{
afootnote}%
\textsuperscript{\alph{afootnote}}}
%%% Put the tabular into a command
\newcommand{\mytabular}{%
\begin{tabular}{ccc}
aaa & bbb\afootnote{One footnote} & ccc\\
ddd\afootnote{Another} & eee & fff
\end{tabular}}
%%% Measure the tabular
\settowidth{\tablewidth}{\mytabular}

\begin{table}
\centering % better this than using the center environment
\renewcommand{\afootnote}{\footnote}%
\begin{minipage}{\tablewidth}
\mytabular
\end{minipage}
\end{table}

The purpose of defining \afootnote is to avoid spurious footnotes
appearing in the output. When you measure the table, you are just
supplying the superscript, with the \renewcommand in the table
environment, the footnotes are correctly read in.

Ciao
Enrico
Ulrike Fischer

2006-02-24, 3:59 am

"ash" <Ashish_Agarwal@cal.berkeley.edu> schrieb:

> This solution is certainly better. Thanks. Actually, it led me to try
> the following: Instead of centering within the minipage, I just center
> the minipage. Unfortunately, centering seems not to affect minipages.


Yes it does.

>
> \begin{table}
> \begin{center}
> \begin{minipage}
> \begin{tabular}
> ...
> \end{tabular}
> \end{minipage}\\
> a second line
> \end{center}
> \end{table}
>
> The second line is centered, but the first (the minipage) is not.
> Centering apparently only works on lines, and I guess a minipage is not
> a line. Is that true?


A minipage isn't a line, but part of a line and centering will affect
it. But centering will not affect things in the minipage. If you want
to center things there, put \centering in the minipage.


--
Ulrike Fischer
e-mail: zusätzlich meinen Vornamen vor dem @ einfügen.
e-mail: add my first name between the news and the @.
ash

2006-02-24, 7:03 pm

>>Unfortunately, centering seems not to affect minipages.
>Yes it does.


I see. That is correct. Problem was that the width of the minipage was
\textwidth. So centering had no visual effect.

The basic issue then is to automatically figure out the the width of
the contents you want in a minipage, and that is provided by Enrico's
response. This works perfectly! Just one thing, I don't see why the
"hack to provide the footnotes" is needed. I just used the \footnote
command. It seems to work fine.

Enrico Gregorio

2006-02-24, 7:03 pm

ash <Ashish_Agarwal@cal.berkeley.edu> wrote:

>
> I see. That is correct. Problem was that the width of the minipage was
> \textwidth. So centering had no visual effect.
>
> The basic issue then is to automatically figure out the the width of
> the contents you want in a minipage, and that is provided by Enrico's
> response. This works perfectly! Just one thing, I don't see why the
> "hack to provide the footnotes" is needed. I just used the \footnote
> command. It seems to work fine.


It was just a precaution to ensure correct spacing; I don't know why
it seems useless. The footnotes do not appear anyway because they
are buried inside an \hbox when the tabular is measured.

Ciao
Enrico
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com