2

This is a follow-up question about this answer, where I would like to get the same output using the conventional article class and edit the printed cross-reference text such that, for example, I would like the output of \Cref{first} to be Figure 1(a) where Figure 1 is the output of \Cref{main}.

However, this MWE shows me an error

Undefined control sequence. ...itle={\subcaption{$y=x^2$}\label{second}}]

which I don't understand where it comes from.

\documentclass[a4paper]{article}
\usepackage{mathtools,booktabs}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots,subcaption,siunitx}


\captionsetup[subfigure]{labelformat=parens}
\usepgfplotslibrary{groupplots,units}
\pgfplotsset{compat=newest}
\usepackage{hyperref,cleveref}

\newlength\figureheight 
\newlength\figurewidth 
\setlength\figureheight{0.4\textwidth}
\setlength\figurewidth{0.35\textwidth}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
  width=\figurewidth,
  height=\figureheight,
  scale only axis,
  title style   = {
        at={(current axis.north)},
        yshift=-2.5mm,
        font=\normalfont,
        color=black,
    text width=\figurewidth
},
  group style={
    group size=2 by 2,
    vertical sep=3cm,
    horizontal sep=1.5cm
},
  ylabel=$v_n$,xlabel=$y$,
  y unit=\m\per\s,x unit=\cm,
  domain=4:5,samples=10,
]
\nextgroupplot[title={\subcaption{Subcaption 1}\label{first}}]
  \addplot {x};
  %
  \nextgroupplot[title={\subcaption{$y=x^2$}\label{second}}]
  \addplot {x^2};
  %
  \nextgroupplot[title={\subcaption{Subcaption 3. A longer one that will break across lines.}\label{third}}]
  \addplot {-x};
  %
  \nextgroupplot[title={\subcaption{$y=x^2+4$}\label{fourth}}]
  \addplot {x^2 + 4};
\end{groupplot}

\end{tikzpicture}
\caption{Main caption}\label{main}
\end{figure}

\Cref{main} is the parent figure, and \Cref{first} is its child.

\end{document}

Edit

When adding some code to my document, a different error shows up

You can't use `\hrule' here except with leaders. ...itle={\subcaption{$y=x^2$\label{second}}}]

for this MWE

\documentclass[a4paper]{article}
\usepackage{mathtools,booktabs}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots,subcaption,siunitx}


\captionsetup[subfigure]{labelformat=parens}
\usepgfplotslibrary{groupplots,units}
\pgfplotsset{compat=newest}
\usepackage{hyperref,cleveref}

\begin{document}
\thispagestyle{empty}
\centering

\newlength{\BorderLineWeight}       \setlength{\BorderLineWeight}{1pt}
\newlength{\MajorGridLineWeight}    \setlength{\MajorGridLineWeight}{0.1pt}
\newlength{\MinorGridLineWeight}    \setlength{\MinorGridLineWeight}{0.2pt}
\newlength{\MajorTickLineWeight}    \setlength{\MajorTickLineWeight}{1pt}
\newlength{\MinorTickLineWeight}    \setlength{\MinorTickLineWeight}{0.5pt}

\newlength{\HorSep} \setlength{\HorSep}{22pt}
\newlength{\VerSep} \setlength{\VerSep}{40pt}

\newlength{\PanelWidth} \setlength{\PanelWidth}{0.35\textwidth}
\newlength{\PanelHeight} \setlength{\PanelHeight}{0.28\textheight}

\begin{figure}
    \centering

    \begin{tikzpicture}
    \pgfkeys{/pgf/number format/.cd,1000 sep={ }}% \:\! == 4mu - 3mu = 1mu
    \begin{groupplot}[
    group style={
        group name=G,
        group size=2 by 2,
        y descriptions at=edge left,% make only single y axis label at left
        x descriptions at = edge bottom,
        horizontal sep=\HorSep, % adjust as needed
        vertical sep=\VerSep, 
    },
    enlargelimits=false,
    width= \PanelWidth ,%-width("300"),
    height= \PanelHeight,
    scale only axis, axis on top , grid=both,
    axis line style = {line width = \BorderLineWeight, draw=black},
    %
    tick label style = {font=\bfseries,color=black},
    tick align = outside,
    %
    xlabel  =   {r \si{\um}},
    ylabel  =   {z \si{\um}},
    %
    x label style = {at={(current axis.south)}, below=6.5mm, font=\normalfont, color=black},
    y label style = {at={(current axis.west)},above=6.5mm, font=\normalfont, color=black},
    %
    title style = {
        at={(current axis.north)},
        yshift=-2.5mm,
        font=\normalfont,
        color=black,
        %text width=\figurewidth,
    },
    %
    xmin=0, xmax=50, xtick={0,10,...,50},       minor x tick num={1}, 
    ymin=0, ymax=150, ytick={0,50,...,150},     minor y tick num={4},
    %
    major x grid style = {line width    =   \MajorGridLineWeight,dashed,black},
    minor x grid style = {line width    =   \MinorGridLineWeight,dotted,black},
    major y grid style = {line width    =   \MajorGridLineWeight,dashed,black},
    minor y grid style = {line width    =   \MinorGridLineWeight,dotted,black},
    %
    major x tick style = {line width    =   \MajorTickLineWeight,solid,black},
    minor x tick style = {line width    =   \MinorTickLineWeight,solid,black},
    major y tick style = {line width    =   \MajorTickLineWeight,solid,black},
    minor y tick style = {line width    =   \MinorTickLineWeight,solid,black},
    %
    xtick pos=left,
    ytick pos=left,
    %
    major tick length   =   1.5mm, % distance outside plot area
    minor tick length   =   1mm,
    ]
