3

I'm using pgfplots (with the excellent matlab2tikz), pgfplotstable and subcaptionbox together, and for some inexplicable reason (at least to me), the command \pgfplotsforeachungrouped seems to break the labels of \subcaptionbox - namely, the labels get turned from (a) into (2subfigure).

I've tried to keep the MWE short, but I wanted to include a snippet of the use case why I want this particular combination of commands (which isn't too far out of the ordinary, I'd say), based on the answer https://tex.stackexchange.com/a/34045/42225.

Commenting out the \pgfplotsforeachungrouped-command removes (most of) the formatting of my table, but then the \subcaptionbox-labels work again (commenting out the content of the command has no effect).

\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}
Axel
  • 791
  • Did you try \pgfplotsinvokeforeach{...} and replace \alph with #1 – percusse Nov 13 '15 at 01:44
  • @percusse: I briefly tried that when I looked up the documentation, but I must have made a mistake, because now it works. Do you think the bug might be interesting enough to NOT delete the question...? – Axel Nov 13 '15 at 01:49
  • I don't even know what the bug is :) looks like the usual late expansion problem – percusse Nov 13 '15 at 01:55
  • The fact that there is no bug (if it is a fact) doesn't mean the question should be deleted. You can always edit it, if you feel uncomfortable, to simply ask how to do... given that you've tried... and that doesn't work. – cfr Nov 13 '15 at 02:59
  • @cfr: Thanks for the comment. I've decided to keep the question because, when both things work separately (and should be unrelated anyway), but don't work anymore together, it can cost someone - in this case, me - a lot of time to find the error. Even if someone doesn't consider this behaviour a bug, it's conceivable that it might help somebody down the line. – Axel Nov 13 '15 at 05:30
  • @Axel Good ;). That was precisely my thought. – cfr Nov 13 '15 at 13:39

1 Answers1

4

\alph is a macro that prints the value of a counter as letters, and that causes a conflict, because it is redefined by you. Normally, the subcaption numbers are a, b, etc. and they are printed with \alph{subfigure}. When \alph is redefined to 2, the last value in the loop, \alph{subfigure} becomes 2subfigure.

Call the loop macro something else, like \MyVar and your code works fine.

enter image description here

\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}
Torbjørn T.
  • 206,688
  • How could I have forgotten the counter style \alph...? I guess I was too tired to think of that. Thanks! – Axel Nov 13 '15 at 07:35