Graphics, Figures & TablesDetermine table width

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sitex
Posts: 70
Joined: Sat May 09, 2009 12:37 pm

Determine table width

Post by sitex »

Hello,

Is there a way to save for re-use the width of a tabular environment?

Tom

Recommended reading 2024:

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

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

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

Determine table width

Post by gmedina »

Hi Tom,

you can use a \hbox to "store" the tabular material; the width of that box will be the width of the tabular; the following example shows this approach:

Code: Select all

\documentclass{article}

\begin{document}

% storing the tablular material in the box \box0
\setbox0=\hbox{%
  \begin{tabular}{|ccc|}\hline
    column1 & column2 & column3\\\hline
  \end{tabular}}


% \wd0 stores the width of \box0 (i.e., the width of the tabular material)
\noindent\the\wd0

% just a test rule whose width will be equal to that of tha tabular
\noindent\rule{\the\wd0}{1pt}

% now we show the contents of \box0 (i.e., the tabular material)
\copy0

\end{document}
P.S.: I've been really busy these days, but as soon as I can I will take a look at your question about footnotes.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
sitex
Posts: 70
Joined: Sat May 09, 2009 12:37 pm

Re: Determine table width

Post by sitex »

Thank you! I will try your code tomorrow.

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

Determine table width

Post by gmedina »

Just a little comment: \copy0 won't empty the box register (\box0 in my example); you can use \box0 instead if you want to show the contents of the box register (the tabular material, in my example) and then to empty the box register:

Code: Select all

\documentclass{article}

\begin{document}

% storing the tablular material in the box register \box0
\setbox0=\hbox{%
  \begin{tabular}{|ccc|}\hline
    column1 & column2 & column3\\\hline
  \end{tabular}}

% \wd0 holds the width of \box0 (i.e., the width of the tabular material)
\noindent\the\wd0

% just a test rule whose width will be equal to that of the tabular
\noindent\rule{\the\wd0}{1pt}

% now we show the contents of \box0 (i.e., the tabular material)
% without emptying \box0
\copy0

% \wd0 still has the same value since the box register is not empty
\noindent\the\wd0

% now we show the contents of \box0 (i.e., the tabular material)
% emptying \box0
\box0

% \wd0 is zero since the box register is now empty
\noindent\the\wd0

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
sitex
Posts: 70
Joined: Sat May 09, 2009 12:37 pm

Re: Determine table width

Post by sitex »

Hello,

You have a nice solution but, sadly, it will not work in my situation. I wish to use the code in Scientific WorkPlace (or Word). If the tabular environment is inside a command I cannot take advantage of the WorkPlace interface; I can place tabular commands inside another environment that is not inside a command. I should have been more specific about this but the problem did not occur to me until I tried to use your code.

Thanks for your efforts.

Tom
Post Reply