tikz block diagonally divided
The computations are basic,
and if you consider using a pic
instead of a full-fledged the node shape, the implementation is straightforward, too. The free parameter is taken to be the height of the full shape. The width is then computed in such a way that the diagonal does not cut any of the nodes (where the "safety distance" is given by the inner sep
).
\documentclass{article}
\usepackage{tikz}
\tikzset{pics/dbox/.style 2 args={code={%
\pgfmathsetmacro{\w}{max((width("#1")+2*\pgfkeysvalueof{/pgf/inner xsep})/(\pgfkeysvalueof{/tikz/dbox/height}-2*\pgfkeysvalueof{/pgf/inner xsep}-height("#1")),%
(width("#2")+2*\pgfkeysvalueof{/pgf/inner xsep})/(\pgfkeysvalueof{/tikz/dbox/height}-2*\pgfkeysvalueof{/pgf/inner xsep}-height("#2")))*\pgfkeysvalueof{/tikz/dbox/height}}
\path[pic actions] (-\w*1pt/2,-\pgfkeysvalueof{/tikz/dbox/height}/2)
node[above right] {#2}
rectangle
(\w*1pt/2,\pgfkeysvalueof{/tikz/dbox/height}/2)
node[below left] {#1} (-\w*1pt/2,\pgfkeysvalueof{/tikz/dbox/height}/2)
-- (\w*1pt/2,-\pgfkeysvalueof{/tikz/dbox/height}/2) ;
}},dbox/.cd,height/.initial=2cm}
\begin{document}
\begin{tikzpicture}
\path pic[draw]{dbox={abc}{xyzuv}} (3,0) pic[draw,blue]{dbox={abc}{xyz}}
(6,0) pic[draw,red,thick]{dbox={abcdefgh}{xyz}};
\end{tikzpicture}
\end{document}
You can give this shape the usual anchors with a simple trick: just turn it into a node using fit
. The names have then to be set using dbox/name=<name>
.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\tikzset{pics/dbox/.style 2 args={code={%
\pgfmathsetmacro{\w}{max((width("#1")+2*\pgfkeysvalueof{/pgf/inner xsep})/(\pgfkeysvalueof{/tikz/dbox/height}-2*\pgfkeysvalueof{/pgf/inner xsep}-height("#1")),%
(width("#2")+2*\pgfkeysvalueof{/pgf/inner xsep})/(\pgfkeysvalueof{/tikz/dbox/height}-2*\pgfkeysvalueof{/pgf/inner xsep}-height("#2")))*\pgfkeysvalueof{/tikz/dbox/height}}
\path (-\w*1pt/2,-\pgfkeysvalueof{/tikz/dbox/height}/2)
node[above right] (bl) {#2}
rectangle
(\w*1pt/2,\pgfkeysvalueof{/tikz/dbox/height}/2)
node[below left] (tr) {#1};
\node[pic actions,inner sep=0pt,fit=(bl)(tr),path picture={\path[pic actions]
(path picture bounding box.north west)
-- (path picture bounding box.south east);}]
(\pgfkeysvalueof{/tikz/dbox/name}){};
}},dbox/.cd,height/.initial=2cm,name/.initial=}
\begin{document}
\begin{tikzpicture}
\path pic[draw] {dbox={abc}{xyzuv}} (3,0)
pic[draw,blue,dbox/name=A] {dbox={abc}{xyz}}
(6,0) pic[draw,red,thick,rounded corners,dbox/name=B] {dbox={abcdefgh}{xyz}};
\draw[stealth-stealth] (A.north) to[out=80,in=100](B.100);
\end{tikzpicture}
\end{document}