
我想畫兩張圖,第二張圖應該在第一張圖的下面。這就是我目前所擁有的:
第二張圖片如何獲得與第一張圖片相同的 x 座標?
代碼:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\begin{document}
\tikzset{
pics/vhsplit/.style n args = {3}{
code = {
\node (A) at (0,0) {#1};
\node[anchor=south west] (B) at (A.east) {#2};
\node[anchor=north west] (C) at (A.east) {#3};
\node[inner sep=0pt, outer sep=0pt, draw, rounded corners, fit=(A)(B)(C)] (-box) {};
\draw (B.north west) -- (C.south west)
(B.south west) -- (C.north east);
}
}
}
\begin{tikzpicture}
\pic (a) {vhsplit={a}{2.0}{6.0}};
\pic[below=10mm of a-box.south] (b) {vhsplit={b}{-3.0}{-4.0}};
\end{tikzpicture}
\end{document}
答案1
圖片不像節點那麼聰明。圖片的原點始終放置在指定位置。
如果這就是您所需要的,那麼使用帶有 tabular
節點內部的普通節點可能會更容易。以下是三種方法:
- 普通文字和兩行一列的表格
→vhsplit
- 一張兩行兩列的表格,
#1
放在\multirow
→vhsplit'
- A
rectangle split
形狀從shapes.multipart
庫中再次使用表格,如 1 所示
。vhsplit''
由於表格的線條需要接觸節點的邊框,因此我將 s inner sep
(預設為 0.3333em)設為零,並再次在適當的位置插入該空格。
左側部分的垂直放置有點可變,選擇最適合您的用例的一個(解決方案按此順序著色為紅色/綠色/藍色,並用 彼此疊放opacity=.3333
):
程式碼
\documentclass[tikz,border=2mm]{standalone}
\usepackage{hhline}
\usepackage{multirow}
\usetikzlibrary{positioning}
\tikzset{
vhsplit/.style n args={3}{% text and a tabular
shape=rectangle, draw, rounded corners, inner sep=+0pt,
node contents={%
\setlength{\tabcolsep}{.3333em}%
\hspxsep#1\hspxsep
\begin{tabular}{|l@{\hspxsep}}
\vstysep$#2$\\\hhline{|-}
\vstysep$#3$%
\end{tabular}}},
vhsplit'/.style n args={3}{
shape=rectangle, draw, rounded corners, inner sep=+0pt,
node contents={% multirow in a tabular
\setlength{\tabcolsep}{.3333em}%
\begin{tabular}{l|l@{\hspxsep}}
\multirow{2}{*}{#1} &
\vstysep$#2$\\\hhline{~|-}
& \vstysep$#3$%
\end{tabular}}}}
\usetikzlibrary{shapes.multipart}
\tikzset{
vhsplit''/.style n args={3}{
shape=rectangle split, rectangle split horizontal, rectangle split parts=2,
draw, rounded corners, inner sep=+0pt,
node contents={% two nodeparts and a tabular
\hspxsep#1\hspxsep\null
\nodepart{two}\setlength{\tabcolsep}{.3333em}%
\begin{tabular}{l}
\vstysep$#2$\\\hline
\vstysep$#3$
\end{tabular}}}}
\newcommand*\hspxsep{\hspace{.3333em}}
\newcommand*\vstysep{\rule{0pt}{1.0333em}}
\begin{document}
\begin{tikzpicture}
\node (a) [vhsplit={a}{ 2.0} {6.0}];
\node[below=of a] (b) [vhsplit={b}{-3.0}{-4.0}];
\tikzset{xshift=2cm}
\node (a) (a) [vhsplit'={a} {2.0} {6.0}];
\node[below=of a] (b) [vhsplit'={b}{-3.0}{-4.0}];
\tikzset{xshift=2cm}
\node (a) [vhsplit''={a} {2.0} {6.0}];
\node[below=of a] (b) [vhsplit''={b}{-3.0}{-4.0}];
\end{tikzpicture}
\end{document}
輸出
答案2
嘗試這個:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\begin{document}
\tikzset{
pics/vhsplit/.style n args = {3}{
code = {
\node (A) at (0,0) {#1};
\node[anchor=south west] (B) at (A.east) {#2};
\node[anchor=north west] (C) at (A.east) {#3};
\node[inner sep=0pt, outer sep=0pt, draw, rounded corners, fit=(A)(B)(C)] (-box) {};
\draw (B.north west) -- (C.south west)
(B.south west) -- (C.north east);
}
}
}
\begin{tikzpicture}{scale=3} % <-- changed
\pic (a) {vhsplit={a}{2.0}{6.0}};
\pic[xshift=0cm,yshift=-1cm] (b) {vhsplit={b}{-3.0}{-4.0}};
\end{tikzpicture}
\end{document}
輸出: