Reading a paper where authors have labels (red circles) in a figure and they are able to render the labels in the figure reference in the text as well. Thought this was pretty neat. Can someone reveal the Latex magic behind doing this ?
You could use the features of the subcaption
package to customize labels and their references. To draw these circled numbers, I think the easiest is to use TikZ.
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{tikz}
\newcommand*{\circlednumber}[1]{%
\tikz[text=white, font=\bfseries, baseline=(X.base)]{%
\node[circle, draw=red!70!black, fill=red!70!black, inner sep=1.5pt] (X) {#1};
}%
}
\renewcommand{\thesubfigure}{\arabic{subfigure}}
\DeclareCaptionLabelFormat{circled}{\circlednumber{#2}}
\captionsetup[subfigure]{labelformat=circled}
\captionsetup{subrefformat=circled}
\begin{document}
References to subfigures \subref{fig:A} and \subref{fig:B}.
\begin{figure}
\begin{subfigure}{.45\textwidth}
\includegraphics[width=\linewidth]{example-image-A}
\caption{A first subfigure.}
\label{fig:A}
\end{subfigure}
\begin{subfigure}{.45\textwidth}
\includegraphics[width=\linewidth]{example-image-B}
\caption{A second subfigure.}
\label{fig:B}
\end{subfigure}
\end{figure}
\end{document}
Note that in this example, the formatting of the references is defined only for subreferences, that is, for references made using the \subref
command. Using simply \ref
would not format the references using the circles.