数学符号破坏对齐

数学符号破坏对齐

当我输入此代码时(编辑版本https://tex.stackexchange.com/a/236098/272441):

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix,backgrounds}

\begin{document}
\begin{tikzpicture}[font=\ttfamily,
array/.style={matrix of nodes,nodes={draw, minimum size=10mm, fill=green!30},column sep=-1.5\pgflinewidth, row sep=0.5mm, nodes in empty cells,
row 1/.style={nodes={draw=none, fill=none, minimum size=10mm}},
row 1 column 1/.style={nodes={draw}}}]

\matrix[array] (array) {
$0$ & $1$ & $2$ & $3$ & $4$ & $\cdots$ & $i$ & ... & $n-1$\\
7 & 1 & 9 & 3 & 4 & ... &   &  &  \\};
\node[draw, fill=gray, minimum size=4mm] at (array-2-7) (box) {$\star$};

\begin{scope}[on background layer]
\fill[green!10] (array-1-1.north west) rectangle (array-1-9.south east);
\end{scope}

\draw[<->]([yshift=-3mm]array-2-1.south west) -- node[below] {Array length is $n$} ([yshift=-3mm]array-2-9.south east);

\draw (array-1-1.north)--++(90:3mm) node [above] (first) {First index};
\draw (array-1-9.east)--++(0:3mm) node [right]{Indices};
\node [align=center, anchor=south] at (array-2-9.north west|-first.south) (8) {Element\\ (at index $i$)};
\draw (8)--(box);
\end{tikzpicture}
\end{document}

对齐被破坏了。如果你知道如何“修复”它,那就太棒了。

在此处输入图片描述

答案1

显然,这nodes in empty cells会破坏对齐。

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{matrix,backgrounds}

\begin{document}

\begin{tikzpicture}[
  font=\ttfamily,
  array/.style={
    matrix of nodes,
    nodes={draw, minimum size=10mm, fill=green!30},
    column sep=-1.5\pgflinewidth,
    row sep=0.5mm,
    row 1/.style={nodes={draw=none, fill=none, minimum size=10mm}},
    row 1 column 1/.style={nodes={draw}},
  },
]
\matrix[array] (array) {
  $0$ & $1$ & $2$ & $3$ & $4$ & $\cdots$ & $i$ & $\cdots$ & $n-1$\\
  7 & 1 & 9 & 3 & 4 & \vphantom{1}\smash{$\cdots$} & \vphantom{1} & \vphantom{1} & \vphantom{1} \\};
\node[draw, fill=gray, minimum size=4mm] at (array-2-7) (box) {$\star$};

\begin{scope}[on background layer]
\fill[green!10] (array-1-1.north west) rectangle (array-1-9.south east);
\end{scope}

\draw[<->]([yshift=-3mm]array-2-1.south west) -- node[below] {Array length is $n$}
  ([yshift=-3mm]array-2-9.south east);

\draw (array-1-1.north)--++(90:3mm) node [above] (first) {First index};
\draw (array-1-9.east)--++(0:3mm) node [right]{Indices};
\node [align=center, anchor=south] at (array-2-9.north west|-first.south)
  (8) {Element\\ (at index $i$)};
\draw (8)--(box);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容