\nextgroupplot[title={\subcaption{Subcaption 1}\label{first}}]
  \addplot {x};
  %
  \nextgroupplot[title={\subcaption{$y=x^2$\label{second}}}]
  \addplot {x^2};
  %
  \nextgroupplot[title={\subcaption{Subcaption 3. A longer one that will break across lines.\label{third}}}]
  \addplot {-x};
  %
  \nextgroupplot[title={\subcaption{$y=x^2+4$\label{fourth}}}]
  \addplot {x^2 + 4};
\end{groupplot}

\end{tikzpicture}
\caption{Main caption}\label{main}
\end{figure}

\Cref{main} is the parent figure, and \Cref{first} is its child.

\end{document}
2

Regarding the second example. You for some reason commented the text width setting in the title style. In order to use \subcaption in a title you must set the text width I think, so just unncomment that line.


For the first one:

The units library doesn't by default pass the units to the \si macro of siunitx, so you cannot use things like

y unit=\m\per\s,x unit=\cm,

directly. To be able to use that, you must add

unit code/.code 2 args={\si{#1#2}}

to the axis/groupplot options, as given in the pgfplots manual.

Regarding the cross reference, see Subcaption format with cleveref. You need

\captionsetup[subfigure]{subrefformat=simple,labelformat=simple}
\renewcommand\thesubfigure{(\alph{subfigure})}

Complete code:

enter image description here

\documentclass[a4paper]{article}
\usepackage{mathtools,booktabs}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots,subcaption,siunitx}


\captionsetup[subfigure]{subrefformat=simple,labelformat=simple}
\renewcommand\thesubfigure{(\alph{subfigure})}

\usepgfplotslibrary{groupplots,units}
\pgfplotsset{compat=newest}
\usepackage{hyperref,cleveref}

\newlength\figureheight 
\newlength\figurewidth 
\setlength\figureheight{0.4\textwidth}
\setlength\figurewidth{0.35\textwidth}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
  width=\figurewidth,
  height=\figureheight,
  scale only axis,
  title style   = {
        at={(current axis.north)},
        yshift=-2.5mm,
        font=\normalfont,
        color=black,
        text width=\figurewidth
},
  group style={
    group size=2 by 2,
    vertical sep=3cm,
    horizontal sep=1.5cm
},
  ylabel=$v_n$,xlabel=$y$,
  y unit=\m\per\s,x unit=\cm,
  domain=4:5,samples=10,
  unit code/.code 2 args={\si{#1#2}}
]
\nextgroupplot[title={\subcaption{Subcaption 1}\label{first}}]
  \addplot {x};
%  %
  \nextgroupplot[title={\subcaption{$y=x^2$}\label{second}}]
  \addplot {x^2};
  %
  \nextgroupplot[title={\subcaption{Subcaption 3. A longer one that will break across lines.}\label{third}}]
  \addplot {-x};
  %
  \nextgroupplot[title={\subcaption{$y=x^2+4$}\label{fourth}}]
  \addplot {x^2 + 4};
\end{groupplot}

\end{tikzpicture}
\caption{Main caption}\label{main}
\end{figure}

\Cref{main} is the parent figure, and \Cref{first} is its child.



\end{document}
  • Many thanks for your help. For my second code in the question, a different error (You can't use \hrule here except with leaders. ...itle={\subcaption{$y=x^2$\label{second}}}]) only happens when setting the title to be title={\subcaption{Subcaption 1}\label{first}} or \captionof{subfigure}{heading\label{first}}. I couldn't move on with my real document because of this error. – Diaa Jul 9 '18 at 19:40
  • 2
    @Diaa Try uncommenting the text width setting in the title style. I think you must set the text width to use a subcaption in a title. – Torbjørn T. Jul 9 '18 at 19:45
  • You literally saved my day by this tweak. – Diaa Jul 9 '18 at 20:02

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.