简单的解决方案

简单的解决方案

我正在尝试排列几个等腰三角形,但无法让它们全部在顶部(或底部)正确对齐。以下是 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}

结果是这样的: 结果:4 幅图像未正确对齐

是的,如果删除 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 widthalign选项。

\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,这使得包含文本的框在所有情况下都有相同的宽度,从而产生相同的结果。

在下面的代码中我还做了一些其他更改:

代码输出

\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} 

在此处输入图片描述

相关内容