Skip to content

Commit

Permalink
Configurable matrix inner styles pgf-tikz#867
Browse files Browse the repository at this point in the history
  • Loading branch information
hmenke committed May 28, 2020
1 parent 68bebd7 commit 65bcaaf
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 14 deletions.
68 changes: 68 additions & 0 deletions doc/generic/pgf/text-en/pgfmanual-en-tikz-matrices.tex
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,74 @@ \subsubsection{Cell Styles and Options}
\end{tikzpicture}
\end{codeexample}

The order in which these styles are applied is configurable. You can also
install your own styles. The following styles (in fact, internally they are
|/.code| keys) wrap the styles introduced in the previous paragraph passing the
correct argument and ensuring that they are only called for even or odd rows.
It's not recommended to override these.

\begin{stylekey}{/tikz/matrix/inner style/every cell}
Wraps |/tikz/every cell|.
\end{stylekey}
\begin{stylekey}{/tikz/matrix/inner style/column}
Wraps |/tikz/column |\meta{number}.
\end{stylekey}
\begin{stylekey}{/tikz/matrix/inner style/even odd column}
Wraps |/tikz/every even column| and |/tikz/every odd column|.
\end{stylekey}
\begin{stylekey}{/tikz/matrix/inner style/row}
Wraps |/tikz/row |\meta{number}.
\end{stylekey}
\begin{stylekey}{/tikz/matrix/inner style/even odd row}
Wraps |/tikz/every even row| and |/tikz/every odd row|.
\end{stylekey}
\begin{stylekey}{/tikz/matrix/inner style/this cell}
Wraps |/tikz/row |\meta{number}| column |\meta{number}.
\end{stylekey}

\begin{stylekey}{/tikz/matrix/inner style order}
The order in which these styles are applied to the matrix cells is specified
by this key. By default it is
%
\begin{codeexample}[code only]
\tikzset{
matrix/inner style order={
every cell,
column,
even odd column,
row,
even odd row,
this cell
}
}
\end{codeexample}
%
You can use this to install your own styles here, but only \emph{names} of
styles are permitted here. The style specification has to be placed
outside of |matrix/inner style order| and unless it is installed inside
|/tikz/matrix/inner style/|, it has to be fully qualified.
%
\begin{codeexample}[code only]
\tikzset{
my style/.code={%
\ifnum\pgfmatrixcurrentcolumn=2
\tikzset{font=\itshape}%
\fi
},
matrix/inner style order={
every cell,
even odd column,
even odd row,
column,
row,
this cell,
/tikz/my style
}
}
\end{codeexample}
%
\end{stylekey}

In many matrices all cell pictures have nearly the same code. For example,
cells typically start with |\node{| and end |};|. The following options allow
you to execute such code in all cells:
Expand Down
56 changes: 42 additions & 14 deletions tex/generic/pgf/frontendlayer/tikz/tikz.code.tex
Original file line number Diff line number Diff line change
Expand Up @@ -3999,6 +3999,44 @@
{\pgfutil@ifnextchar e{\tikz@fig@mustbenamed\tikz@fig@continue}
{\ifx\tikz@after@path\pgfutil@empty\expandafter\tikz@fig@continue\else\expandafter\tikz@fig@mustbenamed\expandafter\tikz@fig@continue\fi}}}}}%}%
\tikzset{
matrix/inner style/every cell/.code={%
\tikzset{every cell/.try={\the\pgfmatrixcurrentrow}{\the\pgfmatrixcurrentcolumn}}%
},
matrix/inner style/column/.code={%
\tikzset{column \the\pgfmatrixcurrentcolumn/.try}%
},
matrix/inner style/even odd column/.code={
\ifodd\pgfmatrixcurrentcolumn%
\tikzset{every odd column/.try}%
\else%
\tikzset{every even column/.try}%
\fi
},
matrix/inner style/row/.code={%
\tikzset{row \the\pgfmatrixcurrentrow/.try}%
},
matrix/inner style/even odd row/.code={%
\ifodd\pgfmatrixcurrentrow%
\tikzset{every odd row/.try}%
\else%
\tikzset{every even row/.try}%
\fi
},
matrix/inner style/this cell/.code={%
\tikzset{row \the\pgfmatrixcurrentrow\space column \the\pgfmatrixcurrentcolumn/.try}%
},
matrix/inner style order/.store in=\tikz@common@matrix@code@styleorder,
matrix/inner style order={
every cell,
column,
even odd column,
row,
even odd row,
this cell
},
}%
\def\tikz@do@matrix{%
\tikzset{every matrix/.try}%
\tikz@node@transformations%
Expand Down Expand Up @@ -4050,20 +4088,10 @@
\def\tikz@common@matrix@code{%
\let\tikz@options=\pgfutil@empty%
\let\tikz@mode=\pgfutil@empty%
\tikzset{every cell/.try={\the\pgfmatrixcurrentrow}{\the\pgfmatrixcurrentcolumn}}%
\tikzset{column \the\pgfmatrixcurrentcolumn/.try}%
\ifodd\pgfmatrixcurrentcolumn%
\tikzset{every odd column/.try}%
\else%
\tikzset{every even column/.try}%
\fi%
\tikzset{row \the\pgfmatrixcurrentrow/.try}%
\ifodd\pgfmatrixcurrentrow%
\tikzset{every odd row/.try}%
\else%
\tikzset{every even row/.try}%
\fi%
\tikzset{row \the\pgfmatrixcurrentrow\space column \the\pgfmatrixcurrentcolumn/.try}%
\pgfutil@for\pgf@temp:=\tikz@common@matrix@code@styleorder\do{%
\expandafter\def\expandafter\pgf@temp\expandafter{\romannumeral-`0\expandafter\pgfutil@trimspaces\expandafter{\pgf@temp}}%
\pgfutil@ifempty\pgf@temp{}{\expandafter\tikzset\expandafter{matrix/inner style/.cd,\pgf@temp/.try}}%
}%
\tikz@options%
}%
\def\pgfmatrixendcode{%
Expand Down
12 changes: 12 additions & 0 deletions tex/generic/pgf/utilities/pgfutil-common.tex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
\def\pgfutil@trimspaces@@#1Q#2{#1}
\catcode`\Q=11

% \pgfutil@ifempty{<cs>}{<true code>}{<false code>}
%
% Check if <cs> is equal to \pgfutil@empty
%
% This macro is expandable.
\def\pgfutil@ifempty#1{%
\ifx#1\pgfutil@empty
\expandafter\pgfutil@firstoftwo
\else
\expandafter\pgfutil@secondoftwo
\fi}

% \pgfutil@ifundefined{<macro name with backslash>}
% {<is undefined code>}{<is defined code>}
%
Expand Down

0 comments on commit 65bcaaf

Please sign in to comment.