2

I was trying to work on a very nice question about building chips for circuitikz, and I have a problem. I reduced it to this MWE:

\documentclass[tikz, border=2mm]{standalone}

\pgfdeclareshape{chip}{
\savedanchor\center{\pgfpoint{0pt}{0pt}}
\anchor{center}{\center}
  \backgroundpath{%
    \pgfpathrectanglecorners{\pgfpoint{-.5cm}{-1cm}}%
    {\pgfpoint{.5cm}{1cm}}%
    \pgftext[right, at=\pgfpoint{.5cm}{0.5cm}]{\tiny 1\ }
    \pgftext[right, at=\pgfpoint{.5cm}{-0.5cm}]{\tiny 12\ }
    \pgftext[left, at=\pgfpoint{-.5cm}{0.5cm}]{\tiny\ 3}
    \pgftext[left, at=\pgfpoint{-.5cm}{-0.5cm}]{\tiny\ 14}
  }%
}

\begin{document}

\begin{tikzpicture}
\node [chip, draw] (A) {A};
\node [chip, draw, rotate=-90](B) at (2,2){B};
\node [chip, draw, rotate=-180](C) at (2,0){C};
\end{tikzpicture}

\end{document}

which produces the following thing:

enter image description here

Which is almost ok. The position of the labels (especially the small numbers) is ok, but --- is it possible to change the shape definition in order to unrotate them? Even if it's only for multiples of 90 degrees woyld be ok.

If there is a way to "read" the rotation from inside the shape, I could for example use ifs to have 4 cases, or something similar.

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • Des not Mark Wibrows answer solve this? (If you want to continue along the path you outline here, you may want to save the point anchors of the text positions and then add \pgftransformresetnontranslations before adding the texts.) –  Feb 05 '19 at 21:26
  • @marmot, the cited answer adds the labels after drawing the pin numbers, outside the shape, while I'd like to have the numbers drawn automatically (in my full code, they're automatically drawn). The other idea could be nice... But I don't know how to use it. – Rmano Feb 05 '19 at 21:37
  • 1
    You want to "unrotate", ok. But what is your reference? The current node, the current path, the current scope, the current tikzpicture, the current box, the current page? – Paul Gaborit Feb 05 '19 at 22:05
  • \backgroundpath{% \pgfpathrectanglecorners{\pgfpoint{-.5cm}{-1cm}}% {\pgfpoint{.5cm}{1cm}}% \pgfcoordinate{RmanoTR}{\pgfpoint{.3cm}{0.5cm}} \pgfcoordinate{RmanoTL}{\pgfpoint{-.3cm}{0.5cm}} \pgfcoordinate{RmanoBR}{\pgfpoint{.3cm}{-0.5cm}} \pgfcoordinate{RmanoBL}{\pgfpoint{-.3cm}{-0.5cm}} \pgftransformresetnontranslations \pgftext[at=\pgfpointanchor{RmanoTR}{center}]{\tiny 1\ } \pgftext[at=\pgfpointanchor{RmanoBR}{center}]{\tiny 12\ } \pgftext[at=\pgfpointanchor{RmanoTL}{center}]{\tiny\ 3} \pgftext[at=\pgfpointanchor{RmanoBL}{center}]{\tiny\ 14} }%? –  Feb 05 '19 at 22:13
  • @PaulGaborit good question... I think the current tikzpicture would be nice. – Rmano Feb 05 '19 at 22:35
  • @marmot thanks, now I am going to sleep... Will check tomorrow when I have time. I just checked the TikZ manual and could be... – Rmano Feb 05 '19 at 22:37
  • I do believe @PaulGaborit has a very good point. My above proposal is not really good, nor elegant. –  Feb 05 '19 at 22:38
  • @marmot - The comment/solution works great, but you lose all the formatting. Please resubmit as an answer. – John Kormylo Feb 06 '19 at 17:23
  • BTW, the \text savedanchor is used to place the node text, but I don't think you can control the rotation (unless you use \rotatebox from graphicx). – John Kormylo Feb 06 '19 at 17:29

1 Answers1

3

