1

This is a follow up to this question.

Once I positioned the node that crops the image relative to another node, the cropped image is not centered any more inside the circle.

How to fix it?

I want the letter "A" of the example-image-A to be centered.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[every node/.append style={draw=blue,line width=1pt},background rectangle/.style={fill=olive!25}, show background rectangle]

\node [text width=0.5\textwidth,font={\large\bfseries\sffamily},align=center] at (0,0) (A) {% John Doe }; \node[above= 1 ex of A,circle,draw,minimum size=0.3\textwidth, path picture={ \node at (path picture bounding box.center){ \includegraphics[width=0.4\textwidth]{example-image-a.png} }; }] (B) {}; \end{tikzpicture} \end{document}

enter image description here

tush
  • 1,115

1 Answers1

2

What's happening?

Your inner node (the one inside the path picture) inherits all options of its parent node (and also the every node style, of course). Importantly, it does inherit anchor=south that is implicitly set by above=….

You need anchor=center at the path picture node to reset this.

Alternative to \node

If you don't actually need all the bells and whistle of a node and just want to insert a picture (or text) in the path picture, you might better be off using the \pgftext macro.


Since the path picture's center of an otherwise empty node is always at its origin, we can actually leave the at part out in both cases.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{backgrounds,positioning}
\begin{document}
\begin{tikzpicture}[
  every node/.append style={draw=blue, line width=1pt},
  background rectangle/.style={fill=olive!25},
  show background rectangle]
\node[
  text width=0.5\textwidth, font=\large\bfseries\sffamily,
  align=center] at (0,0) (A) {John Doe};
\node[above=1ex of A, circle, minimum size=0.3\textwidth, path picture={
    \node[anchor=center, draw=none, rectangle]
%        at (path picture bounding box.center)
      {\includegraphics[width=0.4\textwidth]{example-image-a.pdf}};}] (B) {};
\end{tikzpicture}
\begin{tikzpicture}[
  every node/.append style={draw=blue, line width=1pt},
  background rectangle/.style={fill=olive!25},
  show background rectangle]
\node[
  text width=0.5\textwidth, font=\large\bfseries\sffamily,
  align=center] at (0,0) (A) {John Doe};
\node[above=1ex of A, circle, minimum size=0.3\textwidth, path picture={
    \pgftext[
%      at=\pgfpointanchor{path picture bounding box}{center}
    ]{\includegraphics[width=0.4\textwidth]{example-image-a.pdf}};}] (B) {};
\end{tikzpicture}
\end{document}

Even better: The tikzfill package.

That said, there seems to be a tikzfill package that already solves all your problems, maybe?

Here, I'm using the fill overzoom image that stretches a given image so that it fully covers the path picture.

Code

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{positioning}
\usetikzlibrary{fill.image}
\begin{document}
\begin{tikzpicture}[
  every node/.append style={
    draw=blue,
    line width=1pt},
  background rectangle/.style={
    fill=olive!25},
  show background rectangle]

\node[ text width=0.5\textwidth, font=\large\bfseries\sffamily, align=center] at (0,0) (A) {John Doe};

\node[above=1ex of A, circle, minimum size=0.3\textwidth, fill overzoom image={example-image-a.pdf} ] (B) {}; \end{tikzpicture} \end{document}

Qrrbrbirlbel
  • 119,821