我正在尝试使用 TikZ 绘制 SWOT 矩阵。显然,我在 MWE 中介绍的程序并未优化。此外,文本(即左侧的文本)未正确对齐。
\documentclass[10pt]{standalone}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{backgrounds}
\tikzset{desc/.style={outer sep=0pt,inner sep=0pt,text centered,font=\scriptsize,fill=black!10}}
\newcommand{\texta}{Helpful\\ \tiny (to achieve the objective)}
\newcommand{\textb}{Harmful\\ \tiny (to achieve the objective)}
\newcommand{\textc}{\rotatebox[origin=r]{90}{\shortstack{Internal origin\\ \tiny (product\slash company attributes)}}}
\newcommand{\textd}{\rotatebox[origin=r]{90}{\shortstack{External origin\\ \tiny (environment\slash market attributes)}}}
\begin{document}
\begin{tikzpicture}[x=3cm,y=3cm]
\coordinate (c1) at (0,0);
\coordinate (c2) at (2,0);
\coordinate (c3) at (2,2);
\coordinate (c4) at (0,2);
\coordinate (c5) at ($(c1)!0.5!(c2)$);
\coordinate (c6) at ($(c2)!0.5!(c3)$);
\coordinate (c7) at ($(c3)!0.5!(c4)$);
\coordinate (c8) at ($(c4)!0.5!(c1)$);
\coordinate (c8) at ($(c4)!0.5!(c1)$);
\draw (c1) --+ (c2) --+ (c3) --+ (c4) -- cycle;
\path [name path=c5--c7,draw] (c5) -- (c7);
\path [name path=c6--c8,draw] (c6) -- (c8);
\coordinate [name intersections={of=c5--c7 and c6--c8,by=c9}];
\node [outer sep=0pt,inner sep=0pt,fit={(c8) (c9) (c7) (c4)}] (S){Strengths};
\node [outer sep=0pt,inner sep=0pt,fit={(c9) (c6) (c3) (c7)}] (W){Weaknesses};
\node [outer sep=0pt,inner sep=0pt,fit={(c1) (c5) (c9) (c8)}] (O){Opportunities};
\node [outer sep=0pt,inner sep=0pt,fit={(c5) (c2) (c6) (c9)}] (T){Threats};
%%%%
\coordinate[yshift=1cm] (u4) at (S.north west);
\coordinate[yshift=1cm] (u3) at (W.north east);
\coordinate[yshift=1cm] (u7) at (W.north west);
\draw (c4) -- (u4) -- (u3) -- (c3);
\draw (c7) -- (u7);
\begin{pgfonlayer}{background}
\node [desc,fit={(c4) (u4) (u7) (c7)}] {\texta};
\node [desc,fit={(c7) (u7) (u3) (c3)}] {\textb};
\end{pgfonlayer}{background}
%%%%
\coordinate[xshift=-1cm] (l4) at (S.north west);
\coordinate[xshift=-1cm] (l8) at (S.south west);
\coordinate[xshift=-1cm] (l1) at (O.south west);
\draw (c4) -- (l4) -- (l1) -- (c1);
\draw (c8) -- (l8);
\begin{pgfonlayer}{background}
\node [desc,fit={(c4) (l4) (l8) (c8)}] {\textc};
\node [desc,fit={(c8) (l8) (l1) (c1)}] {\textd};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案1
您可以使用matrix (of nodes)
。如果您确定minimum width
和minimum height
足够大,可以容纳其内容,那么它的大小就是这个。使用 这样matrix
的 ,您不需要draw
线条,因为node
边框 提供了线条。使用column(row) sep=-.5\pgflinewidth
,节点边框将一个接一个地绘制。
\documentclass[10pt,tikz]{standalone}
\usepackage{times} % for the font
\usetikzlibrary{matrix}
\newcommand{\texta}{Helpful\\[-1ex] \tiny{(to achieve the objective)}}
\newcommand{\textb}{Harmful\\[-1ex] \tiny{(to achieve the objective)}}
\newcommand{\textcn}{Internal origin\\[-1ex] \tiny{(product\slash company attributes)}}
\newcommand{\textdn}{External origin\\[-1ex] \tiny{(environment\slash market attributes)}}
\begin{document}
\begin{tikzpicture}[
any/.style={draw,minimum width=3cm,minimum height=3cm,text width=2.5cm,align=center},
header/.style={any,minimum height=1cm,fill=black!10},
leftcol/.style={header,rotate=90}
]
\matrix (SWOT) [matrix of nodes,nodes={any,anchor=center},column sep=-1\pgflinewidth,row sep=-1\pgflinewidth,inner sep=0pt]
{
&|[header]| {\texta} & |[header]| {\textb} \\
|[leftcol]| {\textcn} & Strengths & Weakness \\
|[leftcol]| {\textdn} & Opportunities & Threats \\
};
\end{tikzpicture}
\end{document}
Qrrbrbirlbel 对以前的代码提出了几项改进建议:
1 - 可以定义特定row/column/cell
样式并避免使用语法|[stuff]|
。类似 的row 1/.style={stuff}
意思是应用于.append style=stuff
第一行中的所有节点。2 - 我错误地更正了 OP 的代码以进行更改\text..
。3 -在每个命令末尾添加。它将在标题单元格内产生不同的垂直对齐。\tiny (...)
\tiny{(...)}
\par
\text..
接下来您将得到结果代码和图形:
\documentclass[10pt,tikz]{standalone}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\texta}{Helpful\\ \tiny (to achieve the objective)\par}
\newcommand{\textb}{Harmful\\ \tiny (to achieve the objective)\par}
\newcommand{\textcn}{Internal origin\\ \tiny (product\slash company attributes)\par}
\newcommand{\textdn}{External origin\\ \tiny (environment\slash market attributes)\par}
\begin{document}
\begin{tikzpicture}[
any/.style={draw,minimum width=3cm,minimum height=3cm,%
text width=2.5cm,align=center,outer sep=0pt},
header/.style={any,minimum height=1cm,fill=black!10},
leftcol/.style={header,rotate=90}
]
\matrix (SWOT) [matrix of nodes,nodes={any,anchor=center},%
column sep=-\pgflinewidth,%
row sep=-\pgflinewidth,%
row 1/.style={nodes=header},%
column 1/.style={nodes=leftcol},
inner sep=0pt]
{
& {\texta} & {\textb} \\
{\textcn} & Strengths & Weakness \\
{\textdn} & Opportunities & Threats \\
};
\end{tikzpicture}
\end{document}