[Tex/LaTex] Reference to figure label in adjustbox

adjustboxcross-referencingfloats

\begin{figure}
 \begin{adjustbox}{addcode={\begin{minipage}{\width}}{\caption[bla]
{%
bla ..., fig. \protect\ref{fig:fig1}, ...bla.
  }
\end{minipage}}}
\includegraphics[scale=1]{figure.png}
\end{adjustbox}
\label{fig:fig2}
\end{figure}

For some reason the referencing to \ref{fig.fig1} does not work inside the caption of fig.2

Best Answer

Not discussing your adjustbox/minipage solution here, Werner already mentioned

[T]he caption package allows you to specify a \caption width (using \captionsetup{width=<len>}), rather than including it in an adjustbox minipage the way you're doing at the moment.

Either way, here is a solution, that does work. Also provided is the usage of \newenvironment to make your life a little bit easier.

Minimal working example (MWE) I

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{adjustbox}
\begin{document}
\begin{figure}
 \begin{adjustbox}{addcode={%
  \begin{minipage}{\width}
 }{
  \caption[Short Caption]{Similar to Figure \ref{fig:fig2}}\label{fig:fig1}
  \end{minipage}
 }
 }
\includegraphics[scale=1]{figure.png}
\end{adjustbox}
\end{figure}
\begin{figure}
 \begin{adjustbox}{addcode={%
  \begin{minipage}{\width}
 }{
  \caption[Short Caption]{Similar to Figure \ref{fig:fig1}}\label{fig:fig2}
  \end{minipage}
 }
 }
\includegraphics[scale=1]{figure.png}
\end{adjustbox}
\end{figure}
\end{document}

MWE II

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{adjustbox}

\newenvironment{adjustboxwithstuff}[1]{\begin{adjustbox}{addcode={\minipage{\width}}{#1\endminipage}}}{\end{adjustbox}}

\begin{document}
\begin{figure}
    \begin{adjustboxwithstuff}{\caption[Short Caption]{Similar to Figure \ref{fig:fig2}}\label{fig:fig1}}
        \includegraphics[scale=1]{figure.png}
    \end{adjustboxwithstuff}
\end{figure}

\begin{figure}
    \begin{adjustboxwithstuff}{\caption[Short Caption]{Similar to Figure \ref{fig:fig1}}\label{fig:fig2}}
        \includegraphics[scale=1]{figure.png}
    \end{adjustboxwithstuff}
\end{figure}

Output

Output

Related Question