
Considere a seguinte matriz
Esta matriz foi produzida com o código
\begin{tikzpicture}
\matrix[
, matrix of nodes
, left delimiter={[},right delimiter = {]}
] (m)
{
* & * & * & * \\
* & * & * & * \\
* & * & * & * \\
};
\node at (m-2-4) {\textbullet};
\end{tikzpicture}
O ponto na posição (1, 4) foi colocado com \node at (m-1-4) {\textbullet};
.
Talvez eu queira adicionar colunas a esta matriz, mas quero que o ponto permaneça na última coluna. Estou curioso para saber se é possível referir-se programaticamente à última coluna desta matriz com uma sintaxe como \node at (m-1-last column index) {\textbullet};
. Isso é possível?
Responder1
pgf possui as contagens \pgfmatrixcurrentrow
e \pgfmatrixcurrentcolumn
, que são zeradas sempre que você inicia uma nova matriz. Portanto, se você inspecionar as contagens logo após uma matriz, elas conterão o número de linhas e colunas. Caso contrário, você poderá armazená-los em macros. No entanto, no seu exemplo, você precisa apenas
\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}
Se o número de colunas for menor que o máximo na última linha, o método acima falhará. Você pode definir estilos para este caso. A partir da versão 3.1.6 do pgf existe um método que permite contrabandear os resultados para fora do caminho. Você pode recuperá-los depois de usar algum pop apropriado.
\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}
Alternativamente, você pode introduzir novas contagens.
\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}
Finalmente, você não precisa saber o número explicitamente.
\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}
Responder2
A rigor, isso não é uma resposta à pergunta, mas talvez algumas pessoas estejam interessadas em ver que esse recurso está diretamente disponível nos ambientes de nicematrix
(que criam nós PGF/Tikz sob as células da matriz).
\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\begin{document}
$\begin{pNiceMatrix}
* & * & * & * \\
* & * & * & * \\
* & * \\
\CodeAfter
\tikz \node at (1-last) {$\bigcirc$};
\end{pNiceMatrix}$
\end{document}