
다음 행렬을 고려하십시오.
이 매트릭스는 코드로 생성되었습니다
\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부터 결과를 경로 밖으로 밀수할 수 있는 방법이 있습니다. 그런 다음 적절한 팝을 사용한 후 검색할 수 있습니다.
\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
nicematrix
엄밀히 말하면 이것은 질문에 대한 대답은 아니지만 아마도 일부 사람들은 이 기능이 (매트릭스 셀 아래에 PGF/Tikz 노드를 생성하는) 환경에서 직접 사용할 수 있다는 사실에 관심을 가질 것입니다 .
\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\begin{document}
$\begin{pNiceMatrix}
* & * & * & * \\
* & * & * & * \\
* & * \\
\CodeAfter
\tikz \node at (1-last) {$\bigcirc$};
\end{pNiceMatrix}$
\end{document}