
次の行列を考えてみましょう
このマトリックスはコードで作成されました
\begin{tikzpicture}
\matrix[
, matrix of nodes
, left delimiter={[},right delimiter = {]}
] (m)
{
* & * & * & * \\
* & * & * & * \\
* & * & * & * \\
};
\node at (m-2-4) {\textbullet};
\end{tikzpicture}
(1, 4)の位置の点は で配置されました\node at (m-1-4) {\textbullet};
。
このマトリックスに列を追加したいのですが、ドットは最後の列に残しておきたいです。 のような構文を使用して、このマトリックスの最後の列をプログラムで参照できるかどうか知りたいです\node at (m-1-last column index) {\textbullet};
。 これは可能ですか?
答え1
pgfにはカウント\pgfmatrixcurrentrow
とがあり\pgfmatrixcurrentcolumn
、新しい行列を開始するたびにリセットされます。したがって、行列の直後にカウントを調べると、行と列の数が含まれます。それ以外の場合は、マクロに保存できます。ただし、例では、
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[
, matrix of nodes
, left delimiter={[},right delimiter = {]}
] (m)
{
* & * & * & * \\
* & * & * & * \\
* & * & * & * \\
};
\node at (m-2-\the\pgfmatrixcurrentcolumn) {\textbullet};
\end{tikzpicture}
\end{document}
列数が最後の行の最大値より小さい場合、上記の方法は失敗します。この場合のスタイルを定義できます。pgf バージョン 3.1.6 以降では、結果をパスから密かに取り出すことができるメソッドがあります。その後、適切な pop を使用して結果を取得できます。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix}
\makeatletter
\tikzset{record number of columns in/.style={execute at end matrix={%
\edef#1{\the\pgf@matrix@numberofcolumns}%
\pgfutil@pushmacro#1}},
record number of rows in/.style={execute at end matrix={%
\edef#1{\the\pgfmatrixcurrentrow}%
\pgfutil@pushmacro#1}}
}
\newcommand\pgfpop[1]{\pgfutil@popmacro#1}
\makeatother
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
left delimiter={[},right delimiter = {]},
record number of columns in=\mycols,
record number of rows in=\myrows
] (m)
{
* & * & * & * \\
* & * & * & * \\
* & * & * & * \\
};
\pgfpop\mycols
\pgfpop\myrows
\node[anchor=center] at (m-2-\mycols.center) {\textbullet};
\end{tikzpicture}
\end{document}
あるいは、新しいカウントを導入することもできます。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix}
\newcount\tikzmatrixrows
\newcount\tikzmatrixcols
\makeatletter
\tikzset{record matrix dimensions/.style={execute at end matrix={%
\global\tikzmatrixcols=\pgf@matrix@numberofcolumns
\global\tikzmatrixrows=\pgfmatrixcurrentrow}}}
\makeatother
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
left delimiter={[},right delimiter = {]},
record matrix dimensions
] (m)
{
* & * & * & * \\
* & * & * & * \\
* & * & * & * \\
};
\node[anchor=center] at (m-2-\the\tikzmatrixcols.center) {\textbullet};
\end{tikzpicture}
\end{document}
最後に、数字を明示的に知る必要はありません。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,nodes={alias=m-\the\pgfmatrixcurrentrow-last},
left delimiter={[},right delimiter = {]}
] (m)
{
* & * & * & * \\
* & * & * & * \\
* & * & * & * \\
};
\node at (m-2-last) {\textbullet};
\end{tikzpicture}
\end{document}
答え2
厳密に言えば、これは質問に対する答えではありませんが、この機能が (マトリックスのセルの下に PGF/Tikz ノードを作成する) の環境で直接利用できることに興味を持つ人もいるかもnicematrix
しれません。
\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\begin{document}
$\begin{pNiceMatrix}
* & * & * & * \\
* & * & * & * \\
* & * \\
\CodeAfter
\tikz \node at (1-last) {$\bigcirc$};
\end{pNiceMatrix}$
\end{document}