4

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
5
  • 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
    – Christoph
    Oct 30 '18 at 8:25
  • Is it possible to code this in lua?
    – Christoph
    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
    – Christoph
    Oct 30 '18 at 16:57
  • @Christoph: You can also use \startplacefigure[reference=fig:foo, location=..., title=....] \externalfigure[...] \stopplacefigure
    – Aditya
    Oct 31 '18 at 4:35
2

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{about}        \EQ \about[fig:foo]               \NC\NR
\NC \tex{getreference} \EQ \getreference[title][fig:foo] \NC\NR
\stoptabulate

\stoptext

enter image description here

1
  • 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.
    – sztruks
    Oct 30 '18 at 17:38
2

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 

compilation output

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.