# Can I add a table through PGF \nextgroupplot functionality?

I have made a minimal working example which shows how far I am right now:

\RequirePackage{luatex85}
\documentclass[tikz]{standalone}
% Default preamble
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{polar}
\usepgfplotslibrary{smithchart}
\usepgfplotslibrary{statistics}
\usepgfplotslibrary{dateplot}
% Custom preamble from global variable:
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size={2 by 2}}, height={4cm}, width={4cm}]
\nextgroupplot[title={1}]
coordinates {
(0,0)
(1,1)
(2,2)
}
;

\nextgroupplot[title={2}]
%                        \pgfplotstabletypeset[
%                       col sep=&,  % specify the column separation character
%                       row sep=\\, % specify the row separation character
%                       columns/E/.style={string type} % specify the type of data in the designated column
%                        ]{
%                           A & B & C & D & E \\
%                           1 & 10 & 0.1 & 1000 & 2011-01-01 \\
%                           2 & 20 & 0.2 & 2000 & 2012-02-02 \\
%                           3 & 30 & 0.3 & 3000 & 2013-03-03 \\
%                           4 & 40 & 0.4 & 4000 & 2014-04-04 \\
%                           5 & 50 & 0.5 & 5000 & 2015-05-05 \\
%                        }

\nextgroupplot[title={3}]
coordinates {
(0,2)
(1,1)
(2,1)
}
;
\nextgroupplot[title={4}]
coordinates {
(0,2)
(1,1)
(1,0)
}
;
\end{groupplot}
\end{tikzpicture}
\end{document}


Running this code produces the following figure:

Instead of the empty figure to the top right, ("2"), I want to insert a table. This table has already been defined in my code, but commented (with %) - if I uncomment and run my LaTeX code again the result becomes:

Would anyone know how to stay in this workflow with \nextgroupplot, and be able to resolve this problem, such that I can insert a table instead of a figure?

Kind regards

• Does the table datas correspond to the plots? – cis Sep 21 '20 at 15:58

You could remove the empty axis with

\nextgroupplot[hide axis]


and then add the table in a node after \end{groupplot}. The axes are named group cNrM, where N and M are column and row numbers respectively, so you can use for example

\node [below right, xshift=-5mm] at (group c2r1.north west) { <table code> };


Whether you're using \pgfplotstabletypeset or a tabular directly doesn't matter, but you can't have a table environment in the node.

\documentclass[tikz]{standalone}
% Default preamble
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{polar}
\usepgfplotslibrary{smithchart}
\usepgfplotslibrary{statistics}
\usepgfplotslibrary{dateplot}
% Custom preamble from global variable:
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size={2 by 2}}, height={4cm}, width={4cm}]
\nextgroupplot[title={1}]
coordinates {
(0,0)
(1,1)
(2,2)
}
;

\nextgroupplot[hide axis]

\nextgroupplot[title={3}]
coordinates {
(0,2)
(1,1)
(2,1)
}
;
\nextgroupplot[title={4}]
coordinates {
(0,2)
(1,1)
(1,0)
}
;
\end{groupplot}

\node [below right, xshift=-5mm] at (group c2r1.north west) {
\pgfplotstabletypeset[
col sep=&,  % specify the column separation character
row sep=\\, % specify the row separation character
columns/E/.style={string type} % specify the type of data in the designated column
]{
A & B & C & D & E \\
1 & 10 & 0.1 & 1000 & 2011-01-01 \\
2 & 20 & 0.2 & 2000 & 2012-02-02 \\
3 & 30 & 0.3 & 3000 & 2013-03-03 \\
4 & 40 & 0.4 & 4000 & 2014-04-04 \\
5 & 50 & 0.5 & 5000 & 2015-05-05 \\
}};

\end{tikzpicture}
\end{document}

• Thanks! That is what I wanted, in a much more robust way in what I figured out at the end. Could you show me how I would go about adding a table like this using the "normal" table enviroment in LaTex? For an example one generated by tablesgenerator.com/latex_tables# When I try I get an error about a missing item Kind regards – CuriosLatex Sep 19 '20 at 20:48
• @CuriosLatex You can't add a table environment in that context, but just remove/comment \begin{table} and \end{table}. Do you want a caption? – Torbjørn T. Sep 19 '20 at 20:54
• Oh you are right, my bad. I got it to work by removing the lines you mentioned and only keeping the tabular. Afterwards I discovered I had forgot to escape a \$ correctly. I have updated my question with the final result for my MWE, thanks once again! – CuriosLatex Sep 19 '20 at 20:59
• @CuriosLatex You actually tried editing my answer, but anyways, I'll just add a note saying that if you have a tabular instead of a \pgfplotstabletypeset that works the same. – Torbjørn T. Sep 19 '20 at 21:04
• Sorry I suppose I am bit tired. I spent a good few hours trying to figure this out, so I am pretty happy that you took time to write me a detailed answer Once again thanks! – CuriosLatex Sep 19 '20 at 21:05

