pgfplots+subcaption 中有错误吗?

pgfplots+subcaption 中有错误吗?

我正在使用pgfplots(与优秀的matlab2tikz),pgfplotstablesubcaptionbox一起,并且由于某些无法解释的原因(至少对我来说),该命令\pgfplotsforeachungrouped似乎破坏了的标签\subcaptionbox- 即标签从变成(a)(2subfigure)

我试图让 MWE 保持简短,但我想根据答案包含一个用例片段,说明为什么我想要这个特定的命令组合(我想说,这并不太离谱)https://tex.stackexchange.com/a/34045/42225

注释掉\pgfplotsforeachungrouped-command 会删除我的表格的(大部分)格式,但随后 -labels\subcaptionbox会再次起作用(注释掉命令的内容没有效果)。

\documentclass{scrbook}

\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[hypcap=true,justification=centering]{subcaption} % replacement for subfigure (obsolete) and subfig (out-of-date)

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{tikzscale}

\newlength{\plotsize}

\newcommand{\findmax}[3]{% tex.stackexchange.com/a/34045/42225
    \pgfplotstablevertcat{\datatable}{#1}
    \pgfplotstablecreatecol[
    create col/expr={%
    \pgfplotstablerow
    }]{rownumber}\datatable
    \pgfplotstablesort[sort key={#2},sort cmp={float >}]{\sorted}{\datatable}%
    \pgfplotstablegetelem{0}{rownumber}\of{\sorted}%
    \pgfmathtruncatemacro#3{\pgfplotsretval}
    \pgfplotstableclear{\datatable}
}

\pgfplotstableset{
    highlight row max/.code 2 args={% tex.stackexchange.com/a/34045/42225
        \pgfmathtruncatemacro\rowindex{#2-1}
        \pgfplotstabletranspose{\transposed}{#1}
        \findmax{\transposed}{\rowindex}{\maxval}
        \edef\setstyles{\noexpand\pgfplotstableset{
                every row \rowindex\space column \maxval\noexpand/.style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={$\noexpand\bf}{$}
                    },
                }
            }
        }\setstyles
    }
}

\pgfplotstableset{%
    every head row/.style={font=\sffamily,before row=\toprule,after row=\midrule},
    every last row/.style={after row=\bottomrule}
}

%%% Commenting this out puts the subfigure labels back in order
\pgfplotsforeachungrouped \alph in {0.9,1,1.1,1.2,1.5,1.75,2}{% tex.stackexchange.com/a/207996/42225
    \pgfplotstableset{
        columns/a\alph/.estyle={
            multiply by=100,fixed,precision=1,zerofill,column name={$\alpha=\alph$},
            postproc cell content/.append style={/pgfplots/table/@cell content/.add={}{$\%$}}
        }
    }
}

\pgfplotstableread{% dummy row so that row/col number from "highlight row max" are correct
dummy   a0.9    a1      a1.1    a1.2    a1.5    a1.75   a2
0       0.8628  0.8544  0.8567  0.8305  0.7798  0.6812  0.6204
0       0.8300  0.8233  0.8407  0.8376  0.7853  0.7032  0.6371
0       0.8215  0.8442  0.8391  0.8475  0.7886  0.7166  0.6405
0       0.8224  0.8631  0.8470  0.8514  0.7924  0.7364  0.6503
0       0.8317  0.8434  0.8394  0.8678  0.7952  0.7408  0.6457
0       0.8332  0.8540  0.8401  0.8284  0.7983  0.7270  0.6584
}\alphadata

\begin{filecontents}{empty.tikz}
\begin{tikzpicture}[trim axis left]
\begin{axis}[%
width=\plotsize,
height=\plotsize,
scale only axis,
xmin=0,
xmax=1,
ymin=0,
ymax=1
]
\end{axis}
\end{tikzpicture}%
\end{filecontents}


\begin{document}

\setcounter{chapter}{1}

\begin{table}
\centering
\pgfplotstabletypeset[
    highlight row max={\alphadata}{1},
    highlight row max={\alphadata}{2},
    highlight row max={\alphadata}{3},
    highlight row max={\alphadata}{4},
    highlight row max={\alphadata}{5},
    highlight row max={\alphadata}{6},
    create on use/j/.style={create col/expr={\pgfplotstablerow}},% need to create column separately, as it would be the row max otherwise
    columns/j/.style={int detect,column name=$j$},
    columns={j,a0.9,a1,a1.1,a1.2,a1.5,a1.75,a2}
]\alphadata
\caption{Caption}
\end{table}

\setlength{\plotsize}{0.45\linewidth}
\begin{figure}
{%\hfill%
\subcaptionbox{One}{%
    \includegraphics[width=\plotsize]{empty.tikz}
    }%
\hfill%
\subcaptionbox{Two}{%
    \includegraphics[width=\plotsize]{empty.tikz}
    }%
%\hfill%
}
\caption{Caption}
\end{figure}

\end{document}

答案1

\alph是一个将计数器的值打印为字母的宏,这会导致冲突,因为它是由您重新定义的。通常,子标题编号为ab等,并使用 打印\alph{subfigure}。当\alph重新定义为时2,循环中的最后一个值\alph{subfigure}变为2subfigure

将循环宏称为其他名称,这样\MyVar您的代码就可以正常工作。

在此处输入图片描述

\documentclass{scrbook}

\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[hypcap=true,justification=centering]{subcaption} % replacement for subfigure (obsolete) and subfig (out-of-date)

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{tikzscale}

\newlength{\plotsize}

\newcommand{\findmax}[3]{% tex.stackexchange.com/a/34045/42225
    \pgfplotstablevertcat{\datatable}{#1}
    \pgfplotstablecreatecol[
    create col/expr={%
    \pgfplotstablerow
    }]{rownumber}\datatable
    \pgfplotstablesort[sort key={#2},sort cmp={float >}]{\sorted}{\datatable}%
    \pgfplotstablegetelem{0}{rownumber}\of{\sorted}%
    \pgfmathtruncatemacro#3{\pgfplotsretval}
    \pgfplotstableclear{\datatable}
}

\pgfplotstableset{
    highlight row max/.code 2 args={% tex.stackexchange.com/a/34045/42225
        \pgfmathtruncatemacro\rowindex{#2-1}
        \pgfplotstabletranspose{\transposed}{#1}
        \findmax{\transposed}{\rowindex}{\maxval}
        \edef\setstyles{\noexpand\pgfplotstableset{
                every row \rowindex\space column \maxval\noexpand/.style={
                    postproc cell content/.append style={
                        /pgfplots/table/@cell content/.add={$\noexpand\bf}{$}
                    },
                }
            }
        }\setstyles
    }
}

\pgfplotstableset{%
    every head row/.style={font=\sffamily,before row=\toprule,after row=\midrule},
    every last row/.style={after row=\bottomrule}
}

%%% Commenting this out puts the subfigure labels back in order
\pgfplotsforeachungrouped \MyVar in {0.9,1,1.1,1.2,1.5,1.75,2}{% tex.stackexchange.com/a/207996/42225
    \pgfplotstableset{
        columns/a\MyVar/.estyle={
            multiply by=100,fixed,precision=1,zerofill,column name={$\alpha=\MyVar$},
            postproc cell content/.append style={/pgfplots/table/@cell content/.add={}{$\%$}}
        }
    }
}

\pgfplotstableread{% dummy row so that row/col number from "highlight row max" are correct
dummy   a0.9    a1      a1.1    a1.2    a1.5    a1.75   a2
0       0.8628  0.8544  0.8567  0.8305  0.7798  0.6812  0.6204
0       0.8300  0.8233  0.8407  0.8376  0.7853  0.7032  0.6371
0       0.8215  0.8442  0.8391  0.8475  0.7886  0.7166  0.6405
0       0.8224  0.8631  0.8470  0.8514  0.7924  0.7364  0.6503
0       0.8317  0.8434  0.8394  0.8678  0.7952  0.7408  0.6457
0       0.8332  0.8540  0.8401  0.8284  0.7983  0.7270  0.6584
}\alphadata

\begin{filecontents}{empty.tikz}
\begin{tikzpicture}[trim axis left]
\begin{axis}[%
width=\plotsize,
height=\plotsize,
scale only axis,
xmin=0,
xmax=1,
ymin=0,
ymax=1
]
\end{axis}
\end{tikzpicture}%
\end{filecontents}


\begin{document}

\setcounter{chapter}{1}


\begin{table}
\centering
\pgfplotstabletypeset[
    highlight row max={\alphadata}{1},
    highlight row max={\alphadata}{2},
    highlight row max={\alphadata}{3},
    highlight row max={\alphadata}{4},
    highlight row max={\alphadata}{5},
    highlight row max={\alphadata}{6},
    create on use/j/.style={create col/expr={\pgfplotstablerow}},% need to create column separately, as it would be the row max otherwise
    columns/j/.style={int detect,column name=$j$},
    columns={j,a0.9,a1,a1.1,a1.2,a1.5,a1.75,a2}
]\alphadata
\caption{Caption}
\end{table}

\setlength{\plotsize}{0.45\linewidth}
\begin{figure}
{%\hfill%
\subcaptionbox{One}{%
    \includegraphics[width=\plotsize]{empty.tikz}
    }%
\hfill%
\subcaptionbox{Two}{%
    \includegraphics[width=\plotsize]{empty.tikz}
    }%
%\hfill%
}
\caption{Caption}
\end{figure}

\end{document}

相关内容