4

I need to fill a triangular node with an image, and rotate it at various angles. My approach for filling the node with an image is based on this one, and it works great if my node is not rotated, or rotated 180 degrees. But for other rotation angles, the node and the image do not rotate in sync. I suspect the problem involves the anchor, and I tried anchor=center, but it had no effect.

MWE:

\documentclass[margin=5pt]{standalone}
\usepackage{mwe}
\usepackage{tikz}
\usepgflibrary{shapes.geometric}
\begin{document}

    \begin{tikzpicture}[
        tile/.style={regular polygon, 
          regular polygon sides=3, 
          draw,
          minimum width=5cm, 
          minimum height=5cm, 
          rotate=#1, 
          path picture={
               \node at (path picture bounding box.center){
                   \includegraphics[width=5cm,angle=#1]{example-image-a}
               };
          }}
    ]

        % node 1, not rotated
        \node[tile={0}] at (0,0) {};
        % node 2, rotated 180
        \node[tile={180}] at (5,0) {};
        % node 3, rotated 120
        \node[tile={120}] at (0,-5) {};
        % node 4, rotated 57
        \node[tile={57}] at (5,-5) {};

    \end{tikzpicture}       

\end{document}  

Result:

enter image description here

Follow-up question: What if the node and the image are already rotated at different angles? For example, if I want to fill an "upside-down" triangle with a "right-side-up" image, then I have to use rotate=180 for the node but angle=0 for the image. Then I want to rotate the whole thing with those angles as a starting point.

4

The path picture bounding box is always a box whose edges are horizontal or vertical, which explains the displacements. So you need anchors that rotate with the node.

\documentclass[margin=5pt]{standalone}
\usepackage{mwe}
\usepackage{tikz}
\usepgflibrary{shapes.geometric}
\begin{document}

    \begin{tikzpicture}[
        tile/.style={regular polygon,name=3gon, 
          regular polygon sides=3, 
          draw,
          minimum width=5cm, 
          minimum height=5cm, 
          rotate=#1, 
          path picture={
               \node at ([yshift=0.6cm]3gon.center){
                   \includegraphics[width=5cm,angle=#1]{example-image-a}
               };
          }}
    ]

        % node 1, not rotated
        \node[tile={0}] at (0,0) {};
        % node 2, rotated 180
        \node[tile={180}] at (5,0) {};
        % node 3, rotated 120
        \node[tile={120}] at (0,-5) {};
        % node 4, rotated 57
        \node[tile={57}] at (5,-5) {};

    \end{tikzpicture}       

\end{document} 

enter image description here

If you want to give these nodes different names, use alias.

  • Where does the 0.6cm come from? Is that calculated somehow or just trial and error? – LarrySnyder610 May 21 at 2:13
  • 1
    @LarrySnyder610 This is chosen such that the result resembles yours for 0 or 180 degrees rotation. Note that the center of the node does not necessarily coincide with the center of the path picture. Here it does not, for instance, hence the shift. You can compute it from first principles if you look up the definition or use shape uses incircle or something of that sort. The main point of the answer is that the center of the path picture moves, see this related question. – user121799 May 21 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.