Add an ID to Figure Element

I am using Hugo for Scientific writing.
Hence it is important for me to be able to number and then cross reference to figures in the post.

Using counters I’m able to automatically add numbers to my Figure elements.

Now I’m after being able to cross reference them in the post.
So I have few questions:

  1. Could I have some way to add an ID to the figure elements? It seems the built in shortcode, for some reason, doesn’t expose such option. Here I assume I will be able to easily link to tags in the MarkDown text.
  2. In case there is no simple way to add an ID to the figure element, Is there another way you’d recommend?

Thank You.

Hey! Yes, it’s possible! The way to do it would be:

  1. Add your own copy of layouts/shortcodes/figure.html or themes/[your-theme]/shortcodes/figure.html. Hugo will use this one instead of the standard one.
  2. Copy in the code for the default figure shortcode
  3. Modify it to accept and set an ID parameter.

If you want to number them automatically from Hugo instead of doing it in CSS, that’s possible too! From the shortcode, you can store values in .Page.Scratch, and then increment a stored value every time the shortcode gets used.

I’ve used both of these techniques before, but I’m also writing this from the top of my head, so they might need to be checked / adjusted :slight_smile:

1 Like

Do you think it is reasonable to open GitHub issue with asking adding the ID option to the official figure template?