
考慮以下 MWE:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{3d}
\makeatletter
\tikzset{
fitting node/.style={
inner sep=0pt,
fill=none,
draw=none,
reset transform,
fit={(\pgf@pathminx,\pgf@pathminy) (\pgf@pathmaxx,\pgf@pathmaxy)}
},
reset transform/.code={\pgftransformreset},
}
\makeatother
\tikzstyle{dr} = [draw, rectangle, line width=1pt, font=\Huge, align=left]
\tikzstyle{cl} = [>=latex,->,line width=1pt]
\begin{document}
\begin{tikzpicture}[
% x={(0.5cm,0.5cm)}, y={(1cm,0cm)}, z={(0cm,1cm)},
% every node/.append style={transform shape},
]
\draw[very thick] (0,0) rectangle (18,8) node[fitting node] (starter) {};
\begin{scope}[
shift={(1,1)},
% canvas is yx plane at z=0.25,
]
\node[dr] (NodeA) at (1,5) {Node A};
\draw[dr] (4.5,2.5) rectangle (11,6.5) node [fitting node] (NodeB) {Node B\\Slightly bigger};
\node[dr] (NodeC) [above=10pt of NodeB.south, anchor=south] {Node C};
\draw[cl] (NodeA) -- (NodeB);
\draw[cl] (NodeA) |- ($(NodeB.north west)-(10pt,10pt)$) -- ($(NodeB.south west)-(10pt,7.5pt)$) -| ($(NodeC.south west)+(10pt,0)$);
\end{scope}
\end{tikzpicture}
\end{document}
如果我按原樣編譯它,我會得到節點和連接線的預期排列:
但如果我透過取消 MWE 中註解行的註解來啟用 3D,我會得到以下結果:
NodeB 文字的不正確並沒有讓我感到那麼困擾;令我困擾的是,基於 NodeB 擬合節點矩形的座標在 3D 轉換下無法正常工作(請注意,「正確」節點之間一切正常,例如直接進入 NodeC 的那條線)。
(我知道這個例子,特別是,可以在沒有擬合節點矩形的情況下完成 - 所以問題就解決了;問題是,這些對於 TikzEdt 中的 GUI 編輯非常有用,所以我有大量的程式碼這些到處都是;這就是為什麼我寧願有一個解決方案,例如修復“擬合節點”樣式)。
答案1
我不知道問題出在哪裡,可能是transform shape
沒有應用在fit
節點。如果沒有人提供更好的解決方案,您可以在這裡找到解決方法。
用作節點label={center:{node text}}
的選項fitting
:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{3d}
\makeatletter
\tikzset{
fitting node/.style={
inner sep=0pt,
fill=none,
draw=none,
reset transform,
fit={(\pgf@pathminx,\pgf@pathminy) (\pgf@pathmaxx,\pgf@pathmaxy)}
},
reset transform/.code={\pgftransformreset},
}
\makeatother
\tikzstyle{dr} = [draw, rectangle, line width=1pt, font=\Huge, align=left]
\tikzstyle{cl} = [>=latex,->,line width=1pt]
\begin{document}
\begin{tikzpicture}[
x={(0.5cm,0.5cm)}, y={(1cm,0cm)}, z={(0cm,1cm)},
every node/.append style={transform shape},
]
\draw[very thick] (0,0) rectangle (18,8) node[fitting node] (starter) {};
\begin{scope}[
shift={(1,1)},
canvas is yx plane at z=0.25,
]
\node[dr] (NodeA) at (1,5) {Node A};
\draw[dr] (4.5,2.5) rectangle (11,6.5) node [fitting node, label={center:{Node B\\Slightly bigger}}] (NodeB) {};
\node[dr] (NodeC) [above=10pt of NodeB.south, anchor=south] {Node C};
\draw[cl] (NodeA) -- (NodeB);
\draw[cl] (NodeA) |- ($(NodeB.north west)-(10pt,10pt)$) -- ($(NodeB.south west)-(10pt,7.5pt)$) -| ($(NodeC.south west)+(10pt,0)$);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
編輯:請注意,這個答案在 Texlive 2014 凍結中被破壞:
好吧,我想我明白了,但目前不太明白為什麼。
首先,問題是它fit
完全按照它的要求進行操作 - 我們告訴它適合最後一個 pgf min 和 max x 和 y,它確實做到了這一點(可以通過更改為draw=red
of來看到fitting node
):
問題似乎是,矩形在到達 時已經變換fit
,但fit
不知道 3D 變換。
所以我試圖停止變換,然後執行矩形擬合節點,然後恢復變換 - 不起作用;試圖尋找一種方法來將 pgf 繪圖指令分組,然後對所有指令套用變換 - 找不到。
然後我發現#47797 如何在 (tikz) 矩形路徑上建立類似節點錨點的點(是否有比所描述的方法更好的方法)?,並使用它,以便將坐標錨點添加到矩形中 - 除其他外,嘗試fit
矩形的錨點......突然它起作用了?我看不出這會如何中斷座標轉換,就像我對上述問題的理論一樣?
儘管如此,現在的輸出是:
……雖然不幸的是,我將無法僅更改樣式fitting node
並繼續(我必須在各處添加對矩形和相應擬合節點的引用)——代碼的工作方式也與 TikzEdt GUI 中的工作方式相同,這樣很好。代碼是:
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{3d}
\makeatletter
%https://tex.stackexchange.com/a/47797/2595
\tikzset{add reference/.style={insert path={%
coordinate [pos=0,xshift=-0.5\pgflinewidth,yshift=-0.5\pgflinewidth] (#1 south west)
coordinate [pos=1,xshift=0.5\pgflinewidth,yshift=0.5\pgflinewidth] (#1 north east)
coordinate [pos=.5] (#1 center)
(#1 south west |- #1 north east) coordinate (#1 north west)
(#1 center |- #1 north east) coordinate (#1 north)
(#1 center |- #1 south west) coordinate (#1 south)
(#1 south west -| #1 north east) coordinate (#1 south east)
(#1 center -| #1 south west) coordinate (#1 west)
(#1 center -| #1 north east) coordinate (#1 east)
}}}
\tikzset{
fitting node/.style={
inner sep=0pt,
fill=none,
draw=red,
fit={(#1 south west) (#1 north east)},
},
}
\makeatother
\tikzstyle{dr} = [draw, rectangle, line width=1pt, font=\Huge, align=left]
\tikzstyle{cl} = [>=latex,->,line width=1pt]
\begin{tikzpicture}[
x={(0.5cm,0.5cm)}, y={(1cm,0cm)}, z={(0cm,1cm)},
every node/.append style={transform shape},
]
\draw[very thick] (0,0) rectangle (18,8) [add reference=R1] node[fitting node=R1] (starter) {};
\begin{scope}[
shift={(1,1)},
canvas is yx plane at z=0.25,
]
\node[dr] (NodeA) at (1,5) {Node A};
\draw[dr] (4.5,2.5) rectangle (11,6.5) [add reference=R2] node [fitting node=R2] (NodeB) {Node B\\Slightly bigger};
\node[dr] (NodeC) [above=10pt of NodeB.south, anchor=south] {Node C};
\draw[cl] (NodeA) -- (NodeB);
\draw[cl] (NodeA) |- ($(NodeB.north west)-(10pt,10pt)$) -- ($(NodeB.south west)-(10pt,7.5pt)$) -| ($(NodeC.south west)+(10pt,0)$);
\end{scope}
\end{tikzpicture}