从单个 pgfPlot 数据库中创建多个表格和 tikZ 图片

从单个 pgfPlot 数据库中创建多个表格和 tikZ 图片

我想从单个 pgfPot 数据库中动态创建多个表和 TikZ 图片。

这个 MWE 展示了如何根据给定的生成单个table和。代码基本上来自TikZ picturedata.csv这个帖子

\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]
     \draw[red,very thick](0,0)--(0,10);
     \draw[red,very thick](-10,10)--(10,10);
     \draw[red,very thick,line cap=round](-10,20) 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}

现在我想根据我的楼层信息动态创建多个TableTikZ picturedata.csv这种情况下,应该创建Table两个和两个( / )。TikZ pictureGroundFloorUpperFloor

因此,所有data.csv设置为的信息GroundFloor应仅显示在GroundFloor相关表和GroundFloor相关中TikZ picture。对于 也是如此UpperFloor

要确定 Floor 标识符是否有效,我想使用类似\IfFileExists{../Images/Layout/__FLOOR__.png}{Valid}{\errmessage{Floor not supported!}}这样的步骤,因为我总是需要将专用的 floor.png 包含到每个 中TikZ picture。这可以在 的注释行中看到TikZ picture%\node[inner sep=0pt] (ground_floor){\includegraphics{../Images/Layout/GroundFloor.png}};

答案1

您可以从列中获取当前值,并使用基本 来Floor检查它是否等于GroundFloor(或等于或其他) 。您可以在排版表格时执行此测试,并通过排除此测试产生的行。UpperFloor\ifxFALSErow predicate

\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]
                \draw[red,very thick](0,0)--(0,10);
                \draw[red,very thick](-10,10)--(10,10);
                \draw[red,very thick,line cap=round](-10,20) 
                    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} 

\begin{document}

\edef\tempfloorname{GroundFloor}

\section*{Table}
\begin{center}
\pgfplotstabletypeset[
    row predicate/.code={
        \pgfplotstablegetelem{#1}{Floor}\of\csvdata
        \ifx\pgfplotsretval\tempfloorname\else
            \pgfplotstableuserowfalse
        \fi
    },
    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] (floor) {
        \IfFileExists{\tempfloorname .png}{
            \includegraphics[scale=5]{\tempfloorname}
        }{\PackageError{}{Floor not supported!}{}}
    };
    \begin{scope}
    \foreach \row in {0,...,\CSVDataRows}{
        \pgfplotstablegetelem{\row}{Floor}\of{\csvdata}
        \ifx\pgfplotsretval\tempfloorname\relax
            \pgfplotstablegetelem{\row}{ID}\of{\csvdata}
            \pgfmathsetmacro{\i}{"\pgfplotsretval"}
            \pgfplotstablegetelem{\row}{X-Pos}\of{\csvdata}
            \pgfmathsetmacro{\x}{\pgfplotsretval}
            \pgfplotstablegetelem{\row}{Y-Pos}\of{\csvdata}
            \pgfmathsetmacro{\y}{\pgfplotsretval}
            \pgfplotstablegetelem{\row}{Image}\of{\csvdata}
            \pgfmathsetmacro{\t}{"\pgfplotsretval"}
            \pic[transform shape] at (\x,\y) {\t};
        \fi
    }
    \end{scope}
    \end{tikzpicture}
}
\end{center}
\end{document}

在此处输入图片描述


为每个特定楼层打印一页的全自动版本可能类似于以下内容:

\documentclass{article}
\usepackage[a4paper,margin=10mm]{geometry}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

%PowerOutlet
\tikzset{
    pics/powerOutlet/.style={
        code={
            \begin{scope}[scale=0.4]
                \draw[red,very thick](0,0)--(0,10);
                \draw[red,very thick](-10,10)--(10,10);
                \draw[red,very thick,line cap=round](-10,20) 
                    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,F2CB3,GroundFloor,2,3
powerOutlet,7ZF3I,UpperFloor,20,1
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1} 

\pgfplotstablesave[
    col sep=comma,
    columns={Floor},
    sort cmp=string <,
    unique={Floor}
]{\csvdata}{floor-data.csv}
\pgfplotstableread[col sep=comma]{floor-data.csv}{\csvfloordata}
\pgfplotstablegetrowsof{\csvfloordata}
\pgfmathtruncatemacro\CSVFloorDataRows{\pgfplotsretval-1} 

\pgfplotstableset{
    row predicate/.code={
        \pgfplotstablegetelem{#1}{Floor}\of\csvdata
        \ifx\pgfplotsretval\tempfloorname\else
            \pgfplotstableuserowfalse
        \fi
    },
    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}
}

\newcommand{\PrintFloor}[1]{
    \edef\tempfloorname{#1}
    
    \section*{Table}
    \begin{center}
    \pgfplotstabletypeset{\csvdata}
    \end{center}
    
    \section*{Graphic}
    \begin{center}%
    \scalebox{0.1}{%
        \begin{tikzpicture}
        \node[inner sep=0pt] (floor) {
            \IfFileExists{\tempfloorname .png}{
                \includegraphics[scale=5]{\tempfloorname}
            }{\PackageError{}{Floor not supported!}{}}
        };
        \begin{scope}
        \foreach \row in {0,...,\CSVDataRows} {
            \pgfplotstablegetelem{\row}{Floor}\of{\csvdata}
            \ifx\pgfplotsretval\tempfloorname\relax
                \pgfplotstablegetelem{\row}{ID}\of{\csvdata}
                \pgfmathsetmacro{\i}{"\pgfplotsretval"}
                \pgfplotstablegetelem{\row}{X-Pos}\of{\csvdata}
                \pgfmathsetmacro{\x}{\pgfplotsretval}
                \pgfplotstablegetelem{\row}{Y-Pos}\of{\csvdata}
                \pgfmathsetmacro{\y}{\pgfplotsretval}
                \pgfplotstablegetelem{\row}{Image}\of{\csvdata}
                \pgfmathsetmacro{\t}{"\pgfplotsretval"}
                \pic[transform shape] at (\x,\y) {\t};
            \fi
        }
        \end{scope}
        \end{tikzpicture}
    }
    \end{center}
    
    \clearpage
}

\begin{document}

\foreach \floor in {0,...,\CSVFloorDataRows} {
    \pgfplotstablegetelem{\floor}{Floor}\of{\csvfloordata}
    \PrintFloor{\pgfplotsretval}
}

\end{document}

相关内容