
我正在嘗試解決如何正確旋轉 Tikz 節點的組合,而不旋轉其標籤文字?;我發現tikz pgf - 「在指令後面附加」和「插入路徑」的問題。
這給了我以下想法- 定義一種樣式,這樣它只需將另一個節點放置在“當前”節點之上,並帶有彩色背景,以便新節點遮蓋舊節點,並且具有與“當前”節點相同的內容,但是旋轉了。這就是我走了多遠:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
% https://tex.stackexchange.com/questions/55722/problem-with-append-after-command-and-insert-path
\makeatletter
\def\mymacro{
\begin{scope}
% { % MUST have this group! or scope!
\setbox\tikz@figbox=\box\pgfutil@voidb@x
\node at (\tikzlastnode) [fill=yellow] {\rotatebox{180}{YY}}; %
% }
\end{scope}
}
\makeatother
\tikzstyle{drc} = [draw, rectangle, line width=1pt, align=center,
append after command={\pgfextra{\mymacro}},
% insert path={\pgfextra{\mymacro}}, % no \tikzlastnode
]
\begin{document}
\begin{tikzpicture}[line width=3mm]
\node[drc] at (0,0) {\ \ A};
\node[drc] at (1,1) {B\ \ };
\end{tikzpicture}
\end{document}
這是輸出:
問題/疑問是:
- 附加的節點是在後面原始節點,因此它的填充不能遮蓋原始節點 - 有什麼方法可以將其放在原始節點前面嗎?
- 我只是要
YY
測試這些字母 - 但我寧願“提取”\tikzlastnode
;的文本但是,似乎\tikzlastnode
基本上只是最後一個節點的名稱,而不是「物件引用」。無論如何,有沒有辦法提取和重複使用 的文字\tikzlastnode
?
答案1
我認為沒有內建方法可以保存最後一個節點的框,因此這是一種快速而骯髒的方法(並且可能不可靠):
\documentclass[tikz, border=5]{standalone}
\newbox\lastnodebox
\begin{document}
\begin{tikzpicture}[every node/.style={draw,
execute at begin node=\global\setbox\lastnodebox\hbox\bgroup,
execute at end node=\egroup\copy\lastnodebox}]
\foreach \i [count=\y] in {A,...,F}
\node at (0,-\y/2) {\box\lastnodebox \i};
\end{tikzpicture}
\end{document}
編輯:關於 OP 範例,即使使用此方法透過 新增附加節點append after command={\pgfextra{\mymacro}}
,附加的節點仍然位於「後面」(因此不會「覆蓋」前一個節點)。然而,這種方法可以直接在樣式中使用,與包turn
中的環境一起rotating
「按時」發出旋轉,因此「附加」或「覆蓋」第二個旋轉節點是不必要的。所以我們可以使用:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usepackage{rotating} %tlmgr install rotating
\newbox\lastnodebox
\tikzstyle{drc} = [draw, rectangle, line width=1pt, align=center,]
\tikzstyle{drcr} = [drc,
execute at begin node=\begin{turn}{180}\global\setbox\lastnodebox\hbox\bgroup,
execute at end node=\egroup\copy\lastnodebox\end{turn},
]
\begin{document}
\begin{tikzpicture}[line width=3mm]
\node[drc] at (0,0) {\ \ A\\A\ \ };
\node[drcr] at (1,1) {B\ \ \\\ \ B};
\end{tikzpicture}
\end{document}
....取得: