我正在尝试排列几个等腰三角形,但无法让它们全部在顶部(或底部)正确对齐。以下是 MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\tikzstyle{tri} = [draw, isosceles triangle, shape border rotate=-90,inner sep=0pt, minimum width=4em]
\begin{document}
\begin{center}
\begin{tikzpicture}[node distance=1.8cm,>=latex']
\node [tri] (gain3) {$4$};
\node [tri,left of=gain3] (gain2) {$3$};
\node [tri,left of=gain2] (gain1) {$-2$};
\node [tri,left of=gain1] (gain0) {$1$};
\end{tikzpicture}
\end{center}
\end{document}
是的,如果删除 2 前面的减号,所有内容都会正确排列。如何以优雅的方式修复此问题?(即,不将三角形节点的内容放在预定义大小的 \mbox 内等)
答案1
简单的解决方案
这是一个非常简单的解决方案(使用shape border uses incircle
选项)。但这个解决方案不够稳健(请尝试使用$-245$
而不是$-2$
)。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\tikzset{
tri/.style={
minimum width=4em,
draw,
isosceles triangle,
shape border uses incircle,
shape border rotate=-90,
inner sep=0pt,
},
}
\begin{document}
\begin{tikzpicture}[node distance=1cm,>=latex']
\node [tri] (gain3) {$4$};
\node [tri,left=of gain3] (gain2) {$3$};
\node [tri,left=of gain2] (gain1) {$-2$};
\node [tri,left=of gain1] (gain0) {$1$};
\end{tikzpicture}
\end{document}
通用解决方案
稳健解决方案不再使用选项minimum width
而是使用text width
和align
选项。
\documentclass[tikz]{standalone}
\usetikzlibrary{shapes,positioning}
\tikzset{
tri width of/.style={
draw,
isosceles triangle,
shape border uses incircle,
shape border rotate=-90,
inner sep=0pt,
align=flush center,text width={width("#1")},
},
}
\begin{document}
\begin{tikzpicture}[node distance=1cm]
\tikzset{tri2/.style={tri width of=$-2$}}
\node [tri2] (gain3) {$4$};
\node [tri2,left=of gain3] (gain2) {$3$};
\node [tri2,left=of gain2] (gain1) {$-2$};
\node [tri2,left=of gain1] (gain0) {$1$};
\end{tikzpicture}
\begin{tikzpicture}[node distance=1cm]
\tikzset{tri3/.style={tri width of=$-345$}}
\node [tri3] (gain3) {$4$};
\node [tri3,left=of gain3] (gain2) {$3$};
\node [tri3,left=of gain2] (gain1) {$-345$};
\node [tri3,left=of gain1] (gain0) {$678$ $-345$};
\end{tikzpicture}
\end{document}
答案2
可能,最快的解决方法是添加text width=2em,align=center
样式tri
,这使得包含文本的框在所有情况下都有相同的宽度,从而产生相同的结果。
在下面的代码中我还做了一些其他更改:
left of=
将弃用的语法更改left=of
为需要\usetikzlibrary{positioning}
(PGF/TikZ 中“right of=”和“right=of”之间的区别)根据手册,该
arrows
库也被视为已弃用,取而代之的是arrows.meta
。最后,
\tikzset{stylename/.style={..}}
通常建议使用\tikzstyle
,尽管我认为这主要是偏好问题(应该\tikzset
或被\tikzstyle
用来定义 TikZ 样式吗?)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
shapes.geometric,
arrows.meta, % supersedes arrows
positioning
}
\tikzset{
tri/.style={
draw,
isosceles triangle,
shape border rotate=-90,
inner sep=0pt,
text width=2em, % added
align=center, % added
minimum width=4em
}
}
\begin{document}
\begin{center}
\begin{tikzpicture}[>=Latex]
\node [tri] (gain3) {$4$};
% alternative, setting anchors explicitly (requires larger node distance)
% \node [tri,left=of gain3.north, anchor=north] (gain2) {$3$};
% \node [tri,left=of gain2.north, anchor=north] (gain1) {$-2$};
% \node [tri,left=of gain1.north, anchor=north] (gain0) {$1$};
\node [tri,left=of gain3] (gain2) {$3$};
\node [tri,left=of gain2] (gain1) {$-2$};
\node [tri,left=of gain1] (gain0) {$1$};
\end{tikzpicture}
\end{center}
\end{document}
答案3
一种可能性:使用定位库和锚点。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\tikzset{tri/.style={draw, isosceles triangle, shape border rotate=-90,inner
sep=0pt, minimum width=4em}}
\begin{document}
\begin{center}
\begin{tikzpicture}[node distance=1.8cm,>=latex']
\node [tri] (gain3) {$4$};
\node [tri,left=of gain3.north,anchor=north] (gain2) {$3$};
\node [tri,left=of gain2.north,anchor=north] (gain1) {$-2$};
\node [tri,left=of gain1.north,anchor=north] (gain0) {$1$};
\end{tikzpicture}
\end{center}
\end{document}
答案4
一个简单的 hack makebox
:
\documentclass{article}
\usepackage{eqparbox, makebox, mathtools}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\tikzstyle{tri} = [draw, isosceles triangle, shape border rotate=-90,inner sep=0pt, minimum width=4em]
\begin{document}
\begin{center}
\begin{tikzpicture}[node distance=1.8cm,>=latex']
\node [tri] (gain3) {$4$};
\node [tri,left of=gain3] (gain2) {$3$};
\node [tri,left of=gain2] (gain1) {\makebox[0.6em]{$-2$}};
\node [tri,left of=gain1] (gain0) {$1$};
\end{tikzpicture}
\end{center}
\end{document}