![图片定位](https://linux22.com/image/456038/%E5%9B%BE%E7%89%87%E5%AE%9A%E4%BD%8D.png)
我想画两张图片,第二张图片应该在第一张图片下面。这是我目前的情况:
第二张图片如何能获得与第一张图片相同的 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
(默认 .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}
输出: