5

MWE first.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage[subrefformat=simple,labelformat=simple]{subfig}

%\newcommand{\subfigureautorefname}{\figureautorefname}
%\renewcommand\thesubfigure{(\alph{subfigure})}

\begin{document}
\begin{figure}
\subfloat[]{\label{a}\qquad}
\caption{}
\end{figure}
\begin{tabular}{|c|c|}
\hline
&Result\\\hline
ref&\ref{a}\\\hline
subref&\subref{a}\\\hline
autoref&\autoref{a}\\\hline
subref*&\subref*{a}\\\hline
\end{tabular}
\end{document}

I use package subfig to create subfigures and I want to use both \subref in captions and \autoref in the context.

My desired output of \subref and \autoref are (a) and Figure 1(a) respectively, but I'm not able to achieve them at the same time. I have tried different options for subfig and the solution here(where this question title came from). I also tried to create a new command but failed because I know little about macro or something like that.

Edit: Thanks for @Mico 's solution using package subcaption! I prefer to stick to subfig because it's exhausting to edit all floats in my document.

So I didn't check the subfig manual carefully enough. There are 3 options for typesetting subfloat labels. labelformat sets the label displayed under each subfloat. listofformat sets the output of \subref. subrefformat sets the output of \subref*.

In my case, I only need to add listofformat=subsimple to get rid of the extra pair of parens in \subref, then I could use \renewcommand\thesubfigure{(\alph{subfigure})} to add the parens to \autoref. Full solution below. I didn't notice listofformat because its name can't seem to do anything, compared to subrefformat.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage[listofformat=subsimple,labelformat=simple]{subfig}

\newcommand{\subfigureautorefname}{\figureautorefname}
\renewcommand\thesubfigure{(\alph{subfigure})}

\begin{document}
\begin{figure}
\subfloat[]{\label{a}\qquad}
\caption{}
\end{figure}
\begin{tabular}{|c|c|}
\hline
&Result\\\hline
subref&\subref{a}\\\hline
autoref&\autoref{a}\\\hline
\end{tabular}
\end{document}
4
  • 1
    Welcome to TeX.SE. Does it have to be a solution that involves the subfig package, or are you willing to entertain a solution based on the subcaption package?
    – Mico
    Oct 12, 2019 at 19:57
  • You have to load hyperref last
    – DG'
    Oct 12, 2019 at 20:00
  • Welcome to TeX.SE! Links are not reliable, it can become death after while, so please provide all relevant code in your question only.
    – Zarko
    Oct 12, 2019 at 20:10
  • @DG' - The change you suggest (plus uncommenting two lines in the OP's code) doesn't achieve the OP's formatting objectives.
    – Mico
    Oct 12, 2019 at 20:14

1 Answer 1

5

Here's a subcaption-based solution, which works with both \autoref and \cref (provided by the cleveref package).

enter image description here

\documentclass{article}
\usepackage[demo]{graphicx} % remove 'demo' option in real document
\usepackage[utf8]{inputenc}

\usepackage{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
\captionsetup[subfigure]{labelformat=simple}

\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\begin{document}

\begin{figure}
\centering
\subcaptionbox{First subfigure\label{a}}{\includegraphics[width=0.25\textwidth]{figa}}%
\qquad
\subcaptionbox{Second subfigure\label{b}}{\includegraphics[width=0.25\textwidth]{figa}}
\caption{A figure with two subfigures}
\end{figure}

\begin{tabular}{ll}
\textbackslash ref    &\ref{a}    \\
\textbackslash subref &\subref{a} \\
\textbackslash subref*&\subref*{a}\\
\textbackslash autoref&\autoref{a}\\
\textbackslash Cref   &\Cref{a}
\end{tabular}
\end{document}
2
  • Thanks! What if I want to change the refname like how I set the figureautorefname in hyperref? Optional argument for label may work, but I have hundreds of figures in my document and I don't want to edit every label. Sorry that I didn't include this information in the OP because I didn't expect the solution to not use the autorefname set by hyperref. Oct 13, 2019 at 9:17
  • @putianyi888 - I'm not sure I understand your follow-up question. (The final sentence of your follow-up question contains a double, or maybe even triple, negative. Moreover, my answer doesn't modify \figureautorefname at all.) For sure, just use \renewcommand to change the definition of \figureautorefname. E.g., \renewcommand{\figureautorefname}{Figura}.
    – Mico
    Oct 13, 2019 at 9:31

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.