# Automatic width of floats

I need to automatically compute the width of tables and graphics; including the total width of a graphic that contains subfigures. I would also like the code to include a minimum width parameter to be used in case the tables or figures are narrow. I appreciate any assistance provided by members.

I added a minimal working example below. Martin, I would like to see the code you mentioned. Thank you to all who replied.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subfig}
\newlength{\floatwidth} % The code should determine this value
\newenvironment{mytext}%          environment name
{\newline\hspace*{-.15in}\begin{minipage}{\floatwidth}\textbf{mytext: }\begin{itshape}}% begin code
{\end{itshape}\end{minipage}}
\begin{document}
\setlength{\floatwidth}{4.4in}
\begin{figure}[tbp]
\begin{center}
\caption{Figure Caption}
\subfloat[Second figure]{\label{fig:secondfig}\includegraphics{figure2}}
\begin{mytext}
Acceptable! The text in this environment is left justified on the left side of the left-most figure. If the text is long enough, it should wrap at the right side of the right-most figure.
\end{mytext}
\end{center}
\end{figure}
\end{document}

-
What environment or package are you using to create the subfigures? Does the width you are referring to include the caption? –  Werner Nov 11 '11 at 15:27

It is possible to use PDF markers to pinpoint (and reference) the exact location of an element on the page using the zref package with the savepos module activated:

\usepackage[savepos]{zref}% http://ctan.org/pkg/zref


Now you can specify a marker using \zsavepos{<label>}. The x,y coordinates of the marker <label> can be extracted using \zposx{<label>} and \zposy{<label>} respectively.

In the following minimal example, the macros \leftpos{<label>} is used at the left-most point of a figure/table, while \rightpos{<label>} is used the right-most point. Then, use the provided \getHdist{<length>}[<left-label>]{<right-label>} to store the distance between <left-label> and <right-label> in <length>. The second argument [<left-label>] is optional and will default to <right-label> if not specified. The reason for this is because one might typically use the same label for the left and right positions in the figure/table.

\documentclass{article}
\usepackage{printlen}% http://ctan.org/pkg/printlen
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\usepackage[savepos]{zref}% http://ctan.org/pkg/zref
\usepackage{xparse}% http://ctan.org/pkg/xparse
\makeatletter
\newcommand{\leftpos}[1]{\mbox{}\zsavepos{leftpos@#1}}%
\newcommand{\rightpos}[1]{\zsavepos{rightpos@#1}\mbox{}}%
\NewDocumentCommand{\getHdist}{m o m}{%
\IfNoValueTF{#2}%
{\setlength{#1}{\dimexpr\zposx{rightpos@#3}sp-\zposx{leftpos@#3}sp\relax}}%
{\setlength{#1}{\dimexpr\zposx{rightpos@#3}sp-\zposx{leftpos@#2}sp\relax}}%
}
\makeatother
\newlength{\figwidth} \newlength{\tblwidth}
\begin{document}
\uselengthunit{pt}% Print lengths in pt measurement

\begin{figure}[t]
\centering

\rule{323pt}{1pt}
\caption{This is a figure caption} \label{fig:myfig}
\end{figure}

\begin{table}[t]
\centering
\leftpos{tbl:mytbl}%
\begin{tabular}{@{}p{50pt}@{}p{60pt}@{}}
Some & thing \\
in & a \\
tabular & that \\
is & exactly \\
50pt & + 60pt \\
wide & !
\end{tabular}%
\rightpos{tbl:mytbl}

\rule{110pt}{1pt}
\caption{This is a table caption} \label{tbl:mytbl}
\end{table}

\getHdist{\figwidth}{fig:myfig} \getHdist{\tblwidth}{tbl:mytbl}

The width of Figure~\ref{fig:myfig} is \printlength{\figwidth}. The width of Table~\ref{tbl:mytbl} is \printlength{\tblwidth}.

\end{document}


In the above example, the horizontal rules below the figure and table was hard-coded as a rule of fixed width (323pt for the figure and 110pt for the table) merely as reference; to show the correct length calculation.

graphicx was loaded with the demo option to simulate the correct use of figures. It typesets every figure (regardless of the existence of the image) as a 150pt x 100pt black rectangle/box. Remove the demo option in your final document. printlen was also loaded for easy printing of TeX lengths via \printlength{<len>} (with the default length unit specified using \uselengthunit{<unit>}; I chose pt). Finally, xparse was loaded to provide an easy interface for generating document commands/environments.

-

To measure the width of a table or image you need to use a savebox (e.g. \newsavebox\mysavebox) and store it in it with either \sbox\mysavebox{...}), \savebox[<width>}{\mysavebox}{...} or the {lrbox}{\mysavebox} environment. Then you can get the width using \wd\mysavebox. For larger things like multiple subfigures you would need to group them into one box.

The adjustbox package provides an {adjustbox}{<key=value>} environment which stores its content in such a savebox and provides the width as \width which can be used as part of the keys. There is a min width=<length> key which will scale the content to that width if it is smaller. This will work with subfigures as long they don't mind being put into one horizontal box. Otherwise, you might use the minipage=<length> key first to place them into a (vertical) minipage. However, this will scale everything in this environment, including the caption text. I can give you some example code if you give us more precise information about your specific application, best in form of a minimal working example (MWE).

-