# Labeling and adding caption inside a figure

Seems like this question is rather closed. However, I feel that something is missing there.

Inside a figure environment, the \caption alone is nice, but its real power comes from the combination with \label. This is what enables the user to enumerate the graphics in his/her document automatically and establish cross references.

Is it possible to combine everything together?

That is, have a figure which is something like:

|---------------------------|
|                           |
|       <The figure>        |
|                           |
| Figure 1. And its caption |
|---------------------------|


and labeled using \label{fig:1}, such that its caption is embedded in the figure itself (like the wonderful possibilities offered in the answers to the linked question). Then have something like \ref{fig:1} to refer to this figure.

Is it possible to automate this? Somehow, let hand-coded caption communicate with the \caption+\label+\ref mechanism. I have to say that I don't even know where to start addressing this issue.

-
Can you give more indication about what the question is? I have to say I don't understand it at all. In the accepted answer to the linked question the caption is not embedded in the image file it is just typeset with latex/tikz but overlayed over the image, but caption/label would just work as normal. –  David Carlisle Mar 12 '13 at 20:04
Perhaps the package textpos could be useful for your purpose. Check the manual HERE and look for the option overlay. –  Dox Mar 12 '13 at 20:32

You can use the caption package to set up a style, lets call it overlay that saves the caption for use later. Here is an example:

EDIT example technique slightly change, example extended, now working with hyperref

\documentclass{article}

\usepackage{tikz,caption}
\usepackage{hyperref}
\usetikzlibrary{calc}
\DeclareCaptionFormat{overlay}{\gdef\capoverlay{#1#2#3\par}}
\DeclareCaptionStyle{overlay}{format=overlay}

\begin{document}

\begin{figure}[htp]
\centering
\captionsetup{format=overlay}
\caption{Example.}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0)
{\includegraphics[width=\linewidth]{example-image-a}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\draw[white, fill=gray,opacity=0.5] (0,0) rectangle (1,0.15);
\draw let \p1 = (1,0)
in node[opacity=0.5,text width=\x1,align=center,color=white] at
(0.5, 0.075)
{\capoverlay};
\end{scope}
\end{tikzpicture}
\label{fig:example}
\end{figure}

\begin{figure}[htp]
\centering
\includegraphics[width=3cm]{example-image-a}
\caption{Non-overlay example}
\label{fig:non}
\end{figure}

\begin{figure}[htp]
\centering
\captionsetup{format=overlay}
\caption{Example with caption overlay and long placed on a smaller figure.}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0)
{\includegraphics[width=0.7\linewidth]{example-image-a}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\draw[white, fill=gray,opacity=0.5] (0,0) rectangle (1,0.15);
\draw let \p1 = (1,0)
in node[opacity=0.5,text width=\x1,align=center,color=blue] at
(0.5, 0.075)
{\capoverlay};
\end{scope}
\end{tikzpicture}
\label{fig:example2}
\end{figure}

A reference to Figure~\ref{fig:example} and to Figure~\ref{fig:example2}.

\listoffigures

\end{document}


Note this needs the caption to come before the tikz code. It formats the caption, saves it in a command and reuses that command in the code of the linked question to place text on top of the figure. As set-up now this works with hyperref and does not affect other figures. You can change the formatting of the caption via standard tikz commands on the node.

-