# Centering images in a TikZ matrix node

I would like to construct a figure using a collection of images/icons as nodes.

I'd like to be able to use matrix, relative positioning and draw arrows between them.

I'm a bit stumped as to why my current solution (see below) displays the images centered at the lower left corner of node boxes, rather than at the center.

I've found that img./style={align=center} solves the problem of centering them horizontally.

Any ideas as on how to align them vertically as well?

Use of pgfdeclareimage and pgfbox are not required, but just a part of my attempt to get it to work.

Code:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\begin{document}
% a bunch of images at 64pxx64px
\pgfdeclareimage{observer}{figures/engineer.png}
\pgfdeclareimage{home}{figures/home.png}
\pgfdeclareimage{user}{figures/user.png}
\pgfdeclareimage{framework}{figures/user.png}

\tikzset{
% style for inserting images as nodes
img/.style={
text width=2cm,
text height=2cm,
rectangle,
align=center,
draw} % only for debugging..
}

\begin{tikzpicture}
\matrix[nodes=img, column sep=0.2cm]{
\node[] (user) {\pgfbox[center,center]{\pgfuseimage{user}}}; &
\node[ right=of user] (home) {\pgfbox[center,center]{\pgfuseimage{home}}}; &
\node[ right=of home] (obse) {\pgfbox[center,center]{\pgfuseimage{observer}}}; \\
};
\end{tikzpicture}
\end{document}


Result:

• Welcome to TeX.SX!. When asking questions it is better to provide a full minimal working example (MWE) both in order to demonstrate what you are trying to do and to help others help you. The MWE should look like \documentclass...\begin{document}...\end{document}, it should compile and contain close to the minimal amount of code needed to explain/demonstrate what you are asking. This saves everyone time:) – user30471 Feb 4 '15 at 12:07
• one possibility is just include your png pictures with \includegraphics command in node, for example \node {\includegraphics{your picture}; . – Zarko Feb 4 '15 at 12:26
• @kidmose Welcome to TeX.SX! I've edited your post to hopefully make it clearer. I also uploaded your image to the official StackExchange imgur server, so that in case you delete the image from your Dropbox, the image will still be available here for any future visitors. If you feel I've changed the intention of your post, please feel free to edit it and change anything back :-) – darthbith Feb 4 '15 at 12:57

## 1 Answer

This is one possibility as suggested by Zarko.

\documentclass[tikz,border=4mm]{standalone}
\usetikzlibrary{matrix}
\newcommand{\ing}[1]{\includegraphics[width=2cm]{#1}}
\tikzset{
% style for inserting images as nodes
img/.style={
text width=2cm,
%text height=2cm,                 %% don't use this
inner sep=0pt,     %% use this
outer sep=0pt,     %% and this
rectangle,
align=center,
draw,thick} % only for debugging..
}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes=img, column sep=0.2cm]{
\ing{example-image-a} &
\ing{example-image-b} &
\ing{example-image-c} \\
};
\end{tikzpicture}
\end{document}


Notes:

1. Don't use text height as it will spoil the show.
2. Use width value in \ing and text width as you wish.
3. inner and outer sep with zero value give better look.