I would like to dynamically create multiple tables & TikZ pictures out of a single pgfPot database.
This MWE shows how to generate a single table and TikZ picture out of the given data.csv. The code is basically derived from this post
\documentclass{article}
\usepackage[a4paper,margin=10mm]{geometry}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{mwe}
%PowerOutlet
\tikzset{
pics/powerOutlet/.style={
code={
\begin{scope}[scale=0.4]
\drawred,very thick--(0,10);
\drawred,very thick--(10,10);
\drawred,very thick,line cap=round arc[start angle=180,end angle=360,radius=10];
\end{scope}
}
}
}
%Example data for MWE
\begin{filecontents}{data.csv}
Image,ID,Floor,X-Pos,Y-Pos
powerOutlet,A1F5F,GroundFloor,1,2
powerOutlet,7ZF3I,UpperFloor,20,1
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1}
%Define Components for TikZ Picture
\tikzset{
pics/ElectricComponent/.style={
code={
\tikzset{ElectricComponent/.cd, #1}
\pic{powerOutlet};
}
},
ElectricComponent/floor/.initial={0}%
}
\begin{document}
\section*{Table}
\begin{center}
\pgfplotstabletypeset[
font=\ttfamily,
col sep=comma,
columns={Image,ID,Floor,X-Pos,Y-Pos},
columns/Image/.style={
column name=Image,
assign cell content/.code={
\pgfkeyssetvalue{/pgfplots/table/@cell content}{
\fbox{\raisebox{-0.5\height}{\tikz{\pic[scale=0.04]{##1};}}}
}
}
},
columns/ID/.style={
column name=ID,
string type
},
columns/Floor/.style={
column name=Floor,
string type
},
columns/X-Pos/.style={
column name=X-Pos
},
columns/Y-Pos/.style={
column name=Y-Pos,
column type/.add={}{|}
},
column type/.add={|}{},
after row={\hline},
every head row/.style={before row=\hline}
]{\csvdata}
\end{center}
\section*{Graphic}
\begin{center}%
\scalebox{0.1}{%
\begin{tikzpicture}
%\node[inner sep=0pt] (ground_floor){\includegraphics{../Images/Layout/GroundFloor.png}};
\node[inner sep=0pt] (ground_floor){\includegraphics[scale=10]{example-image}};
\begin{scope}
\foreach \row in {0,...,\CSVDataRows}{
\pgfplotstablegetelem{\row}{ID}\of{\csvdata}
\pgfmathsetmacro{\i}{"\pgfplotsretval"}
\pgfplotstablegetelem{\row}{Floor}\of{\csvdata}
\pgfmathsetmacro{\f}{"\pgfplotsretval"}
\pgfplotstablegetelem{\row}{X-Pos}\of{\csvdata}
\pgfmathsetmacro{\x}{\pgfplotsretval}
\pgfplotstablegetelem{\row}{Y-Pos}\of{\csvdata}
\pgfmathsetmacro{\y}{\pgfplotsretval}
\pic[transform shape] at (\x,\y){
ElectricComponent={
floor={\f}
}};}
\end{scope}
\end{tikzpicture}
}
\end{center}
\end{document}
Now I would like to dynamically create more than one Table and TikZ picture based on the Floor information from my data.csv. In this case two Table's and two TikZ picture's should be created (GroundFloor / UpperFloor).
So all data.csv information which are set to GroundFloor should only be shown in the GroundFloor related table and the GroundFloor related TikZ picture. And the same for the UpperFloor.
To determine if a Floor identifier is valid, I would like to use something like \IfFileExists{../Images/Layout/__FLOOR__.png}{Valid}{\errmessage{Floor not supported!}} This step is mandatory, because I always need to include the dedicated floor.png to every TikZ picture. This can be seen in the commented out line of the TikZ picture. %\node[inner sep=0pt] (ground_floor){\includegraphics{../Images/Layout/GroundFloor.png}};

FloorisGroundFloor, putrow predicate/.code={ \edef\tempfloorname{GroundFloor} \pgfplotstablegetelem{#1}{Floor}\of\csvdata \ifx\pgfplotsretval\tempfloorname\else \pgfplotstableuserowfalse \fi },as first option to\pgfplotstabletypeset. If you want to reuse this multiple times, it might be good idea to create a macro (take care of the right number of#s= – Jasper Habicht Mar 01 '24 at 13:44tikzpictureto only print the\pics from the correctFloor. – Jasper Habicht Mar 01 '24 at 13:46\PackageError{<package>}{<message>}{<some info>}, like in\IfFileExists{../Images/Layout/__FLOOR__.png}{}{\PackageError{}{Floor not supported!}{}}. – Jasper Habicht Mar 01 '24 at 13:49