# Specific tikz shape with parameters

The idea is to define several shapes useful for example in control schemes.

For example a generic shape for conversion could be defined instead of creating one shape for each conversion.

The code corresponding to the previous picture was achieved with

\documentclass[tikz]{minimal}

\def\alphabetatodq{
\begin{tikzpicture}
\draw[rounded corners=5pt] (0,0) rectangle (1,1);
\draw (0.1,0.1) -- (.9,.9);
\node at (0.25,0.8) {$\alpha\beta$};
\node at (0.75,0.2) {dq};
\end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}
\node at (0,0) {\alphabetatodq};
\end{tikzpicture}
\end{document}


Another requirement is after my test with the \def stuff, it leads to white spaces around the shape when trying to connect it.

The code is

\documentclass[tikz]{minimal}

\def\alphabetatodq{
\begin{tikzpicture}
\draw[rounded corners=5pt] (0,0) rectangle (1,1);
\draw (0.1,0.1) -- (.9,.9);
\node at (0.25,0.8) {$\alpha\beta$};
\node at (0.75,0.2) {dq};
\end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}
\node (a) at (0,0) {\alphabetatodq};
\draw (-2,0) -- (a) -- (2,0);
\end{tikzpicture}
\end{document}


All this would be nice if the concept can be extended to shape without inner parametric content.

A small hack like this shows the white surrounding spacing problem

\documentclass[tikz]{minimal}
\def\alphabetatodq{
\begin{tikzpicture}
\draw[rounded corners=5pt] (0,0) rectangle (1,1);
\draw (0.1,0.1) -- (.9,.9);
\node at (0.25,0.8) {$\alpha\beta$};
\node at (0.75,0.2) {dq};
\end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}
\node[draw,rounded corners=5pt] (a) at (0,0) {\dctoac};
\draw (-2,0) -- (a) -- (2,0);
\end{tikzpicture}
\end{document}


• You can modify the definition of the circle solidus shape from shapes.multipart library. That would be a genuine node shape so you don't need to nest TikZ pictures. – percusse Jul 3 '13 at 13:10
• Could you develop this a bit more ? I don't get it. – s__C Jul 3 '13 at 13:12
• You can also use the late options like append after command; a couple of references: tex.stackexchange.com/q/98289/13304 for the line and tex.stackexchange.com/a/106836/13304 for getting hints about the labels. – Claudio Fiandrino Jul 3 '13 at 13:28
• The “white surrounding spacing problem” is due the inner sep settings and (if the border is not drawn) the outer sep settings. – Qrrbrbirlbel Jul 3 '13 at 15:19
• Nice fix Qrrbrbirlbel. – s__C Jul 3 '13 at 15:25

Here is a solution via append after command and pgfinterruptpath:

\documentclass[tikz]{standalone}

\tikzset {
convert/.style 2 args={
minimum size=1cm,
rounded corners=5pt,
append after command={
\pgfextra{
\pgfinterruptpath
\path (\tikzlastnode.north west)
node[inner sep=2pt,anchor=north west]{#1};
\path (\tikzlastnode.south east)
node[inner sep=2pt,anchor=south east]{#2};
\draw ([shift={(1mm, 1mm)}]\tikzlastnode.south west)
-- ([shift={(-1mm, -1mm)}]\tikzlastnode.north east);
\endpgfinterruptpath
}
},
}
}

\begin{document}
\begin{tikzpicture}
\node[draw,convert={DC}{AC}] (a) at (0,2) {};
\node[draw,convert={$\alpha\beta$}{dq}] (b) at (2,2) {};
\draw (-2,2) -- (a) -- (b) -- (4,2);
\end{tikzpicture}
\end{document}


Here is a shape called split rectangle with rounded corners. It actually supports different radii for every corner with the keys

• /pgf/rectangle with rounded corners north west radius,
• /pgf/rectangle with rounded corners north east radius,
• /pgf/rectangle with rounded corners south west radius, and
• /pgf/rectangle with rounded corners south east radius.

The key /pgf/rectangle with rounded corners radius to set all radii to the same length.

It is probably not the most efficient way to define that shape but it takes the outer separators into consideration.

However, it does not support different outer separators. It also doesn’t check whether the radii for the corners are not too big. It does not take the line width of the diagonal line into consideration, hopefully this is not a problem in most cases.

The diagonal line will always have an angle of 45° relatively to the node. The node parts and the line will be enclosed in a square. This square will always stretch (if minimum width/minimum height are greater than the possible smallest square).

The PGF library intersection is needed to find the shape border for the part of the rounded corners.

As the code is rather huge, I don’t post it in this answer but it is available at tikzlibraryshapes.qrr.roundedrectangle.code.tex.
Use \usetikzlibrary{shapes.qrr.roundedrectangle} to load.

## Code

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{shapes.qrr.roundedrectangle}
\tikzset{
shape example/.style={
color=black!30,
draw,
fill=yellow!30,
line width=.5cm,
inner xsep=2.5cm,
inner ysep=0.5cm}
}
\begin{document}

\begin{tikzpicture}
\node[
draw,
split rectangle with rounded corners,
line cap=round,
minimum size=1cm
] (n) {$\alpha\beta$ \nodepart{lower} dq};
\foreach \Angle in {0,2.5,...,359} \draw (\Angle:1) -- (n);
\end{tikzpicture}
\begin{tikzpicture}\Huge
\node[draw,split rectangle with rounded corners,shape example,rectangle with rounded corners radius=50pt] (n) {\TeX t \nodepart{lower} more \TeX t};
\foreach \Pos/\Anchor/\Text in {left/below north west,above right/right north west,
above left/left north east,right/below north east,
right/above south east,below left/left south east,
below right/right south west,left/above south west}
\draw[thin] plot[mark=x,only marks] coordinates {
(n.\Anchor)} node[\Pos,text depth=+0pt,text height=+3pt] {\tiny\ttfamily\Text};

\foreach \Pos/\Anchor/\Text in {above/center,above/north,left/west,right/east,below/south,
above left/north west,below left/south west,
below right/south east,above right/north east}
\draw[thin] plot[mark=x,only marks] coordinates {
(n.\Anchor)} node[\Pos,text depth=+0pt,text height=+3pt] {\tiny\ttfamily\Text};

\foreach \Pos/\Anchor/\Text in {above left/mid west,above right/mid east,above/mid,
left/base west,right/base east,
below/base,below/text,below/lower}
\draw[thin] plot[mark=x,only marks] coordinates {
(n.\Anchor)} node[\Pos,text depth=+0pt,text height=+3pt] {\tiny\ttfamily\Text};
\end{tikzpicture}
\end{document}


## Output

• Is there a key to remove the line? In my case, I need to position text at the opposing diagonals of a rectangle, but sometimes without the line. This was the only shape I was able to find that does the job. I assume I also have to save the file tikzlibraryshapes.qrr.roundedrectangle.code.tex to my tikzlibraryshapes directory, right? Thanks for any help. – Sergio Parreiras Dec 9 '15 at 14:47