PGFplots:为分组图表分配子标题

PGFplots:为分组图表分配子标题

这是关于这个答案,其中我希望使用常规文章类获取相同的输出并编辑打印的交叉引用文本,例如,我希望 的输出为\Cref{first}其中Figure 1(a)Figure 1输出为\Cref{main}

但是这个 MWE 显示了一个错误

未定义控制序列。...itle={\subcaption{$y=x^2$}\label{second}}]

我不明白它是从哪里来的。

\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}

编辑

在我的文档中添加一些代码时,出现了不同的错误

除领导者外,您不能在这里使用“\hrule”。...itle={\subcaption{$y=x^2$\label{second}}}]

对于这个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}

答案1

关于第二个例子。你出于某种原因注释了text width中的设置title style。为了\subcaption在标题中使用text width,我认为你必须设置 ,所以只需取消注释该行即可。


对于第一个:

units库默认不将单位传递给\sisiunitx,因此您不能使用类似

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

直接。为了能够使用它,您必须添加

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

axis/选项groupplot,如pgfplots手册中所述。

有关交叉引用,请参阅使用 cleveref 的子字幕格式。 你需要

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

完整代码:

在此处输入图片描述

\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}

相关内容