Here is a proposal. The issue is that the keys right, left, top and bottom are discretized, meaning that the result won't look good for all possible rotation angles.

  1. \pgfgettransformentries\a\b\temp\temp\temp\temp reads the entries of the rotation matrix out. Notice that this version of the answer assumes that you do not do any stretching. The rotation angle is computed.
  2. Then the quadrant mod 2 is determined in order to toggle from right and left to top and bottom aligned texts.
  3. The entries are "unrotated".

\documentclass[tikz, border=2mm]{standalone}

\pgfdeclareshape{chip}{
\savedanchor\center{\pgfpoint{0pt}{0pt}}
\anchor{center}{\center}
  \backgroundpath{%
    \pgfpathrectanglecorners{\pgfpoint{-.5cm}{-1cm}}%
    {\pgfpoint{.5cm}{1cm}}%
    \pgfgettransformentries\a\b\temp\temp\temp\temp
    \pgfmathsetmacro{\rot}{-atan2(\b,\a)}
    \pgfmathtruncatemacro{\quadrant}{mod(4+int(360+(\rot+45)/90),4)}
    \ifcase\quadrant
      \pgftext[rotate=\rot,right, at=\pgfpoint{.5cm}{0.5cm}]{\tiny 1\ }
      \pgftext[rotate=\rot,right, at=\pgfpoint{.5cm}{-0.5cm}]{\tiny 12\ }
      \pgftext[rotate=\rot,left, at=\pgfpoint{-.5cm}{0.5cm}]{\tiny\ 3}
      \pgftext[rotate=\rot,left, at=\pgfpoint{-.5cm}{-0.5cm}]{\tiny\ 14}
    \or
      \pgftext[rotate=\rot,top, at=\pgfpoint{.5cm}{0.45cm}]{\tiny 3\ }
      \pgftext[rotate=\rot,bottom, at=\pgfpoint{.5cm}{-0.45cm}]{\tiny 1\ }
      \pgftext[rotate=\rot,top, at=\pgfpoint{-.5cm}{0.45cm}]{\tiny\ 14}
      \pgftext[rotate=\rot,bottom, at=\pgfpoint{-.5cm}{-0.45cm}]{\tiny\ 12}
    \or
      \pgftext[rotate=\rot,right, at=\pgfpoint{.5cm}{0.5cm}]{\tiny 14\ }
      \pgftext[rotate=\rot,right, at=\pgfpoint{.5cm}{-0.5cm}]{\tiny 3\ }
      \pgftext[rotate=\rot,left, at=\pgfpoint{-.5cm}{0.5cm}]{\tiny\ 12}
      \pgftext[rotate=\rot,left, at=\pgfpoint{-.5cm}{-0.5cm}]{\tiny\ 1}
    \or
      \pgftext[rotate=\rot,top, at=\pgfpoint{.5cm}{0.45cm}]{\tiny 12\ }
      \pgftext[rotate=\rot,bottom, at=\pgfpoint{.5cm}{-0.45cm}]{\tiny 14\ }
      \pgftext[rotate=\rot,top, at=\pgfpoint{-.5cm}{0.45cm}]{\tiny\ 1}
      \pgftext[rotate=\rot,bottom, at=\pgfpoint{-.5cm}{-0.45cm}]{\tiny\ 3}
    \fi  
  }%
}

\begin{document}

\begin{tikzpicture}
\node [chip, draw] (A) {A};
\node [chip, draw, rotate=-90](B) at (2,2){B};
\node [chip, draw, rotate=-180](C) at (2,0){C};
\node [chip, draw, rotate=90](B) at (0,2){D};
\end{tikzpicture}

\end{document}

enter image description here

When reading the question, I was not sure if you want to "unrotate" the main labels as well. If you do, then one may need to use a different approach (I have not tried doing this).

  • Thanks for the idea; almost there. The only problem is that this way the pin number's order is wrong - it should remain the same, as if the box is rotated rigidly. Probably I need to do the full 4 quadrants cases. – Rmano Feb 06 '19 at 07:48
  • 1
    @Rmano My bad. Originally I had four cases, which is why there was an \ifcase. I did not look carefully and thought one needs only two. Now I went back to 4 cases. –  Feb 06 '19 at 15:42