# How can I get access to the size parameters of a TikZ node?

The following is a refined form of the problem that I posted more than 18 hours ago.

I am new to Tikz. However, even with the little knowledge I have of TikZ, I do appreciate its superiority over the other graphics packages that I have used previously.

I would like to create a TikZ node style that can produce the diagram shown below. I have drawn it using Xfig. However, I would like to draw it in a more flexible way using TikZ. The following constraints pertain to the diagram:

1. The arcs on the left and the right sides of the rectangular structure are semi-circles --- each has a radius that is half the height of the underlying rectangle(s).
2. The two triangles inside the structure are similar.
3. Each triangle is an isosceles triangle whose apex lies on the center-line that is parallel to the top and bottom edges of the rectangular structure.
4. The dimensions of the triangles are fixed fractions of the height of the rectangular structure.
5. The base of the filled triangle is on the left edge of the (imaginary) inner rectangle; and that of the second triangle is on the base right edge of the (imaginary) inner rectangle.

The only parameters that I should be free to alter are the positions of the anchors, the height and the width of the rectangular structure. I need to know how I can access the height and width parameters and use them in the calculation of the other parameters of the structure.

Could some experienced user of TikZ and PGF come to my aid.

Thanks. • Can you post two characteristic examples of a small and a large nodes that you want to alternate? Just plain drawings would be sufficient and a simple rule to switch. – percusse Jan 13 '13 at 21:43
• The figure given below is one example (produced using xfig). Is is possible to have a style in which I can change the width and height of the underlying rectangle to change the size, but otherwise retain the shape of the figure? – user24098 Jan 14 '13 at 5:48
• Sorry, I drew the figure using Xfig but the system could not post it. – user24098 Jan 14 '13 at 5:54
• You can edit your question and add the images there. – percusse Jan 14 '13 at 10:17
• @percusse: Thanks for your readiness to help. I have reformulated the question and provided an example of what I want to draw using TikZ. Thanks in advance. – user24098 Jan 14 '13 at 15:06

Here is one possibility with not-exactly-failproof node construction. The triangles are also accesible with (<nodename>-leftri) and (<nodename>-rightri). The possible failure reason would be rotating the node. It's possible to include that but it's unnecessarily complicated at this stage and I don't know if you ever need it.

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}[mynode/.style args={#1 and #2}{
draw,
minimum height=#1,
minimum width=#2,
rounded corners=0.5*#1,
append after command={
\pgfextra{%
\begin{pgfinterruptpath}
\node[isosceles triangle,
isosceles triangle stretches,
draw=black,inner sep=0,
anchor=west,
minimum width=0.5*#1,
minimum height=0.2*#1,
fill=black]
(\tikzlastnode-leftri) at ([xshift=0.5*#1]\tikzlastnode.west) {};
\node[isosceles triangle,
isosceles triangle stretches,
draw=black,inner sep=0,
anchor=west,
minimum width=0.5*#1,
minimum height=0.2*#1,
isosceles triangle apex angle=90]
(\tikzlastnode-rightri) at ([xshift=-0.5*#1]\tikzlastnode.east) {};
\end{pgfinterruptpath}
}
}
}
]

\node[mynode=1cm and 4cm] (a) {};
\node[mynode=3cm and 8cm] (b) at (0,3) {};
\draw (a-leftri) -- (b-rightri);
\end{tikzpicture}

\end{document} • Thanks for the style that you have posted @percusse. It produces the figure nicely. Let me analyze it and see how I can adapt it to my requirements. – user24098 Jan 15 '13 at 5:44
• I have studied your code and adapted it as shown below. I am able to draw nice pictures with it and I like the results. One more thing: How can one modify so that the wholes structure rotates as a rigid body? – user24098 Jan 16 '13 at 15:54
• @user24098 I'll try to have a look whenever I have the chance but it's a little hectic due to my thesis. If you don't have any text inside the node then you can inherit a rectangle shape and put the triangles in the foreground path as a new shape declaration. See the manual for that construction. – percusse Jan 16 '13 at 18:04

I have considered the possibility of adopting the Object-Oriented approach to the problem. Given below is the code that I came up with. It draws the rectangular outline and the filled triangle (just a little more effort would enable me to include the unfilled triangle).

The code works and allows me to change the height and width parameters, albeit indirectly. The program does what I expect it to do.

However, I still have a feeling that a style would offer me more flexibility; and may incorporate a much more elegant code.

Any help?

\pgfooclass{MyRectangle}{
% This is class Myrectangle:
\method MyRectangle(#1,#2,#3,#4,#5,#6) { % The constructor
% (#1,#2) the SW anchor point.
% (#3,#4) the NE anchor point.
% #5*(#4-#2) the base of the triangles (oriented vertically).
% #6*(#4-#2) the height of the triangles (oriented horizontally).
\draw (#1,#2) -- (#3,#2) arc (-90:90:0.5*#4-0.5*#2) -- (#1,#4)
arc (90:270:0.5*#4-0.5*#2);
\coordinate (B) at (#1,#2+0.5*#4-0.5*#2-0.5*#5*#4-0.5*#5*#2);
\coordinate (C) at (#1+#6*#4-#6*#2,#2+0.5*#4-0.5*#2);
\coordinate (D) at (#1,#2+0.5*#4-0.5*#2+0.5*#5*#4-#5*#2);
\fill (B) -- (C) -- (D);
}
}

\begin{tikzpicture}
\pgfoonew \myFigure=new MyRectangle(0,0,2,0.5,0.4,0.3);
\end{tikzpicture}


@percusse, here is my complete adaptation of your code. It enables the (whole) structure to rotate as a rigid body.

Thanks a lot for the solution that you posted.

\tikzset{Hrect/.style args={#1; #2; #3deg; #4; #5deg}{
outer sep=0,
draw=#4,%
rotate=#5,
minimum height=#1,%
minimum width=#2,%
rounded corners=0.5*#1,%
/.default=black,%
append after command={%
\pgfextra{%
\begin{pgfinterruptpath}%
\node[  isosceles triangle,%
rotate = #5,
inner sep=0,%
anchor=west,%
minimum width=0.3*#1,%
minimum height=0.3*#1,%
isosceles triangle apex angle=#3,%
fill=#4] at ([xshift=0.5*#1*cos(#5),yshift=0.5*#1*sin(#5)]
\tikzlastnode.west) {};%
\node[  isosceles triangle,%
rotate=#5,
inner sep=0,%
anchor=east,%
minimum width=0.3*#1,%
minimum height=0.3*#1,%
isosceles triangle apex angle=#3,%
draw=#4] at ([xshift=-0.5*#1*cos(#5),yshift=-0.5*#1*sin(#5)]
\tikzlastnode.east) {};%
\end{pgfinterruptpath}%
}
}
}
}

\begin{tikzpicture}
\node (a) [Hrect = 0.3cm; 1.5cm; 67deg; blue; 45deg,anchor=south west]
\end{tikzpicture} • Solution to the rotation problem figured out! – user24098 Jan 16 '13 at 19:58