5

I'm trying to build upon the rectangle node shape but what I came up with doesn't rotate nicely (the hashed pattern, the second rectangle). As you can see, I've tried adding as many transform shape options as I could think of, without success.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}

\tikzset{%
  miroir/.style = {%
    minimum width = 1cm,
    minimum height = .4cm,
    rectangle,
    transform shape,
    fill = gray!10,
    pattern = north east lines,
    append after command = {%
      \pgfextra{%
        \begin{pgfinterruptpath}[transform shape]
            \draw[transform shape,
                  very thin, fill=gray!10,shorten >=0.2bp, shorten <=0.2bp
            ]
              (\tikzlastnode.north west)
              -- ++(0,.2)
              -- ($(\tikzlastnode.north east)+(0,.2)$)
              -- (\tikzlastnode.north east)
              --cycle;
        \end{pgfinterruptpath}
      }
    }
  }
}

\node [miroir,transform shape,rotate=0] at (0,0) {};
\end{tikzpicture}
\end{document}

With rotate=0, it looks the way I intended

enter image description here

but if I use rotate=45, it's messed up:

enter image description here

How can I fix this ?

  • You can't link to images that are on your computer, you need to upload them. – Torbjørn T. Jun 25 '16 at 14:01
  • Sorry about that,, it's fixed now. – wilk Jun 25 '16 at 14:04
  • You can fix the rectangle on top by putting it in a scope rotated by the same angle, but you first need to know what that angle is. \pgfkeysvalueof{/tikz/rotate} doesn't seem to help. In any case, patterns don't rotate. I would suggest using \rotatebox from graphicx. – John Kormylo Jun 25 '16 at 15:09
  • Ah! [rotate] isn't a value, it's a command. \csname pgftransform@angle\endcsname contains the value, but isn't always defined. – John Kormylo Jun 25 '16 at 15:43
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – samcarter is at topanswers.xyz Nov 3 '17 at 14:03
2

This is my very manual solution since my \pgfmath is almost 0. I manually draw the pattern since we cannot rotate the predefined patterns. In the for-loop, I use a large number to fill in the rectangle. I don't know how to calculate the number of loop base on the box size and on the distance between two consecutive lines. Another downside of my "solution" is that you have to enter the rotation angle as parameter for the memoir style, too.

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}
\begin{tikzpicture}

\tikzset{%
  miroir/.style = {%
    minimum width = 1cm,
    minimum height = .4cm,
    %pattern = north east lines,
    append after command = {%
      \pgfextra{%
        \begin{pgfinterruptpath}
              \fill[gray!10,rotate=#1] ($(\tikzlastnode.north west)$) rectangle
              ($(\tikzlastnode.south east)$);
            \draw[very thin, fill=gray!10,shorten >=0.2bp, 
                  shorten <=0.2bp, 
                  rotate={#1},
            ]
              (\tikzlastnode.north west)
              -- ++(0,.2)
              -- ($(\tikzlastnode.north east)+(0,.2)$)
              -- (\tikzlastnode.north east)
              --cycle;
              \clip[rotate=#1] ($(\tikzlastnode.north west)$) rectangle
              ($(\tikzlastnode.south east)$);
              \foreach\i in {0,...,20}{
                \draw[rotate=#1] ($(\tikzlastnode.south east) -
                \i*(2pt,0) - (.4pt,0)$) -- ++(1cm,1cm);
              }
        \end{pgfinterruptpath}
      }
    }
  }
}

\node [rotate=45,miroir=45] at (0,0) {};
\end{tikzpicture}
\end{document}

enter image description here

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.