Sometimes a table next to a group of plots has something to do with these plots.

In this version the plots are as big as the table.

To place the plots and the table by a loop (where the table was setted to the number 0)

\pgfplotsforeachungrouped \n in {1,0,2,3}{%%
\ifnum\n=0 \nextgroupplot[title=The Table,hide axis]
\else
\edef\tmp{
\noexpand\nextgroupplot[title=Title\n, ylabel=y\n, xlabel=x]
\noexpand\addplot+[black, mark options={color\n}, thick] table[x=X, y=Y\n]{\noexpand\data};
}\tmp
\fi}%%


has the advantage, that you can control where the table (plots) shall appear: You can choose {1,0,2,3} or {1,2,3,0} or...

With \def\ShowHelps{0}% 1 "yes" 0 "no"

\documentclass[a4paper]{article}
\usepackage[margin=2cm, showframe=false]{geometry}
\usepackage{calc}
\usepackage{colortbl}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\def\ShowHelps{1}% 1 "yes"   0 "no"

X       Y1     Y2    Y3       Date
1      10     0.1   100      2011-01-01
2      20     0.2   200      2012-02-02
3      30     0.3   300      2013-03-03
4      40     0.4   400      2014-04-04
5      50     0.5   500      2015-05-05
}\data

\foreach \n/\mycolor in {1/red, 2/blue!66, 3/purple}{
\xglobal\colorlet{color\n}{\mycolor}          }

\pgfplotsinvokeforeach{1,...,3}{
\pgfplotstableset{every col no #1/.style={
}

\newsavebox{\mytable}
\sbox{\mytable}{%
\pgfplotstabletypeset[
columns/Date/.style={string type}
]{\data}}
\pgfmathsetlengthmacro\tablewidth{width("\usebox{\mytable}")}
%\pgfmathsetlengthmacro\tableheight{height("\usebox{\mytable}")}% not good
\newlength{\tabheight}
\settototalheight{\tabheight}{\usebox{\mytable}} % good
\pgfmathsetlengthmacro\tableheight{\the\tabheight}

\ifnum\ShowHelps=1
\tikzset{Help/.style={draw=#1,
nodes={scale=0.6, fill=yellow, draw=#1, text=black, font=\tiny, midway, above, align=left,} },
Help/.default=red,
}\else
\tikzset{Help/.style={opacity=0, overlay},
}\fi

\begin{document}
\begin{tikzpicture}[]
\begin{groupplot}[group style={
group size={2 by 2},
horizontal sep=44pt, vertical sep=45pt,
},
scale only axis,    % 1/2
width=\tablewidth, height=\tableheight, % 2/2
]
\pgfplotsforeachungrouped \n in {1,0,2,3}{%%
\ifnum\n=0 \nextgroupplot[title=The Table,hide axis]
\else
\edef\tmp{
\noexpand\nextgroupplot[title=Title\n, ylabel=y\n, xlabel=x]
\noexpand\addplot+[black, mark options={color\n}, thick] table[x=X, y=Y\n]{\noexpand\data};
}\tmp
\fi}%%

% Help 1/2
\xdef\verticalsep{\pgfkeysvalueof{/pgfplots/group/vertical sep}}
\xdef\horizontalsep{\pgfkeysvalueof{/pgfplots/group/horizontal sep}}
\end{groupplot}
% Help 2/2
\node[draw=gray, anchor=north west, inner sep=0pt] (X) at (group c2r1.north west) {\usebox{\mytable}};
\draw[Help, -|] (X.north west) -- +(\tablewidth,0) node[Help, near start]{\tablewidth \\ tablewidth};
\draw[Help, -|] (X.north west) -- +(0,-\tableheight) node[Help, rotate=90]{\tableheight \\ tableheight};
\draw[Help,-|] (group c2r1.south west) -- +(0,-\verticalsep) node[Help, rotate=90]{\verticalsep \\ vertical sep};
\draw[Help,-|] (group c2r1.south west) -- +(-\horizontalsep,0)  node[Help]{\horizontalsep \\ horizontal sep};
\draw[Help,dashed] (group c2r1.north west) -- +(-\tablewidth-\horizontalsep,0);
\draw[Help,dashed] (group c2r1.south west) -- +(-\tablewidth-\horizontalsep,0);
\draw[Help,dashed] (group c2r2.south west) -- +(0,\tableheight) ;
\draw[Help,dashed] (group c2r2.south east) -- +(0,2*\tableheight+\verticalsep) ;
\end{tikzpicture}
\end{document}