这是我的第一张 TikZ 图,所以请原谅我这个初学者对一些可能非常基础的东西的无知。我试图得到一个接近以下的图:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
column 1/.style={anchor=east,align=right,color=purple},
column 2/.style={anchor=center,text centered,align=center},
column 3/.style={anchor=center,text centered,align=center},
minimum width=2cm,minimum height=2cm] (a) {
row1 & \node[draw]{text1}; & \node[draw]{text2}; \\
row2 & \node[draw]{double\\text3}; & \node[draw]{text4}; \\
{} & \node[anchor=south]{col1}; & \node[anchor=south]{col2}; \\
};
\node at (1,2.5) [fill=white!100] {\emph{is}};
\node at (1,0.5) [fill=white!100] {\emph{is not}};
\end{tikzpicture}
\end{document}
这给了我:
这几乎就是我想要的。但是,我试图让列描述更接近上面节点的边界,但搞不清楚。我从 pgf 手册中知道,为了将其向上移动,我必须使用“南”锚点,但出于某种原因它不会移动。
第二件事(澄清):
节点“text1”和节点“text2”之间的垂直边框不应绘制,并且在节点“text3”和节点“text4”之间的垂直边框下方的行中应为虚线。
任何帮助都非常感谢!
答案1
在您澄清之后,我建议这样做:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[% define styles for row labels and col labels here
row style/.style={color=purple},%
col style/.style={},%
]
\matrix[matrix of nodes,%
column 1/.style={anchor=center,text centered,align=center},%
column 2/.style={anchor=center,text centered,align=center},%
minimum width=2cm,minimum height=2cm] (m) {%
text1 & text2 \\
text3 & text4 \\
};
% column labels
\node[col style,below=2mm of m-2-1] {col1};
\node[col style,below=2mm of m-2-2] {col2};
% row labels
\node[row style,left=2mm of m-1-1] {row1};
\node[row style,left=2mm of m-2-1] {row2};
% border around matrix
\draw (m-1-1.north west) rectangle (m-2-2.south east);
% vertical dotted line
\draw[dotted] (m-2-1.north east) -- (m-2-1.south east);
% horizontal line
\draw (m-1-1.south west) -- (m-1-2.south east);
% place label between text1 and text2
\node at ($(m-1-1)!.5!(m-1-2)$) [fill=white!100] {\emph{is}};
% place label between text3 and text4
\node at ($(m-2-1)!.5!(m-2-2)$) [fill=white!100] {\emph{is not}};
\end{tikzpicture}
\end{document}
答案2
使这两个节点向上移动的简单方法是使用例如来减少行间距\\[-10pt]
。请参阅下面的代码,其中有一些注释。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
column 1/.style={anchor=east,align=right,color=purple,minimum width=1cm}, % added minimum width=1cm here
column 2/.style={anchor=center,text centered,align=center},
column 3/.style={anchor=center,text centered,align=center},
minimum width=2.5cm, % increased this to 2.5cm
minimum height=2cm] (a) {
row1 & text1 & text2 \\ % node [-8pt] added
row2 & |[text width=15mm]| {double\\text3} & text4 \\[-15pt] % note [-15pt] added
{} & col1 & col2 \\
};
\draw (a-2-2.south west) rectangle (a-1-3.north east); % draw border around cells
\draw (a-1-2.south west) -- (a-1-3.south east); % line between rows
\draw [dashed] (a-2-2.north east) -- (a-2-2.south east); % dashed vertical line
% place is,is not nodes midway between the text nodes
\node [fill=white!100] at (a-1-2.east) {\emph{is}};
\node [fill=white!100,align=center,font=\em] at (a-2-2.east) {is\\not};
\end{tikzpicture}
\end{document}