我想要将一个节点 A(参见center
MWE 中的第一个环境)或多个节点 A+AA(参见center
MWE 中的第二个环境)与另一组节点(BF)对齐,以使它们彼此居中。
我怎样才能做到这一点?
笔记:
目前我仍然使用通过在左侧锚定并在 x 方向添加手动移位的方法,这会给我带来一些满意的结果,但看起来并不好。
在第一个
center
环境中,人们可以快速决定对齐 A 和 D,但这是一个有点糟糕的例子,因为节点的宽度通常不一样。
图片
平均能量损失
\documentclass[
12pt,
a4paper
]{scrartcl}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{
calc,
intersections,
positioning
}
\tikzset{
framedAAA/.style={
rectangle,
draw=black,
very thick
},
}
\listfiles
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily]
\node[framedAAA] (nodeA) {A};
\node[framedAAA, below=of nodeA.west, anchor=west, xshift=-2cm] (nodeB) {b};
\node[framedAAA, right=of nodeB.east, anchor=west] (nodeC) {c};
\node[framedAAA, right=of nodeC.east, anchor=west] (nodeD) {d};
\node[framedAAA, right=of nodeD.east, anchor=west] (nodeE) {e};
\node[framedAAA, right=of nodeE.east, anchor=west] (nodeF) {F};
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}[font=\sffamily]
\node[framedAAA] (nodeA) {A};
\node[framedAAA, right=of nodeA] (nodeA) {AA};
\node[framedAAA, below=of nodeA.west, anchor=west, xshift=-2cm] (nodeB) {b};
\node[framedAAA, right=of nodeB.east, anchor=west] (nodeC) {c};
\node[framedAAA, right=of nodeC.east, anchor=west] (nodeD) {d};
\node[framedAAA, right=of nodeD.east, anchor=west] (nodeE) {e};
\node[framedAAA, right=of nodeE.east, anchor=west] (nodeF) {F};
\end{tikzpicture}
\end{center}
\end{document}
答案1
您必须使用matrix
节点,并尝试使用全局声明以every node/.style=framedAAA
避免在每个节点中重复。使用 添加matrix
库\usetikzlibrary{matrix}
。这简化了代码。这是我的代码
\documentclass[12pt,a4paper]{scrartcl}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,positioning,matrix}
\tikzset{
framedAAA/.style={
rectangle,
draw=black,
very thick,
},
}
\listfiles
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily]
\matrix[matrix of nodes,column sep=1cm,every node/.style=framedAAA,name=nodeBF]
{
B & C & D & E & F\\
};
\node[framedAAA,above=5mm of nodeBF] (nodeA) {A};
\end{tikzpicture}
\end{center}
% Here my second alignment (both nodes of `matrix` type)
\begin{center}
\begin{tikzpicture}[font=\sffamily]
\matrix[matrix of nodes,column sep=1cm,every node/.style=framedAAA,name=nodeBF]
{
B & C & D & E & F\\
};
\matrix[matrix of nodes,column sep=1cm,every node/.style=framedAAA,name=nodeAAA,above=5mm of nodeBF]
{
A & AA\\
};
\end{tikzpicture}
\end{center}
\end{document}
结果如下
答案2
一种方法是将节点放入B - F
中scope
,并使用local bounding box
此范围的。
第一个例子只有一个节点,A
可以通过以下方式轻松解决:
\begin{tikzpicture}[font=\sffamily]
% We put the nodes in a scope, with bounding box `scope1`
\begin{scope}[local bounding box=scope1]
\node[framedAAA] (nodeB) {b};
\node[framedAAA, right=of nodeB.east, anchor=west] (nodeC) {c};
\node[framedAAA, right=of nodeC.east, anchor=west] (nodeD) {d};
\node[framedAAA, right=of nodeD.east, anchor=west] (nodeE) {e};
\node[framedAAA, right=of nodeE.east, anchor=west] (nodeF) {F};
\end{scope}
% Simply put the node above `scope1`
\node[framedAAA, above=of scope1.north] (nodeA) {A};
\end{tikzpicture}
当我们想要对齐两组多个节点时,我们可以使用幽灵作用域来帮助计算第二组节点的大小A-AA
,然后适当移动真实节点:
\begin{tikzpicture}[font=\sffamily]
\begin{scope}[local bounding box=scope1]
\node[framedAAA] (nodeB) {b};
\node[framedAAA, right=of nodeB.east, anchor=west] (nodeC) {c};
\node[framedAAA, right=of nodeC.east, anchor=west] (nodeD) {d};
\node[framedAAA, right=of nodeD.east, anchor=west] (nodeE) {e};
\node[framedAAA, right=of nodeE.east, anchor=west] (nodeF) {F};
\end{scope}
% Ghost scope above the nodes B-F
\begin{scope}[local bounding box=ghost, shift={($(scope1.north)+(0,1cm)$)}]
\node[white] (ghostA) {A};
\node[white, right=of ghostA] (ghostAA) {AA};
\end{scope}
% This scope is shifted to the left, so that it is centered
\begin{scope}[local bounding box=scope2, shift={($2*(ghostA)-(ghost)$)}]
\node[framedAAA] (nodeA) {A};
\node[framedAAA, right=of nodeA] (nodeA) {AA};
\end{scope}
\end{tikzpicture}
解释一下公式2*(ghostA)-(ghost)
,它相当于(ghostA) - ((ghost)-(ghostA))
,这意味着节点nodeA
将从 偏移 ,偏移ghostA
距离为ghostA
和 幽灵范围的中心。这会将 的中心置于scope2
,ghostA
与 的中心对齐scope1
。
如您所见,当有多个节点时,我的解决方案A-AA
非常复杂。我对更简单的解决方案感兴趣。