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.

\newlength{\floatwidth} % The code should determine this value
\newenvironment{mytext}%          environment name
    {\newline\hspace*{-.15in}\begin{minipage}{\floatwidth}\textbf{mytext: }\begin{itshape}}% begin code
        \caption{Figure Caption}
        \subfloat[First figure]{\label{fig:firstfig}\includegraphics{figure1}} \qquad
        \subfloat[Second figure]{\label{fig:secondfig}\includegraphics{figure2}}
            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.
  • What environment or package are you using to create the subfigures? Does the width you are referring to include the caption?
    – Werner
    Commented Nov 11, 2011 at 15:27

2 Answers 2


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).


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.

enter image description here

\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
\NewDocumentCommand{\getHdist}{m o m}{%
\newlength{\figwidth} \newlength{\tblwidth}
\uselengthunit{pt}% Print lengths in pt measurement

  \leftpos{fig:myfig}\includegraphics{figure1} \qquad \includegraphics{figure2}\rightpos{fig:myfig}

  \caption{This is a figure caption} \label{fig:myfig}

    Some & thing \\
    in & a \\
    tabular & that \\
    is & exactly \\
    50pt & + 60pt \\
    wide & !

  \caption{This is a table caption} \label{tbl:mytbl}

\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}.


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .