2

I have a very small (probably silly) issue with reference to a subfigure. I am using the following code snippet for the name and reference to a subfigure:

\usepackage{caption}
\usepackage[labelformat=simple]{subcaption}
\renewcommand{\thesubfigure}{Figure \thefigure.(\alph{subfigure}):}
\makeatletter
\renewcommand\p@subfigure{}
\makeatother

Here \ref command to a subfigure \label is showing the following output:

Figure <figure no.>.<(subfigure no.)>:

How to get rid of the punctuation ' : ' in the reference but keep that in the caption label? Please help.

MWE:

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz, pgf}
\usepackage{caption}
\usepackage[labelformat=simple]{subcaption}
\renewcommand{\thesubfigure}{Figure \thefigure.(\alph{subfigure}):}
\makeatletter
\renewcommand\p@subfigure{}
\makeatother
\begin{document}
    \begin{figure}[h]
        \centering
        \begin{subfigure}{0.45\linewidth}
            \centering
            \begin{tikzpicture}[declare function={a=1.5;b=1;}]
                \fill [fill=gray!25] (b,0) node[anchor=south west] {\scriptsize$ (1,0) $} -- (0,b) node[left] {\scriptsize $ (0,1) $} -- (-b,0) node[anchor=north east] {\scriptsize$ (-1,0) $} -- (0,-b) node[right] {\scriptsize$ (0,-1) $} -- cycle;
                \draw [<->] (-a,0) -- (a,0);
                \draw [<->] (0,-a) -- (0,a);
                \draw (0,0) node[above=0.01pt] {\scriptsize$ (0,0) $} circle (1pt);
            \end{tikzpicture}
            \caption{$ B_{1}((0,0);1) $}
        \end{subfigure}
        \unskip\ \vrule\ 
        \hfil
        \begin{subfigure}{0.45\linewidth}
            \centering
            \begin{tikzpicture}[declare function={a=1.5;b=1;}]
                \fill [fill=gray!25] (0,0) node[above=0.01pt] {\scriptsize$ (0,0) $} circle (b);
                \draw [<->] (-a,0) -- (a,0);
                \draw [<->] (0,-a) -- (0,a);
                \draw (b,0) node[anchor=south west] {\scriptsize$ (1,0) $};
                \draw (0,0) node[above=0.01pt] {\scriptsize$ (0,0) $} circle (1pt);
            \end{tikzpicture}
            \caption{$ B_{2}((0,0);1) $}
            \label{Fig: open ball in p=2}
        \end{subfigure}
        \caption{Open unit balls in plane for $ p=1,2,10,\infty $.}
        \label{Fig: p-open balls}
    \end{figure}
    \ref{Fig: open ball in p=2}
\end{document}

Mico
  • 506,678

1 Answers1

2

You asked,

How to get rid of the punctuation : in the reference but keep [it] in the caption label?

I think you should change

\usepackage[labelformat=simple]{subcaption}
\renewcommand{\thesubfigure}{Figure \thefigure.(\alph{subfigure}):}

to

\usepackage[labelformat=simple,labelsep=colon]{subcaption}
\renewcommand{\thesubfigure}{Figure \thefigure.(\alph{subfigure})}

Put differently, you should move the place where the label separator (:) is generated away from where the appearance of the subfigure's number is determined and towards where the appearance of the full caption is determined.


Addendum to address the OP's follow-up question, "Can you please tell me how to produce only \thefigure.(\alph{subfigure}) as the [cross-reference to] a subfigure?" (with further updates)

The answer comes in two parts. First, change

\usepackage[labelformat=simple,labelsep=colon]{subcaption}
\renewcommand{\thesubfigure}{Figure \thefigure.(\alph{subfigure})}

from the preceding answer to

\DeclareCaptionLabelFormat{SubhajitPaul}{Figure \bothIfFirst{#1}{\nobreakspace}#2}
\captionsetup[subfigure]{labelformat=PaulSubhajitPaul,labelsep=colon}
\renewcommand{\thesubfigure}{\thefigure.(\alph{subfigure})}

i.e., get rid of the "Figure" substring in the definition of \thesubfigure, while taking care that "Figure" continues to show up in the caption's label.

Second, start using the cleveref package and its \cref macro to create a cross-referencing call-outs that automatically include the object type -- here: "Figure". One of the many nifty things about \cref is that it can take several arguments, and it will determine automatically if the prefix string needs to be rendered in singular or plural. See below for an application.

For more information on cross-referencing packages and an introduction to the cleveref package, please see the posting Cross-reference packages: which to use, which conflict? (Shameless self-citation alert!)

Finally, a full MWE (minimum working example) and its output:

enter image description here

A quick comment: I think that adding the prefix "Figure " to each subfigure's caption number looks heavy-handed and inelegant. Are you really concerned that your readers won't be able to figure out on their own that "(a)" and "(b)" are the numbers associated with two subfigures? I would hope that your readers aren't that dim.

\documentclass{article}
\usepackage{mathtools}
\usepackage{subcaption}
\DeclareCaptionLabelFormat{SubhajitPaul}{Figure \bothIfFirst{#1}{\nobreakspace}#2}
\captionsetup[subfigure]{labelformat=SubhajitPaul,labelsep=colon}
\renewcommand{\thesubfigure}{\thefigure.(\alph{subfigure})}
\makeatletter
\renewcommand\p@subfigure{}
\makeatother

\usepackage[colorlinks]{hyperref}
\usepackage{cleveref} % be sure to load 'cleveref' AFTER 'hyperref'
\crefname{subfigure}{Figure}{Figures}

\begin{document}
\begin{figure}[h]
\begin{subfigure}{0.45\linewidth}
   \caption{$B_{1}((0,0);1)$} \label{Fig:1a}
\end{subfigure}\hfill
\begin{subfigure}{0.45\linewidth}
   \caption{$B_{2}((0,0);1)$} \label{Fig:1b}
\end{subfigure}
\caption{Open unit balls in plane for $ p=1,2,10,\infty $.}
\end{figure}

\cref{Fig:1b}; \cref{Fig:1a,Fig:1b}
\end{document}
Mico
  • 506,678
  • Thanks. This works. Another small request. Since I hyperlink the references, I do not want to have the word Figure to be hyperlinked while calling the \ref for a subfigure. Can you please tell me how to produce only \thefigure.(\alph{subfigure}) as the \ref for a subfigure? – Subhajit Paul Apr 28 '20 at 21:22
  • 1
    I'll post an addendum to address your follow-up question. – Mico Apr 28 '20 at 21:27
  • Thank you for your addendum. I did not realise my request was not small and a +1 for (Shameless self-citation alert!)! I have two concerns over here. First, the string 'Figure' is not appearing in the subfigure caption label. How to make it appear there? And second, when I modify my \ref command to \renewcommand{\ref}[1]{\cref{#1} in \cpageref{#1}}, and adding \ref{Fig:1a} in your code, it is showing neither the string 'Figure' nor the page number. – Subhajit Paul Apr 29 '20 at 07:45
  • @SubhajitPaul - I've updated the addendum to my answer some more. I couldn't help but insert a remark that implementing your requests ends up creating a heavy-handed, cluttered, and inelegant look. IMNSHO, having three instances of Figure 1 in close proximity is not a good idea. – Mico Apr 29 '20 at 08:44
  • Ok, I consider your comment with sincerity and let me decide whether that looks elegant in the context of my book. But to be honest I am more concerned about the second one. I need to have the page number accompanying the \ref output. Please help with that. – Subhajit Paul Apr 29 '20 at 09:08
  • I suggest you load the varioref package (right before loading hyperref) and use the package's \vref macro to create cross-references that automatically include a call-out to the page the object is located on if the call-out and the object are placed on different pages. E.g., \vref{Fig:1b}. – Mico Apr 29 '20 at 09:12
  • 1
    Ah! This is great! Thanks. – Subhajit Paul Apr 29 '20 at 09:21