将标题放在矩阵上方

将标题放在矩阵上方

我有以下代码,它绘制一个标题(a matrix:)和一个matrix

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \node at (0,2) {\Large a matrix:};
  \matrix[
  matrix of math nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center, minimum width=2cm, cell},
  column 1/.style = {nodes={minimum width=2cm}},
  column 2/.style = {nodes={minimum width=2cm}},
  row 1/.style = {nodes={text height=1.3ex, text depth=0}},
  ] 
  { 123 & 456  \\
  };
\end{tikzpicture}

该代码的一个问题是,如果我在矩阵中放入更多行,标题的坐标(0,2)将不再合适。

那么,无论矩阵的高度和宽度是多少,是否可以将标题放在灵活的位置?我只想把它放在矩阵的中间和上方。

答案1

给矩阵添加标签并参考它绘制节点,如下所示

\node[font=\Large,anchor=south] at (m.north) {a matrix:};

梅威瑟:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
  \begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of math nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center, minimum width=2cm, cell},
  column 1/.style = {nodes={minimum width=2cm}},
  column 2/.style = {nodes={minimum width=2cm}},
  row 1/.style = {nodes={text height=1.3ex, text depth=0}},
  ](m)
  { 123 & 456  \\
  123 & 456  \\
  123 & 456  \\
  123 & 456  \\
  123 & 456  \\
  };
  \node[font=\Large,anchor=south] at (m.north) {a matrix:};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

Amatrixnode。知道 简单label可用于向节点添加标签。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
  \begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of math nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center, minimum width=2cm, cell},
  column 1/.style = {nodes={minimum width=2cm}},
  column 2/.style = {nodes={minimum width=2cm}},
  row 1/.style = {nodes={text height=1.3ex, text depth=0}},
  label={[font=\large]above:a matrix:}   %<--------------------
  ](m)
  { 123 & 456  \\
  123 & 456  \\
  123 & 456  \\
  123 & 456  \\
  123 & 456  \\
  };
%  \node[font=\Large,anchor=south] at (m.north) {a matrix:};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容