3

When referencing floats using \cref commands, they may sometimes yield an incorrect typesetting. That's especially true when subfloats are included.

An example is when we have float 1 with two subfloats; the range 1a and 2 to 5 will be mistaken as though it is from 1a to 5. Also, if both 1a and 1b are ncluded in the source code referencing, it is supposed to mention all of subfloats included in the source code, not only one of them (see point 2). Typeset output is shown below:

enter image description here

This output is generated using the following source code:

\documentclass[twocolumn]{report}

\usepackage{float}
\usepackage{subfloat}
\usepackage{caption}
\usepackage{cleveref}
\usepackage{paralist}



\begin{document}


    \begin{subfigures}

        % Subfigure 1
        \begin{figure}[H]

            \centering
            \fbox{\Huge Figure 1a}


            \caption{Caption 1}
            \label{fig1a}

        \end{figure}


        % Subfigure 2
        \begin{figure}[H]

            \centering
            \fbox{\Huge Figure 1b}


            \caption{Caption 1b}
            \label{fig1b}

        \end{figure}


    \end{subfigures}


    % Figure 2
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 2}


        \caption{Caption 2}
        \label{fig2}

    \end{figure}


    % Figure 3
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 3}


        \caption{Caption 3}
        \label{fig3}

    \end{figure}


    % Figure 4
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 4}


        \caption{Caption 4}
        \label{fig4}

    \end{figure}


    % Figure 5
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 5}


        \caption{Caption 5}
        \label{fig5}

    \end{figure}


    % Figure 6
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 6}


        \caption{Caption 6}
        \label{fig6}

    \end{figure}



    \newpage
    Figures are correctly cited for different citation formats such as:

    \begin{enumerate}

        \item Multiple: \textbf{\cref{fig5,,fig3,,fig4,,fig2}}
        \item Range: \textbf{\cref{fig5,fig3,fig4}}
        \item Multiple and range: \textbf{\cref{fig2,fig4,fig3,fig6}}

    \end{enumerate}


    \vspace{5mm}
    But once subfigures are included, some citations formats do not work:

    \begin{enumerate}

    \item Multiple (works): \textbf{\cref{fig3,,fig4,,fig2,,fig1a}}
    \item Range: \textbf{\cref{fig1a,fig1b,fig5,fig3,fig4}} (the output should be \textbf{figs. 1a to 1b and 3 to 5})
    \item Multiple and range: \textbf{\cref{fig1a,fig2,fig4,fig3,fig6}} (the output should be \textbf{figs. 1a, 2 to 4 and 6})

    \end{enumerate}


\end{document}

I already have a solution that mitigates the problem, but not with the same expected output above.

6
  • @cfr I was working on the answer when you posted the comment. Jul 8, 2017 at 0:03
  • The best thing to do in such cases is to state that you are going to post a partial/unsatisfactory solution as an initial answer later. Or to include it in your question, if it really doesn't solve the problem. (Once a question has an answer, people are obviously that much less inclined to view it to see if they can help.)
    – cfr
    Jul 8, 2017 at 0:08
  • Is paralist necessary? Is float? Can you reproduce with just subfloat and cleveref?
    – cfr
    Jul 8, 2017 at 0:30
  • float allows for deciding the figures locations using [H] after \begin{figure}. paralist is used to provide a list of cases on how the typeset referencing output looks like. Jul 8, 2017 at 0:40
  • Example produces same output with neither, though. You aren't using paralist at all. For cleveref, see page 26 of the manual: 'Cleveref doesn’t know about the subfloat package, so you have to revert to using \ref for cross-references to sub-figures. (Might be fixed in a future version.)'
    – cfr
    Jul 8, 2017 at 0:54

1 Answer 1

2

enter image description here We can define new float type (subfig) for cleveref

\crefformat{subfig}{fig. #1}
\crefrangeformat{subfig}{figs. #1 to #2}
\crefmultiformat{subfig}{figs. #1}{ and #1}{, #1}{ and #1}
\crefrangemultiformat{subfig}{figs. #1 to #2}{}{}{}

Then in the preamble we define a new command:

\newcommand{\labelsf}[1]{ \label[subfig]{#1} } % a new command for labeling subfigures

labelsf can now be used to label subfigures.

A full source code is shown below:

\documentclass[twocolumn]{report}

\usepackage{float}
\usepackage{subfloat}
\usepackage{caption}
\usepackage{paralist}

\usepackage{cleveref}
\crefformat{subfig}{fig. #1}
\crefrangeformat{subfig}{figs. #1 to #2}
\crefmultiformat{subfig}{figs. #1}{ and #1}{, #1}{ and #1}
\crefrangemultiformat{subfig}{figs. #1 to #2}{}{}{}
\newcommand{\labelsf}[1]{ \label[subfig]{#1} } % a new command for labeling subfigures

\begin{document}


    \begin{subfigures}

        % Subfigure 1
        \begin{figure}[H]

            \centering
            \fbox{\Huge Figure 1a}


            \caption{Caption 1}
            \labelsf{fig1a}

        \end{figure}


        % Subfigure 2
        \begin{figure}[H]

            \centering
            \fbox{\Huge Figure 1b}


            \caption{Caption 1b}
            \labelsf{fig1b}

        \end{figure}


    \end{subfigures}


    % Figure 2
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 2}


        \caption{Caption 2}
        \label{fig2}

    \end{figure}


    % Figure 3
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 3}


        \caption{Caption 3}
        \label{fig3}

    \end{figure}


    % Figure 4
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 4}


        \caption{Caption 4}
        \label{fig4}

    \end{figure}


    % Figure 5
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 5}


        \caption{Caption 5}
        \label{fig5}

    \end{figure}


    % Figure 6
    \begin{figure}[H]

        \centering
        \fbox{\Huge Figure 6}


        \caption{Caption 6}
        \label{fig6}

    \end{figure}



    \newpage
    Figures are correctly cited for different citation formats such as:

    \begin{enumerate}

        \item Multiple: \textbf{\cref{fig5,,fig3,,fig4,,fig2}}
        \item Range: \textbf{\cref{fig5,fig3,fig4}}
        \item Multiple and range: \textbf{\cref{fig2,fig4,fig3,fig6}}

    \end{enumerate}


    \vspace{5mm}
    But once subfigures are included, some citations formats do not work:

    \begin{enumerate}

    \item Multiple (works): \textbf{\cref{fig3,,fig4,,fig2,,fig1a}}
    \item Range: \textbf{\cref{fig1a,fig1b,fig5,fig3,fig4}} (the output should be \textbf{figs. 1a to 1b and 3 to 5})
    \item Multiple and range: \textbf{\cref{fig1a,fig2,fig4,fig3,fig6}} (the output should be \textbf{figs. 1a, 2 to 4 and 6})

    \end{enumerate}


\end{document}

cref now will reference subfigures correctly, but the disadvantage of this solution are:

  • we cannot find the usages of floats by double-clicking on their labels and we also cannot find the float source code by clicking on its label
  • the name of the float can sometimes be repeated twice, which is redundant
2
  • You won't get hyperlinks without hyperref anyway, if that's what you mean. Are you talking about features of your editor? That has nothing to do with TeX itself. You can always use a different editor.
    – cfr
    Jul 8, 2017 at 0:15
  • Yes, my editor is not able to find usages if I customized the \labelcommand Jul 8, 2017 at 0:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .