7

As I want my captions to be the same width as my tables and figures I used the threeparttable package and the measuredfigure environment. However since, I have issues with referencing and labels of tables and figures in my text. Basically the figure/table number is replaced by ??. I've searched online but cannot find the appropriate help I need to fix this issue.

If I remove the caption and label from the measuredfigure environment and place it just above under \centering it works fine but the caption is no longer the width of the table/figure.

Many thanks

Egm1b

Here is WME

\documentclass[12pt,a4paper,oneside,english,oldfontcommands]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage{xcolor}
\usepackage{csquotes}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{gensymb}
\usepackage{tikz}
\usepackage{wasysym}
\usepackage{tabularx}
\usepackage{cleveref}
\usepackage{threeparttable}

\usepackage{caption}
\captionsetup{justification   = raggedright,
singlelinecheck = false}
\newsubfloat{figure}% Allow subfloats in figure environment

\begin{document}

Below is summary table \Cref{tab:marker}

% Table generated by Excel2LaTeX from sheet 'Sheet6'
\begin{table}
    \centering
    \begin{measuredfigure}
    \caption{Caribou age groups at Nunalleq based on epiphyseal fusion  \label{tab:marker}}
        \begin{tabular}{lcc}
            \toprule
            Age group & NISP  & \% NISP \\
            \midrule
            Adult (fused) & 441   & 88.6\% \\
            Subadult (unfused) & 46    & 9.2\% \\
            Juvenile (unfused) & 11    & 2.2\% \\
            \midrule
            Total & 498   & 100.0\% \\
            \bottomrule
        \end{tabular}%
    \end{measuredfigure}
\end{table}%

And MWE code for figures

Here is example with reference to \Cref{marker2}

\begin{figure} 
\centering 
\begin{measuredfigure} 
\includegraphics[width=5cm]{figures/ch7_caribouwear1} 
\caption{This is a very long caption very long} 
\label{marker2} 
\end{measuredfigure} 
\end{figure}

\end{document}
4
  • 1
    Hi, welcome. Why are you using the measuredfigure environment at all, and not the threeparttable environment? Apr 3, 2017 at 13:12
  • Good point @Torbjørn T! I have replaced 'measuredfigure' with 'threeparttable' and it works fine for tables. But I still have the issue with figures. Here is my MWE for figures that I should have added to my previous MWE: Here is example with reference to \Cref{marker2} \begin{figure} \centering \begin{measuredfigure} \includegraphics[width=5cm]{figures/ch7_caribouwear1} \caption{This is a very long caption very long} \label{marker2} \end{measuredfigure} \end{figure}
    – Egm1b
    Apr 3, 2017 at 13:27
  • You can edit your question (there should be a link right above the comments), so perhaps you could add that code to the code in the question. Apr 3, 2017 at 13:40
  • ok thanks I've just added the example code for figures. Hope that's clearer.
    – Egm1b
    Apr 3, 2017 at 13:49

5 Answers 5

3

In case anyone else is also still having trouble with this issue, I've figured out a workaround using phantomcaptions and resets of the figure counter. It may not be very elegant, but it seems to work, at least for me.

\documentclass[12pt,a4paper,oneside,english]{article}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{cleveref}
\usepackage{threeparttable}
\usepackage{caption}% necessary for \phantomcaption

\usepackage[allfiguresdraft]{draftfigure}% dummy figures


\begin{document}


Below is an example with a reference to \Cref{fig:ex1}:

\begin{figure}[h] 
  \centering 
    \begin{measuredfigure} 
        \includegraphics[draft=false,width=5cm]{example-image-a}
    \caption{This is a very, very, very long caption.} 
    \end{measuredfigure} 
  \addtocounter{figure}{-1}
  \phantomcaption
  \label{fig:ex1} 
\end{figure}

And here is another example with a reference to \Cref{fig:ex2}:

\begin{figure}[h] 
  \centering 
    \begin{measuredfigure} 
        \includegraphics[draft=false,width=5cm]{example-image-b}
    \caption{This is another very, very, very long caption.} 
    \end{measuredfigure} 
  \addtocounter{figure}{-1}
  \phantomcaption
  \label{fig:ex2} 
\end{figure}

\end{document}
1

It looks like a bug to me, measuredfigure loose the label even in a simple document. But normally you know the width of the image and so you don't need the environment and can use a minipage:

\documentclass[]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{threeparttable,graphicx}
\begin{document}
Here is example with reference to \ref{fig:marker1} %missing

\begin{figure}
\centering
\begin{measuredfigure}
\includegraphics[width=5cm]{example-image}
\caption{This is a very long caption very long\label{fig:marker1}}
\end{measuredfigure}
\end{figure}


Here is example with reference to \ref{fig:marker2}

\begin{figure}
\centering
\begin{minipage}{5cm}
\includegraphics[width=\linewidth]{example-image}
\caption{This is a very long caption very long\label{fig:marker2}}
\end{minipage}
\end{figure}

\end{document}
1

Thanks MC Kosh, your workaround works perfectly.

Just on a side note, to make it more user friendly, I defined a function as follows in the beginning of my tex-file:

\newcommand*\MeasuredFigureLabel[1]{
    \addtocounter{figure}{-1}
    \phantomcaption
    \label{#1}
}

This makes this approach very workable. This way everything stays clean looking, even if many figures must be included. This is the adjusted include of the example figure:

\begin{figure}[h] 
  \centering 
    \begin{measuredfigure} 
        \includegraphics[draft=false,width=5cm]{example-image-a}
    \caption{This is another very, very, very long caption.} 
    \end{measuredfigure} 
  \MeasuredFigureLabel{fig:ex1}
\end{figure}
1

I had the same issue, and I solved it asking the figure length, and setting this length for text width. In my specific case, I wanted the "hang" and "justified" formats. The code was the following:

\usepackage[float]   %it isn't necessary for your purpose. If you don't want use, change "H" to "h" in \begin{figure}[H].
\newlength{\imgwidth}    %New variable \imgwidth
\begin{figure}[H]
    \centering
    \includegraphics[height=9 cm]{#1}    %9 cm is my setting. You can use another height / width, ...
    \settowidth{\imgwidth}{\includegraphics[height=9 cm]{#1}} %Put figure width equals to \imgwidth, remembering that's mandatory put the same height (9cm, in this case)
    \captionsetup{width=\imgwidth} %Put captionwidth equals to figure widt.
    \caption{blablabla}
    \label{blablabla}
\end{figure}
0
1

I suggest to just drop measuredfigure and use caption package instead. Its \captionbox command ensures that the caption has the same width as the image.

\documentclass{book}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{lipsum}

\begin{document}

\begin{figure}[ht]
 \centering
 \captionbox{\lipsum[23]\label{img}}%
       {\includegraphics[width=0.5\textwidth]{example-image}}%
\end{figure}

Image reference: \ref{img}.

\end{document}

Caption is perfectly within the image width

It goes without saying that the main purpose of the caption package is actually to provide limitless possibilities for formatting captions, so you might find that it solved a lot of your other problems too. It's a widely popular, well maintained and well compatible package.

After that, it would make sense to just drop the (somewhat outdated) threeparttable package altogether and migrate to tabularray. Its talltblr table incorporates the threeparttable functionality rather nicely, along with a cornucopia of other features.

You must log in to answer this question.

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