我可以垂直对齐这些多线节点吗?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
fit,
positioning,
matrix,
}
\begin{document}
\begin{tikzpicture}
\tikzset{
status/.style={rectangle, draw=black, text centered, text=black, minimum width=1.5em, minimum height=1.5em, font=\footnotesize\bfseries},
m2style/.style={column sep=0.25em, row sep=0.1em, nodes={font=\footnotesize, inner sep=0pt},
row 1 column 1/.style={nodes={fill=green}},
row 2 column 1/.style={nodes={fill=red}},
row 3 column 1/.style={nodes={fill=yellow}},
row 4 column 1/.style={nodes={fill=gray}},
column 1/.append style={nodes={status}},
column 2/.append style={nodes={text width=3.5em}},
},
}
\matrix(m2)[matrix of nodes,ampersand replacement=\&, m2style]{
H \& Healthy \\
A1 \& Infected (strong) \\
A2 \& Infected (weak) \\
D \& Death \\
};
\node[above=-0.5ex of m2, font=\bfseries\footnotesize, text width=5em] (title) {Cell Status};
\node[draw=black, inner sep=0in, fit=(title) (m2)] {};
\end{tikzpicture}
\end{document}
期望的结果:
答案1
尝试添加anchor=center
到节点样式,即nodes={}
在内m2style
。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{
fit,
positioning,
matrix,
}
\begin{document}
\begin{tikzpicture}
\tikzset{
status/.style={rectangle, draw=black, text centered, text=black, minimum width=1.5em, minimum height=1.5em, font=\footnotesize\bfseries},
m2style/.style={column sep=0.25em, row sep=0.1em, nodes={font=\footnotesize, inner sep=0pt},
row 1 column 1/.style={nodes={fill=green}},
row 2 column 1/.style={nodes={fill=red}},
row 3 column 1/.style={nodes={fill=yellow}},
row 4 column 1/.style={nodes={fill=gray}},
column 1/.append style={nodes={status,anchor=center}},
column 2/.append style={nodes={text width=3.5em,anchor=center}},
},
}
\matrix(m2)[matrix of nodes,ampersand replacement=\&, m2style]{
H \& Healthy \\
A1 \& Infected (strong) \\
A2 \& Infected (weak) \\
D \& Death \\
};
\node[above=-0.5ex of m2, font=\bfseries\footnotesize, text width=5em] (title) {Cell Status};
\node[draw=black, inner sep=0in, fit=(title) (m2)] {};
\end{tikzpicture}
\end{document}