I'm trying to clarify how I can create a style that basically add some nodes/path on top (or even behind if possible) of a node. I tried to play with append after command and I finally managed to add several nodes, but I can't find how to add arbitrary code, and especially I can't add a path. What is the good way to proceed?
Thank you!
MWE:
\documentclass[]{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds,decorations.pathreplacing,calc,math,matrix}
\usepackage{forest}
% My style
\tikzstyle{myboxes}=[draw=#1,fill=#1!20,rounded corners,anchor=base]
\begin{document}
\begin{frame}{My test}
\begin{figure}
\centering
\begin{tikzpicture}[
n/.style={myboxes=orange},
keep name/.style={prefix after command={\pgfextra{\let\fixname\tikzlastnode}}},
c/.style={
keep name,
append after command={
node [
at=(\fixname.north),
inner sep=3pt,
draw=red,
thick,
inner sep=-\pgflinewidth,
cross out
] {}
node [
at=(\fixname.south),
inner sep=3pt,
draw=green,
thick,
inner sep=-\pgflinewidth,
cross out
] {}
% Does not work
% draw [red,very thick] (\fixname.north) -- (\fixname.south)
}
}
]
\matrix[matrix of nodes, row sep=2mm](sel){
|[n,c]|A\\
|[n,c]|C \\
|[n]|B\\
};
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}
-- EDIT --
I tried with path picture as proposed by marmot, but unfortunately I can't go outside of the bounding box:
\documentclass[]{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,backgrounds,decorations.pathreplacing,calc,math,matrix}
\usepackage{forest}
% My style
\tikzstyle{myboxes}=[draw=#1,fill=#1!20,rounded corners,anchor=base]
\begin{document}
\begin{frame}{My test}
\begin{figure}
\centering
\begin{tikzpicture}[
n/.style={myboxes=orange},
c/.style={
path picture = {
\draw[-latex] ($(path picture bounding box.north west)$) -- ($(path picture bounding box.south east)$);
\draw[-latex] ($(path picture bounding box.north west)$) -- ($(path picture bounding box.north east)$);
}
}
]
\matrix[matrix of nodes, row sep=2mm](sel){
|[n,c]|A\\
|[n,c]|C \\
|[n]|B\\
};
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}
-- EDIT 2 -- The best general solution seems to be, as noticed by Ignasi, the one proposed by Marmot here. However it's not working on matrices, where basically all the points are centered near the central point because of the layers...
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,positioning}
\usetikzlibrary{backgrounds}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\tikzset{
% keep name/.style={prefix after command={\pgfextra{\let\fixname\tikzlastnode}}},
myt/.style={
% keep name,
fill=green!30,
rounded corners,
draw=green,
append after command={\pgfextra%
% \fixname does not solve the problem either
\begin{pgfonlayer}{background}
\draw[-latex,blue] (\tikzlastnode.north west) -- (\tikzlastnode.south east);
\node[orange] at (\tikzlastnode.north) {\tiny \scalebox{.5}{A}};
\node[orange] at (\tikzlastnode.south) {\tiny \scalebox{.5}{B}};
\end{pgfonlayer}
\node[] at ($(\tikzlastnode.east)!.5!(\tikzlastnode.north east)$) {\tiny \scalebox{.5}{Samelevel}};
\begin{pgfonlayer}{foreground}
\draw[-latex,red] (\tikzlastnode.south west) -- (\tikzlastnode.north east);
\node[purple] at (\tikzlastnode.east) {\tiny \scalebox{.5}{D}};
\node[purple] at (\tikzlastnode.west) {\tiny \scalebox{.5}{C}};
\end{pgfonlayer}
\endpgfextra}
}
}
\begin{document}
\begin{tikzpicture}
\node[myt](a){a};
\node[right=of a, myt](b){b};
\node[myt, right=of b](c){c};
\matrix[matrix of nodes, right=of c](matrixname){
|[myt]|U & |[myt]|V \\
|[myt]|W & |[myt]|X \\
};
\end{tikzpicture}
\end{document}
Output works on single nodes:
But not on matrices:
-- EDIT 3 & 4 --
After the proposition of using pics, I tried an indeed it seems really powerful. The only problem is that pics are not really styles, but they can simulate styles pretty well else, and it's even possible to "compose" them and they can also be included in matrices. See below the code and picture:
\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes,calc}
\begin{document}
\tikzset{
% Args: style of the main node, content of the main node
% and color of back arrow and node
pics/addarrows/.style args={#1/#2/#3}{
code = {
\node[rounded corners, #1,opacity=0] (tmpnode) {#2};
\draw[-latex,#3] ($(tmpnode.west)+(-0.2,0)$) -- ($(tmpnode.east)+(0.2,0)$);
\node[rounded corners, #1] {#2};
\draw[-latex] (tmpnode.south west) -- (tmpnode.north east);
\draw[-latex] (tmpnode.north west) -- (tmpnode.south east);
\node[fill=#3,circle,inner sep=2pt] at (tmpnode.south) {};
}},
% Creates a shortcut
pics/defarrows/.style args={#1}{
code = {
\pic{addarrows={draw=red,fill=red!20}/#1/red};
}
},
pics/composableHorizVert/.style={
code={
\begin{scope}[transparency group, opacity=0]
#1
\end{scope}
\draw[-latex] ($(tmpnode.west)+(-0.2,0)$) -- ($(tmpnode.east)+(0.2,0)$);
#1
\draw[-latex] ($(tmpnode.north)+(0,0.2)$) -- ($(tmpnode.south)+(0,-0.2)$);
}
},
pics/composableDiag/.style={
code={
\begin{scope}[transparency group, opacity=0]
#1
\end{scope}
\draw[-latex] ($(tmpnode.north west)+(-0.2,0.2)$) -- ($(tmpnode.south east)+(0.2,-0.2)$);
#1
\draw[-latex] ($(tmpnode.north east)+(0.2,0.2)$) -- ($(tmpnode.south west)+(-0.2,-0.2)$);
}
}
}
Some defaults pics on a matrix:\\
\begin{tikzpicture}[auto]
\matrix[](matrixname){
\pic{defarrows=A}; & \pic{defarrows=B}; \\
\pic{defarrows=C}; & \pic{defarrows=DEF};\\
};
\end{tikzpicture}
Some pics with arguments that specify the ``main node style'':\\
\begin{tikzpicture}[auto]
\pic [local bounding box=picgreen] {addarrows={draw=green,fill=green!20,ellipse}/My text/purple};
\pic [below right=of picgreen,local bounding box=picyellow] {addarrows={draw=orange,fill=yellow,ellipse}/My text/red};
% NB: If you want to specify a specific node inside the pic, you can always try to specify
% a name through a new argument
\draw[-latex,dashed] (picgreen) -- (picyellow);
\end{tikzpicture}
Show that it's possible to compose ``pics styles'':\\
\begin{tikzpicture}[baseline]
\pic{composableHorizVert={\node[name=tmpnode,fill=red!30]{A};}};
\end{tikzpicture}
+
\begin{tikzpicture}[baseline]
\pic{composableDiag={\node[name=tmpnode,fill=red!30]{A};}};
\end{tikzpicture}
=
\begin{tikzpicture}[baseline]
\pic{composableHorizVert={\pic{composableDiag={\node[name=tmpnode,fill=red!30]{A};}};}};
\end{tikzpicture}
\end{document}






path picture. – Apr 25 '18 at 16:05tikzpictureinside abeamerframe you needfragileoption:\begin{frame}[fragile]{My test}. – Ignasi Apr 25 '18 at 17:47