0

Why is the image not centered for the right node?

enter image description here

\usepackage{tikz}
\usepackage{mwe}
\tikzset{
  image/.style={
    path picture={
      \node at (path picture bounding box.center) {
        \includegraphics[width=2cm]{example-image}};}}
}

\begin{document}

\begin{tikzpicture}

\tikzstyle{node}=[rectangle, minimum width=2cm, minimum height=1cm, thick, draw =black!100, node distance = 16mm]

    \node[node, image] (y_src_i) [label=below:$\phi_i$] { };
    \node[node, image] (x_src_i) [right=of y_src_i, label=below:$\phi_j$] { };
\end{tikzpicture}
Tivaro
  • 25
  • 1
    You can either load the positioning library, as Marian G suggests, or use the correct syntax for positioning without that library: right of=y_src_i,. (Just in case you haven't read esdd's comment below. –  Aug 26 '18 at 15:17
  • Thanks! What do you mean by that? I cannot see that comment? – Tivaro Aug 28 '18 at 08:13

1 Answers1

2

First, you need the positioning library. Secondly, you have to specify anchor=center in the image-style to achieve the desired effect.

Code

\documentclass{article}
\usepackage{tikz}
    \usetikzlibrary{positioning}

\usepackage{mwe}
\tikzset{
  image/.style={
    path picture={
      \node[anchor=center] at (path picture bounding box.center) {
        \includegraphics[width=2cm]{example-image}};}},
  node/.style={
  rectangle, minimum width=2cm, minimum height=1cm, thick, draw =black!100, node distance = 26mm}
}

\begin{document}
\begin{tikzpicture}
    \node[node,image] (y_src_i) [label=below:$\phi_i$] {};
    \node[node,image] (x_src_i) [right=of y_src_i, label=below:$\phi_j$] {};
\end{tikzpicture}
\end{document}

Output enter image description here

Marian G.
  • 1,951
  • 1
  • 9
  • 14