将 \includegraphics 更改为 \pgfplotstabletypeset 中的 TikZ \pic

将 \includegraphics 更改为 \pgfplotstabletypeset 中的 TikZ \pic

我想\pic根据“类型”列中的信息将 pgf 表“图像”列的内容从使用包含的图形更改为预定义的 TikZ。 应该\pic在 内水平和垂直居中table

这是我的工作 MWE

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

\begin{filecontents}{data.csv}
Image,       ID,    Description,  Type,        X-Pos,   Y-Pos,   rotation
powerSupply, A1F5F, PowerSupply1, PowerSupply, 3590,    2000,    270 
powerSupply, 7ZF3I, PowerSupply2, PowerSupply, 3590,    2500,    270
powerSupply, O7PYQ, PowerSupply3, PowerSupply, 4250,    6070,    180
powerSupply, A7HAZ, Switch1,      singleSwitch,5700,    6070,    180
\end{filecontents}

\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1} 

\begin{document}
\begin{center}
\pgfplotstabletypeset[%
font=\ttfamily,%
col sep=comma,%
columns={Image, ID, Description, Type, X-Pos, Y-Pos},%
columns/Image/.style={%
column name=Image,%
assign cell content/.code={%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\includegraphics[scale=0.07]{example-image}}}  %==> This should be changed to add a TikZ \pic depending on the type column!
},%
columns/ID/.style={%
column name=ID,%
string type%
},%
columns/Description/.style={%
column name=Description,%
string type%
},%
columns/Type/.style={%
column name=Type,%
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}
\end{document}

例如,我们可以使用这两个迷你 TikZ

%PowerOutlet
\tikzset{
 pics/stove/.style={
  code={
   \begin{scope}[scale=\resultImageScale]
     \draw[red,line width=\resultLineScaleMetric](0,0)--(0,1);
     \draw[red,line width=\resultLineScaleMetric](-1,1)--(1,1);
     \draw[red,line width=\resultLineScaleMetric,line cap=round](-1,2) arc[start angle=180,end angle=360,radius=1] node[midway,above,sloped,font=\bfseries] {\scalebox{\scaleRatio}{1}};
  \end{scope}
  }
 }
}

%Button
\tikzset{
 pics/stove/.style={
  code={
   \begin{scope}[scale=\resultImageScale]
     \draw[red,line width=\resultLineScaleMetric](0,0) circle[radius=1];
     \draw[red,line width=\resultLineScaleMetric](0,0) circle[radius=0.6];
   \end{scope}
  }
 }
}

答案1

您只需\includegraphics[scale=0.07]{example-image}在代码中用替换即可\tikz{ \pic{<pic name>}; }

键中的第二部分pics/stove/.style(即stove)是图片的名称。因此,如果您定义不同的图片,则应该为它们指定不同的名称。如果您定义以此方式命名的图片stove,则可以通过 来调用 \pic{stove};tikzpicture

请注意,您显示的图片的定义中有一些命令未定义,所以我需要在下面的 MWE 中定义它们:

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

\def\resultImageScale{1}
\def\resultLineScaleMetric{0.4pt}
\def\scaleRatio{1}

%PowerOutlet
\tikzset{
 pics/PowerOutlet/.style={
  code={
   \begin{scope}[scale=\resultImageScale]
     \draw[red,line width=\resultLineScaleMetric](0,0)--(0,1);
     \draw[red,line width=\resultLineScaleMetric](-1,1)--(1,1);
     \draw[red,line width=\resultLineScaleMetric,line cap=round](-1,2) arc[start angle=180,end angle=360,radius=1] node[midway,above,sloped,font=\bfseries] {\scalebox{\scaleRatio}{1}};
  \end{scope}
  }
 }
}

%Button
\tikzset{
 pics/button/.style={
  code={
   \begin{scope}[scale=\resultImageScale]
     \draw[red,line width=\resultLineScaleMetric](0,0) circle[radius=1];
     \draw[red,line width=\resultLineScaleMetric](0,0) circle[radius=0.6];
   \end{scope}
  }
 }
}

\begin{filecontents}{data.csv}
Image,       ID,    Description,  Type,        X-Pos,   Y-Pos,   rotation
PowerOutlet, A1F5F, PowerSupply1, PowerSupply, 3590,    2000,    270 
PowerOutlet, 7ZF3I, PowerSupply2, PowerSupply, 3590,    2500,    270
button,      O7PYQ, PowerSupply3, PowerSupply, 4250,    6070,    180
button,      A7HAZ, Switch1,      singleSwitch,5700,    6070,    180
\end{filecontents}

\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1} 

\begin{document}
\begin{center}
\pgfplotstabletypeset[%
font=\ttfamily,%
col sep=comma,%
columns={Image, ID, Description, Type, X-Pos, Y-Pos},%
columns/Image/.style={%
column name=Image,%
assign cell content/.code={%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\tikz{\pic{##1};}}
}
},%
columns/ID/.style={%
column name=ID,%
string type%
},%
columns/Description/.style={%
column name=Description,%
string type%
},%
columns/Type/.style={%
column name=Type,%
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}
\end{document}

在此处输入图片描述


如果Type表中的数据与相关的名称相同\pic(即ImageType列是相同的副本),则可以Image从表中删除该列并Type自动将此列创建为该列的副本:

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

\def\resultImageScale{1}
\def\resultLineScaleMetric{0.4pt}
\def\scaleRatio{1}

%PowerOutlet
\tikzset{
 pics/PowerSupply/.style={
  code={
   \begin{scope}[scale=\resultImageScale]
     \draw[red,line width=\resultLineScaleMetric](0,0)--(0,1);
     \draw[red,line width=\resultLineScaleMetric](-1,1)--(1,1);
     \draw[red,line width=\resultLineScaleMetric,line cap=round](-1,2) arc[start angle=180,end angle=360,radius=1] node[midway,above,sloped,font=\bfseries] {\scalebox{\scaleRatio}{1}};
  \end{scope}
  }
 }
}

%Button
\tikzset{
 pics/singleSwitch/.style={
  code={
   \begin{scope}[scale=\resultImageScale]
     \draw[red,line width=\resultLineScaleMetric](0,0) circle[radius=1];
     \draw[red,line width=\resultLineScaleMetric](0,0) circle[radius=0.6];
   \end{scope}
  }
 }
}

\begin{filecontents}{data.csv}
ID,    Description,  Type,        X-Pos,   Y-Pos,   rotation
A1F5F, PowerSupply1, PowerSupply, 3590,    2000,    270 
7ZF3I, PowerSupply2, PowerSupply, 3590,    2500,    270
O7PYQ, PowerSupply3, PowerSupply, 4250,    6070,    180
A7HAZ, Switch1,      singleSwitch,5700,    6070,    180
\end{filecontents}

\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1} 

\pgfplotstablecreatecol[
    create col/copy={Type}
]{Image}{\csvdata}

\begin{document}
\begin{center}
\pgfplotstabletypeset[%
    font=\ttfamily,%
    col sep=comma,%
    columns={Image, ID, Description, Type, X-Pos, Y-Pos},%
    columns/Image/.style={
        column name=Image,%
        assign cell content/.code={%
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{\tikz{\pic{##1};}}
        }
    },
    columns/ID/.style={%
        column name=ID,%
        string type%
    },%
    columns/Description/.style={%
        column name=Description,%
        string type%
    },%
    columns/Type/.style={%
        column name=Type,%
        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}
\end{document}

相关内容