3

I would like to change the content of my pgf table column "Image" from using an included graphic to a predefined TikZ \pic based on the information in column "Type". The \pic should be centered horizontally and vertically inside of the table.

Here is my working 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}

For Example we could use these two mini 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}
  }
 }
}
4
  • 2
    Pleas add a MWE - see tex.meta.stackexchange.com/questions/228/… Commented Feb 26 at 14:22
  • You should replace \cincludegraphics[scale=0.07]{../Images/ElectricComponents/##1.png}} (why \cincludegraphics by the way?) to something like \tikz{ \pic {button}; }. Your follow-up question can only be answered if you provide a full MWE, because currently there are parts in the code of which the defintion is unclear. Commented Feb 26 at 15:14
  • Please take the burden to rework your code snippets into something we can work with, e.g. using tex.meta.stackexchange.com/a/10137/245790 . Thank you
    – MS-SPO
    Commented Feb 26 at 17:15
  • Sorry, all of you are completely right! I edited my whole question, added a working MWE which can be compiled with overleaf and added two \pic's for the answer example :)
    – PascalS
    Commented Feb 26 at 18:03

1 Answer 1

3

You can just replace \includegraphics[scale=0.07]{example-image} in your code by \tikz{ \pic{<pic name>}; }.

The second part in the key pics/stove/.style (that is stove) is the name of the pic. So, if you define different pics, you should name them differently. If you define a pic named stove this way, you can call it via \pic{stove}; in a tikzpicture.

Note that there are some commands in the defintions of the pics you show, that are not defined, so I needed to define them in the below 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}

enter image description here


If the Type in your table data is identical to the name of the relevant \pic (that is, the Image and the Type columns are identical copies), you can delete the Image column from your table and create this column as copy of the Type column automatically:

\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}
7
  • Could you center the \pic's vertically? And can we remove the column Image in raw data and still show it in the result table? The \pic can be defined by the column type.
    – PascalS
    Commented Feb 26 at 19:10
  • I got the first topic by myself :) With \raisebox{-0.3\height}{\tikz{\pic[scale=0.003]{##1}; the \pic is beautiful centered! But could you maybe have a look on the second part of my question? I don't get it how to create such an imagecolumn in my result table, without having this column also in the input.csv. This information could be removed in the input data, because the type information which is used to determine the correct \pic is already given in the Type column...
    – PascalS
    Commented Feb 28 at 17:55
  • @PascalS I don't know exactly what you mean. Currently, the image column contains just the name of the \pic{<name>}. But you mean that you want to use the contents from the type column in two columns in the typset table? Commented Feb 28 at 18:39
  • The point is, that there are two columns which have exactly the same content. This is, because the type is automatically also the \pic's name. Therefore one column is redundant. So I just search for a way to choose the '\pic''s name from the type column and in the best case I also want to remove the Image column from my raw data. Because in my raw I don't need the image column information, when it is already stored in the type column. The reason behind is, that there will be a lot of columns and the more I can reduce, the better I can oversee :)
    – PascalS
    Commented Feb 28 at 19:53
  • @PascalS You can automatically make copies from existing columns. See the last edit of my answer. Commented Feb 28 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .