# ConTeXt: Retrieve caption of referenced figure

The title pretty much says it...

I would like to retrieve the caption of a figure I placed earlier in the document.

\starttext
\placefigure[][fig:foo]{My caption}{\externalfigure[foo.png]}

\input knuth

\getcaption{fig:foo} %Output: My caption
\stoptext

• Ups, thought it would be easier and the caption stored in some sort of data structure. In that case I'm way out of my depth Oct 30 '18 at 8:25
• Is it possible to code this in lua? Oct 30 '18 at 8:27
• @Christoph The reference for a float goes into the second optional argument and not the first like you have it in your example. Even when you don’t have a placement option you need the first optional argument when you add a reference, e.g. \placefigure[][fig:foo]{...}{...}. Oct 30 '18 at 16:53
• @WolfgangSchuster Thank you, I just corrected it Oct 30 '18 at 16:57
• @Christoph: You can also use \startplacefigure[reference=fig:foo, location=..., title=....] \externalfigure[...] \stopplacefigure Oct 31 '18 at 4:35

You can show the text of a float title or a section header with the \about command. Another way is to use the \getreference command with the title keyword.

To remove the quotation marks from the \about command change the values of the left and right keys of the \setupreferencing command.

\useMPlibrary[dum]

% \setupreferencing[left=,right=]

\starttext

\placefigure[][fig:foo]{My caption}{\externalfigure[foo.png]}

\samplefile{knuth}

\starttabulate
\NC \tex{getreference} \EQ \getreference[title][fig:foo] \NC\NR
\stoptabulate

\stoptext


• Thanks for the answer. I saw those macros in the Wiki when I looked for my answer, but couldn't use it properly. I found definedataset only later on. Oct 30 '18 at 17:38

Digging into ConTeXt wiki helped me found an answer using datasets

%create a data set with figure captions
\definedataset[Captions]

%macro that will place figure and store its caption data
\def\Myfigure#1#2#3{%
%title is store in Captions data set under the figure label key
\setdataset[Captions][#1][caption=#2]
\placefigure[#1]{#2}{#3}
}
\def\Getcaption#1{%
%call for dataset with the figure label
\datasetvariable{Captions}{#1}{caption}
}
\starttext
\Myfigure{fig:foo}{a retrievable caption}{\externalfigure[cow]}

\input knuth

\bf \Getcaption{fig:foo}
\stoptext


• Thank you. Exactly what I needed. And much more beautiful than Lua ;-) Oct 30 '18 at 9:21