![通过 tikz 排列三角形](https://linux22.com/image/388511/%E9%80%9A%E8%BF%87%20tikz%20%E6%8E%92%E5%88%97%E4%B8%89%E8%A7%92%E5%BD%A2.png)
任务:
我正在尝试通过 TikZ 排列四个三角形。
正如你所看到的,我面对两个问题我的解决方案是:
A) 由于边框彼此不太完美贴合,因此它看起来像是手工制作的。
B) 它不可扩展,因为节点(手动)固定到某个三角形节点。
MWE 和我的尝试:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\begin{document}
\begin{tikzpicture}
[border rotated/.style = {shape border rotate=180}],
\tikzstyle{triangle}=[draw, shape=regular polygon, regular polygon sides=3,draw,thick,inner sep=0pt,minimum
size=8cm],
\node [triangle][fill=gray!15](1) at (0,0) {Content};
\node [triangle][fill=gray!15] (2) [below left=2.5cm and 0cm of 1] {Content A};
\node [triangle] [fill=blue!15] (3) [below right=2.5cm and 0cm of 1] {Content B};
\node [triangle] [fill=gray!15] [border rotated, below=0cm of 1, align=center] {Content C};
\node (11) [above=0.3cm of 1] {Content D};
\node (21) [below left=0.3cm and 0.3cm of 2] {Content E};
\node (31) [below right=0.3cm and 0.3cm of 3] {Content F};
\end{tikzpicture}
\end{document}
请告诉我如何改善这两个问题!提前致谢。
答案1
对于这种“精确定位”,使用锚点有时是有优势的。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\begin{document}
\begin{tikzpicture}[border rotated/.style = {shape border rotate=180},
triangle/.style={draw, shape=regular polygon, regular polygon sides=3,draw,thick,inner sep=0pt,minimum
size=8cm,inner sep=0pt,outer sep=0pt}]
\node [triangle,fill=gray!15](1) {Content};
\node [triangle,fill=gray!15,anchor=north] (2) at (1.210) {Content A};
\node [triangle,fill=blue!15,anchor=north] (3) at (1.-30) {Content B};
\node [triangle,fill=gray!15,border rotated,below=0pt of 1] (4) {Content C};
\node (11) [above=0.3cm of 1] {Content D};
\node (21) [below left=0.3cm and 0.3cm of 2] {Content E};
\node (31) [below right=0.3cm and 0.3cm of 3] {Content F};
\end{tikzpicture}
\end{document}
请注意,您的图形有点宽(我没有更改任何内容)并且\tikzstyle
已被弃用(所以我使用了与您已经拥有的相同的语法border rotated
)。
您可以减小最小宽度,直到低于某个比例,此时您会看到Content
宽度小于的影响Content A
,即不再达到所有三角形的最小宽度。所以minimum size=5cm
有效
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\begin{document}
\begin{tikzpicture}[border rotated/.style = {shape border rotate=180},
triangle/.style={draw, shape=regular polygon, regular polygon sides=3,draw,thick,inner sep=0pt,minimum
size=5cm,inner sep=0pt,outer sep=0pt}]
\node [triangle,fill=gray!15](1) {Content};
\node [triangle,fill=gray!15,anchor=north] (2) at (1.210) {Content A};
\node [triangle,fill=blue!15,anchor=north] (3) at (1.-30) {Content B};
\node [triangle,fill=gray!15,border rotated,below=0pt of 1] (4) {Content C};
\node (11) [above=0.3cm of 1] {Content D};
\node (21) [below left=0.3cm and 0.3cm of 2] {Content E};
\node (31) [below right=0.3cm and 0.3cm of 3] {Content F};
\end{tikzpicture}
\end{document}
但minimum size=4cm
没有
因为这minimum size
不是实际大小。
你可能通过使用负距离来降低inner sep
,
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\begin{document}
\begin{tikzpicture}[border rotated/.style = {shape border rotate=180},
triangle/.style={draw, shape=regular polygon, regular polygon sides=3,draw,thick,inner sep=0pt,minimum
size=3.5cm,inner sep=-1.2cm,outer sep=0pt}]
\node [triangle,fill=gray!15](1) {Content};
\node [triangle,fill=gray!15,anchor=north] (2) at (1.210) {Content A};
\node [triangle,fill=blue!15,anchor=north] (3) at (1.-30) {Content B};
\node [triangle,fill=gray!15,border rotated,below=0pt of 1] (4) {Content C};
\node (11) [above=0.3cm of 1] {Content D};
\node (21) [below left=0.3cm and 0.3cm of 2] {Content E};
\node (31) [below right=0.3cm and 0.3cm of 3] {Content F};
\end{tikzpicture}
\end{document}
但如果你去2.5cm
而不是3.5cm
,文本就放不下了
然后,您仍然可以扩展所有内容以达到2.5cm
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\begin{document}
\begin{tikzpicture}[border rotated/.style = {shape border rotate=180},
triangle/.style={draw, shape=regular polygon, regular polygon sides=3,draw,thick,inner sep=0pt,minimum
size=3.5cm,inner sep=-1.2cm,outer sep=0pt},scale=5/7,transform shape]
\node [triangle,fill=gray!15](1) {Content};
\node [triangle,fill=gray!15,anchor=north] (2) at (1.210) {Content A};
\node [triangle,fill=blue!15,anchor=north] (3) at (1.-30) {Content B};
\node [triangle,fill=gray!15,border rotated,below=0pt of 1] (4) {Content C};
\node (11) [above=0.3cm of 1] {Content D};
\node (21) [below left=0.3cm and 0.3cm of 2] {Content E};
\node (31) [below right=0.3cm and 0.3cm of 3] {Content F};
\end{tikzpicture}
\end{document}