tikz 矩阵中的垂直对齐

tikz 矩阵中的垂直对齐

有代码:

\documentclass[a4paper,12pt]{report}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{center}
\begin{tikzpicture}

\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={draw,
      text height=#1/2+0.75ex,
      text depth=#1/2-0.75ex,
      text width=#1,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1.4cm
}

\matrix[square matrix]
{
|[fill=lightgray]|+ & |[fill=lightgray]|0 & |[fill=lightgray]|1 & |[fill=lightgray]|$\ldots$ &  |[fill=lightgray]|$\alpha^{2^m-3}$ & |[fill=lightgray]|$2^m - 1$\\
|[fill=lightgray]|0 & 0 & 1 & $\ldots$ &  $2^m - 2$ & $2^m - 1$\\
|[fill=lightgray]|1 & 1 & 0 & $\ldots$ &  $2^m - 2$ + 5465 1\\
|[fill=lightgray]|$\vdots$\\
|[fill=lightgray]|$2^m - 2$\\
|[fill=lightgray]|$2^m - 1$\\
};

\end{tikzpicture}
\end{center}

\end{document}

输出为:

输出

右下角单元格中的文本不适合单元格。如何将单元格的垂直对齐设置为居中,从而使此文本稍微向上移动并适合单元格?

答案1

你应该使用

minimum height=#1,
anchor=center

代替

text height=#1/2+0.75ex,
text depth=#1/2-0.75ex

这将确保文本垂直居中。

\documentclass[a4paper,12pt]{report}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{center}
\begin{tikzpicture}

\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={draw,
      minimum height=#1,
      anchor=center,
      text width=#1,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1.45cm
}

\matrix[square matrix]
{
|[fill=lightgray]|+ & |[fill=lightgray]|0 & |[fill=lightgray]|1 & |[fill=lightgray]|$\ldots$ &  |[fill=lightgray]|$\alpha^{2^m-3}$ & |[fill=lightgray]|$2^m - 1$\\
|[fill=lightgray]|0 & 0 & 1 & $\ldots$ &  $2^m - 2$ & $2^m - 1$\\
|[fill=lightgray]|1 & 1 & 0 & $\ldots$ &  $2^m - 2$ + 5465 1\\
|[fill=lightgray]|$\vdots$\\
|[fill=lightgray]|$2^m - 2$\\
|[fill=lightgray]|$2^m - 1$\\
};

\end{tikzpicture}
\end{center}

\end{document}

更复杂的方形 tikz 矩阵

相关内容