I often have to use "[" and "]" in various labels when cross-referencing items with formulas of inorganic complexes. Everything worked fine until I noticed a problem with \subref{...} of the subcaption package.

When used in the main caption, any entry with a label including square bracket(s) isn't referred correctly and no letter is assigned to the subfigure. Braces and curly brackets in the labels don't cause any issues:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{hyperref}
    \hypersetup{colorlinks = true}

\begin{document}

Check out Figure~\ref{fig:as[many]square[brackets]as[i]want[and]still[works]fine}. Subfigures~\ref{fig:foo{bar}-a} and \ref{fig:foo(bar)-b} are referenced properly in the main caption, whereas \ref{fig:foo[bar]-c} isn't.

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.3\linewidth}
        \includegraphics[width=\linewidth]{example-image-a}
        \caption{}
        \label{fig:foo{bar}-a}
    \end{subfigure}
    \begin{subfigure}[b]{0.3\linewidth}
        \includegraphics[width=\linewidth]{example-image-b}
        \caption{}
        \label{fig:foo(bar)-b}
    \end{subfigure}
    \begin{subfigure}[b]{0.3\linewidth}
        \includegraphics[width=\linewidth]{example-image-c}
        \caption{}
        \label{fig:foo[bar]-c}
    \end{subfigure}
    \caption{
        \subref{fig:foo{bar}-a} -- OK;
        \subref{fig:foo(bar)-b} -- OK;
        \subref{fig:foo[bar]-c} -- WTF?
    }
    \label{fig:as[many]square[brackets]as[i]want[and]still[works]fine}
\end{figure}

\end{document}

enter image description here

I cannot just replace or stop using square brackets in the labels. How can I make the \subref{...} handle them properly?

As nidhin pointed out in the comments, this most likely has to do with hyperref package. Disabling it actually brings back expected functionality. Unfortunately, I need hyperref too.

  • 1
    I think hyperref is the culprit – nidhin Dec 6 at 17:46
  • @nidhin Yep, you are right. I disabled hyperref and it all works fine now. However, hyperref became an essential package for me and I cannot get rid of it either:( – andselisk Dec 6 at 17:51
  • Is \subref* okay for you? But c in caption won't be a link in that case. – nidhin Dec 6 at 17:57
  • @nidhin Not that I particularly like it, but this definitely can be a workaround. Feel free to post an answer, and if you aware of the mechanism behind this hyperref's behavior, I would really like to know it too:) – andselisk Dec 6 at 18:06
  • 1
    Hm.. \ref works fine.. That also has to be understood. – nidhin Dec 6 at 19:15
up vote 4 down vote accepted

This is a stupid bug in subcaption, I simply forgot to put curly braces around the argument when passing it as optional argument to \hyperref. It's my fault; the hyperref package is fine here.

I will fix it within the next days (and upload a new version to CTAN), until then the following bugfix could be used after loading the subcaption package:

\makeatletter
% fix usage of [] within \subref{...}
\renewcommand*\@subref[2]{%
  \caption@ifundefined\hyperref
    {\subcaption@ref{#1}{#2}}%
    {\hyperref[{#2}]{\subcaption@ref{*#1}{#2}}}}
\makeatother

https://gitlab.com/axelsommerfeldt/caption/issues/40

Updated Answer

The issue is caused by hyperref. When \subref expands, the label is passed as an optional argument (inside square brackets) to hyperref command. Something like this:

\hyperref[fig:foo[bar]-c]{
...
}

Since there is a ] in the label, the optional argument is terminated at that point. Hence the remaining -c] is printed before printing the actual subcaption.

Wrapping the closing square bracket in label inside braces can solve the issue. Change the label to:

\label{fig:foo[bar{]}-c}

enter image description here

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{hyperref}
\hypersetup{colorlinks = true}

\begin{document}

Check out Figure~\ref{fig:as[many]square[brackets]as[i]want[and]still[works]fine}. Subfigures~\ref{fig:foo{bar}-a} and \ref{fig:foo(bar)-b} are referenced properly in the main caption, whereas \ref{fig:foo[bar{]}-c} isn't.

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.3\linewidth}
        \includegraphics[width=\linewidth]{example-image-a}
        \caption{}
        \label{fig:foo{bar}-a}
    \end{subfigure}
    \begin{subfigure}[b]{0.3\linewidth}
        \includegraphics[width=\linewidth]{example-image-b}
        \caption{}
        \label{fig:foo(bar)-b}
    \end{subfigure}
    \begin{subfigure}[b]{0.3\linewidth}
        \includegraphics[width=\linewidth]{example-image-c}
        \caption{}
        \label{fig:foo[bar{]}-c}
    \end{subfigure}
    \caption{
        \subref{fig:foo{bar}-a} -- OK;
        \subref{fig:foo(bar)-b} -- OK;
        \subref{fig:foo[bar{]}-c} -- WTF?
    }
    \label{fig:as[many]square[brackets]as[i]want[and]still[works]fine}
\end{figure}

\end{document}

Another work around is to use \subref* instead of \subref. But it won't create link as shown in figure below.

enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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