how can I create keys such as “column … to …” for a TikZ matrix
I have a node style I would like to apply to many (but not all) the cells/columns/rows of a matrix of nodes. Currently, for every column or cell that I want the style to apply to, I declare the style as:
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
It would be more convenient if I could write something like
column 2 to 5/.style={column sep=-0.4pt},
row 3 column 2 to 5/.style={my node style},
row 4 column 2 to 5/.style={my node style},
or even better (for the last two)
cell 3 2 to cell 4 5/.style={my node style},
Here's a MWE where such keys would improve readability of the code
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}

Note: I'm also trying to avoid having to declare the style explicitly in each cell where I want it to apply as in rows 2 and 4 of the above example.
UPDATE
I've tried adding the following to my preamble
tikzset{%%
column thepgfmatrixcurrentcolumnspace to thepgfmatrixcurrentcolumn/.try,
}
but when I try to call it as in:
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
column 2 to 5/.style={red},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
There is no apparent effect.
tikz-styles tikz-matrix
add a comment |
I have a node style I would like to apply to many (but not all) the cells/columns/rows of a matrix of nodes. Currently, for every column or cell that I want the style to apply to, I declare the style as:
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
It would be more convenient if I could write something like
column 2 to 5/.style={column sep=-0.4pt},
row 3 column 2 to 5/.style={my node style},
row 4 column 2 to 5/.style={my node style},
or even better (for the last two)
cell 3 2 to cell 4 5/.style={my node style},
Here's a MWE where such keys would improve readability of the code
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}

Note: I'm also trying to avoid having to declare the style explicitly in each cell where I want it to apply as in rows 2 and 4 of the above example.
UPDATE
I've tried adding the following to my preamble
tikzset{%%
column thepgfmatrixcurrentcolumnspace to thepgfmatrixcurrentcolumn/.try,
}
but when I try to call it as in:
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
column 2 to 5/.style={red},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
There is no apparent effect.
tikz-styles tikz-matrix
add a comment |
I have a node style I would like to apply to many (but not all) the cells/columns/rows of a matrix of nodes. Currently, for every column or cell that I want the style to apply to, I declare the style as:
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
It would be more convenient if I could write something like
column 2 to 5/.style={column sep=-0.4pt},
row 3 column 2 to 5/.style={my node style},
row 4 column 2 to 5/.style={my node style},
or even better (for the last two)
cell 3 2 to cell 4 5/.style={my node style},
Here's a MWE where such keys would improve readability of the code
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}

Note: I'm also trying to avoid having to declare the style explicitly in each cell where I want it to apply as in rows 2 and 4 of the above example.
UPDATE
I've tried adding the following to my preamble
tikzset{%%
column thepgfmatrixcurrentcolumnspace to thepgfmatrixcurrentcolumn/.try,
}
but when I try to call it as in:
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
column 2 to 5/.style={red},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
There is no apparent effect.
tikz-styles tikz-matrix
I have a node style I would like to apply to many (but not all) the cells/columns/rows of a matrix of nodes. Currently, for every column or cell that I want the style to apply to, I declare the style as:
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
It would be more convenient if I could write something like
column 2 to 5/.style={column sep=-0.4pt},
row 3 column 2 to 5/.style={my node style},
row 4 column 2 to 5/.style={my node style},
or even better (for the last two)
cell 3 2 to cell 4 5/.style={my node style},
Here's a MWE where such keys would improve readability of the code
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}

Note: I'm also trying to avoid having to declare the style explicitly in each cell where I want it to apply as in rows 2 and 4 of the above example.
UPDATE
I've tried adding the following to my preamble
tikzset{%%
column thepgfmatrixcurrentcolumnspace to thepgfmatrixcurrentcolumn/.try,
}
but when I try to call it as in:
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2/.style={column sep=-0.4pt},
column 3/.style={column sep=-0.4pt},
column 4/.style={column sep=-0.4pt},
column 5/.style={column sep=-0.4pt},
row 3 column 2/.style={my node style},
row 3 column 3/.style={my node style},
row 3 column 4/.style={my node style},
row 3 column 5/.style={my node style},
row 3 column 6/.style={my node style},
column 2 to 5/.style={red},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
There is no apparent effect.
tikz-styles tikz-matrix
tikz-styles tikz-matrix
edited Jun 1 '14 at 16:33
A.Ellett
asked Jun 1 '14 at 16:15
A.EllettA.Ellett
36.3k1067170
36.3k1067170
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can use the /.list handler for repeated settings. Also you can wrap them around for more complicated cases but here are two actions for simple use. 
documentclass[tikz,border=5pt]{standalone}
usetikzlibrary{calc,matrix,arrows.meta}
defabs#1{|#1|}
tikzset{
foo1/.style={column #1/.style={column sep=-0.4pt}},
foo2/.style={row 3 column #1/.style={nodes=my node style}},
>=Stealth,
my node style/.style={%%
minimum width=0.60in+12pt,
minimum height=0.30cm+12pt,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
foo1/.list={2,...,5},
foo2/.list={3,...,5}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & {$ 3 $ } & {$ 2x^{2}-5$} & {$ -3 $} & { $2x^{2}-5$} & {$ 3 $} \
};
end{tikzpicture}
end{document}
What if I wanted to apply to multiple row? Can list be nested?
– A.Ellett
Jun 1 '14 at 19:02
@A.Ellett Yes put another style that sets the list with two arguments; one argument for the outer list one for the inner
– percusse
Jun 1 '14 at 19:03
Could you illustrate the nested lists?
– A.Ellett
Jun 1 '14 at 19:06
Also, according to the documentation,/.listis processed within aforeachloop. But when I tried usingforeachto settikzset{...}, nothing seemed to take effect. I assumed that it was a scoping issue. Do you know whatforeachdoesn't seem to be localizing things under./list?
– A.Ellett
Jun 1 '14 at 19:09
@A.Ellett I need to delay you a little. (unless someone else picks it up) but shortly, it's the other way around; it is not run under aforeachscope but it usesforeachto generate the keys. Otherwise as you point out the key setting will be local to the foreach spin.
– percusse
Jun 1 '14 at 19:16
add a comment |
This isn't quite the solution that I want, but it does kind of achieve the effect I want.
Using the etoolbox package, I define two macros: foreachcell and foreachcolumn
usepackage{etoolbox}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
Then in a MWE:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{etoolbox}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
foreachcell from (2,2) to (4,6) do
{tikzset{row aerowspace column aecol/.style={my node style}}}
foreachcolumn from 2 to 5 do
{tikzset{column aecol/.style={column sep=4pt}}}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
I get the desired output (except that draw does not take effect as I would like).
It would be nice to do this as keys. So, I'll not accept my own solution until someone shows how to effectively create the desired keys.
The problem with defining keys (as I attempted to do in the update to my question) is that this needs to be initialized (or so it seems to me) within deftikz@common@matrix@code in tikz@do@matrix. I don't want to rewrite all of that. And I'm not savvy enough with patching to understand how to patch this to embed my desired keys in there.
add a comment |
Here's a solution that works basically the way you suggest. In order to avoid having to hack the definition of the tikz matrix code to make it respond to extra styles, I define a custom handler, called .matrix style used like: row 2 to 3 column 2 to 4/.matrix style={blue}. The row and column can come in either order, either can be omitted and it's possible to have just one number rather than a range. For instance the following are all valid:
row 2 to 3 column 2 to 4/.matrix style={...}
row 2 to 3 column 2/.matrix style={...}
column 2 to 3 row 3/.matrix style={...}
row 2 to 3/.matrix style={...}
column 4 row 2/.matrix style={...}
Because I apparently have too much time on my hands, I wrote both a plain tex version and an expl3 version.
First the plain tex version:
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
Here's the expl3 version.
usepackage{expl3}
makeatletter
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
matrixstyle@parse{#1}
}
}
makeatother
ExplSyntaxOn
makeatletter
def matrixstyle@parse { matrixstyle_parse:n }
makeatother
tl_new:N l_matrixstyle_path_tl
tl_new:N l_matrixstyle_style_tl
tl_new:N l_matrixstyle_type_tl
tl_new:N l_matrixstyle_possible_space_tl
tl_new:N l_matrixstyle_columntext_tl
tl_new:N l_matrixstyle_columnmin_tl
tl_new:N l_matrixstyle_columnmax_tl
tl_new:N l_matrixstyle_rowtext_tl
tl_new:N l_matrixstyle_rowmin_tl
tl_new:N l_matrixstyle_rowmax_tl
cs_new:Nn matrixstyle_expectword:nn {
matrixstyle_expectword_aux:wn #1 q_stop { #2 }
}
cs_gset:Npn use_i_delimit_by_period:nw #1 #2 . { #1 }
cs_new:Npn matrixstyle_expectword_aux:wn #1 #2 q_stop #3 {
peek_meaning_remove_ignore_spaces:NTF { #1 } {
tl_if_empty:nTF { #2 } {
#3
}{
matrixstyle_expectword_aux:wn #2 q_stop { #3 }
}
}{ % An error. Escape out and throw an error.
use_i_delimit_by_period:nw { PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{} }
}
}
cs_new:Nn matrixstyle_parse:n {
tl_clear:N l_matrixstyle_columnmin_tl
tl_clear:N l_matrixstyle_rowmin_tl
tl_set:Nn l_matrixstyle_style_tl { #1 }%
tl_set:Nx l_matrixstyle_path_tl { pgfkeyscurrentpath }%
use:x { exp_not:N matrixstyle_parse_aux: pgfkeyscurrentname . }
}
cs_new:Npn matrixstyle_parse_aux: {
peek_meaning_remove_ignore_spaces:NTF . {
matrixstyle_finish_rows:
}{
peek_meaning_ignore_spaces:NTF { c }{
matrixstyle_expectword:nn { column } {
tl_set:Nn l_matrixstyle_type_tl {column}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}{%
matrixstyle_expectword:nn { row }{
tl_set:Nn l_matrixstyle_type_tl {row}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}%
}
}
newcounttempcount
cs_new:Npn matrixstyle_parse_rangegrab:nw #1 {
afterassignment #1
tempcount = int_eval:w
}
cs_new:Nn matrixstyle_parse_rangemin: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl min_tl }{ thetempcount }%
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
peek_meaning_ignore_spaces:NTF { t }{
matrixstyle_expectword:nn { to }{
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemax: }
}%
}{%
matrixstyle_parse_aux:
}
}
cs_new:Nn matrixstyle_parse_rangemax: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
matrixstyle_parse_aux:
}
cs_new:Nn matrixstyle_finish_rows: {%
tl_set_eq:NN l_matrixstyle_possible_space_tl c_space_tl
tl_if_empty:NTF l_matrixstyle_rowmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_rowtext_tl
matrixstyle_finish_columns:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_rowmin_tl }
{ tl_use:N l_matrixstyle_rowmax_tl }
{
tl_set:Nn l_matrixstyle_rowtext_tl { row ~ ##1 }
matrixstyle_finish_columns:
}
}
}
cs_new:Nn matrixstyle_finish_columns: {%
tl_if_empty:NTF l_matrixstyle_columnmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_columntext_tl
matrixstyle_finish_key:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_columnmin_tl }
{ tl_use:N l_matrixstyle_columnmax_tl }
{
tl_set:Nn l_matrixstyle_columntext_tl { column ~ ##1 }
matrixstyle_finish_key:
}
}
}
makeatletter
cs_new:Nn matrixstyle_finish_key: {
exp_args:Nx pgfkeys
{ tl_use:N l_matrixstyle_path_tl /
tl_use:N l_matrixstyle_rowtext_tl
tl_use:N l_matrixstyle_possible_space_tl
tl_use:N l_matrixstyle_columntext_tl
/. code = { exp_not:o { l_matrixstyle_style_tl } } }
}
makeatother
ExplSyntaxOff
The plain tex version in use:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
begin{document}
tikzset{%%
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2 to 3 row 2 to 3/.matrix style={blue}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f182616%2fhow-can-i-create-keys-such-as-column-to-for-a-tikz-matrix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the /.list handler for repeated settings. Also you can wrap them around for more complicated cases but here are two actions for simple use. 
documentclass[tikz,border=5pt]{standalone}
usetikzlibrary{calc,matrix,arrows.meta}
defabs#1{|#1|}
tikzset{
foo1/.style={column #1/.style={column sep=-0.4pt}},
foo2/.style={row 3 column #1/.style={nodes=my node style}},
>=Stealth,
my node style/.style={%%
minimum width=0.60in+12pt,
minimum height=0.30cm+12pt,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
foo1/.list={2,...,5},
foo2/.list={3,...,5}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & {$ 3 $ } & {$ 2x^{2}-5$} & {$ -3 $} & { $2x^{2}-5$} & {$ 3 $} \
};
end{tikzpicture}
end{document}
What if I wanted to apply to multiple row? Can list be nested?
– A.Ellett
Jun 1 '14 at 19:02
@A.Ellett Yes put another style that sets the list with two arguments; one argument for the outer list one for the inner
– percusse
Jun 1 '14 at 19:03
Could you illustrate the nested lists?
– A.Ellett
Jun 1 '14 at 19:06
Also, according to the documentation,/.listis processed within aforeachloop. But when I tried usingforeachto settikzset{...}, nothing seemed to take effect. I assumed that it was a scoping issue. Do you know whatforeachdoesn't seem to be localizing things under./list?
– A.Ellett
Jun 1 '14 at 19:09
@A.Ellett I need to delay you a little. (unless someone else picks it up) but shortly, it's the other way around; it is not run under aforeachscope but it usesforeachto generate the keys. Otherwise as you point out the key setting will be local to the foreach spin.
– percusse
Jun 1 '14 at 19:16
add a comment |
You can use the /.list handler for repeated settings. Also you can wrap them around for more complicated cases but here are two actions for simple use. 
documentclass[tikz,border=5pt]{standalone}
usetikzlibrary{calc,matrix,arrows.meta}
defabs#1{|#1|}
tikzset{
foo1/.style={column #1/.style={column sep=-0.4pt}},
foo2/.style={row 3 column #1/.style={nodes=my node style}},
>=Stealth,
my node style/.style={%%
minimum width=0.60in+12pt,
minimum height=0.30cm+12pt,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
foo1/.list={2,...,5},
foo2/.list={3,...,5}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & {$ 3 $ } & {$ 2x^{2}-5$} & {$ -3 $} & { $2x^{2}-5$} & {$ 3 $} \
};
end{tikzpicture}
end{document}
What if I wanted to apply to multiple row? Can list be nested?
– A.Ellett
Jun 1 '14 at 19:02
@A.Ellett Yes put another style that sets the list with two arguments; one argument for the outer list one for the inner
– percusse
Jun 1 '14 at 19:03
Could you illustrate the nested lists?
– A.Ellett
Jun 1 '14 at 19:06
Also, according to the documentation,/.listis processed within aforeachloop. But when I tried usingforeachto settikzset{...}, nothing seemed to take effect. I assumed that it was a scoping issue. Do you know whatforeachdoesn't seem to be localizing things under./list?
– A.Ellett
Jun 1 '14 at 19:09
@A.Ellett I need to delay you a little. (unless someone else picks it up) but shortly, it's the other way around; it is not run under aforeachscope but it usesforeachto generate the keys. Otherwise as you point out the key setting will be local to the foreach spin.
– percusse
Jun 1 '14 at 19:16
add a comment |
You can use the /.list handler for repeated settings. Also you can wrap them around for more complicated cases but here are two actions for simple use. 
documentclass[tikz,border=5pt]{standalone}
usetikzlibrary{calc,matrix,arrows.meta}
defabs#1{|#1|}
tikzset{
foo1/.style={column #1/.style={column sep=-0.4pt}},
foo2/.style={row 3 column #1/.style={nodes=my node style}},
>=Stealth,
my node style/.style={%%
minimum width=0.60in+12pt,
minimum height=0.30cm+12pt,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
foo1/.list={2,...,5},
foo2/.list={3,...,5}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & {$ 3 $ } & {$ 2x^{2}-5$} & {$ -3 $} & { $2x^{2}-5$} & {$ 3 $} \
};
end{tikzpicture}
end{document}
You can use the /.list handler for repeated settings. Also you can wrap them around for more complicated cases but here are two actions for simple use. 
documentclass[tikz,border=5pt]{standalone}
usetikzlibrary{calc,matrix,arrows.meta}
defabs#1{|#1|}
tikzset{
foo1/.style={column #1/.style={column sep=-0.4pt}},
foo2/.style={row 3 column #1/.style={nodes=my node style}},
>=Stealth,
my node style/.style={%%
minimum width=0.60in+12pt,
minimum height=0.30cm+12pt,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
foo1/.list={2,...,5},
foo2/.list={3,...,5}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & {$ 3 $ } & {$ 2x^{2}-5$} & {$ -3 $} & { $2x^{2}-5$} & {$ 3 $} \
};
end{tikzpicture}
end{document}
answered Jun 1 '14 at 19:01
percussepercusse
137k14254493
137k14254493
What if I wanted to apply to multiple row? Can list be nested?
– A.Ellett
Jun 1 '14 at 19:02
@A.Ellett Yes put another style that sets the list with two arguments; one argument for the outer list one for the inner
– percusse
Jun 1 '14 at 19:03
Could you illustrate the nested lists?
– A.Ellett
Jun 1 '14 at 19:06
Also, according to the documentation,/.listis processed within aforeachloop. But when I tried usingforeachto settikzset{...}, nothing seemed to take effect. I assumed that it was a scoping issue. Do you know whatforeachdoesn't seem to be localizing things under./list?
– A.Ellett
Jun 1 '14 at 19:09
@A.Ellett I need to delay you a little. (unless someone else picks it up) but shortly, it's the other way around; it is not run under aforeachscope but it usesforeachto generate the keys. Otherwise as you point out the key setting will be local to the foreach spin.
– percusse
Jun 1 '14 at 19:16
add a comment |
What if I wanted to apply to multiple row? Can list be nested?
– A.Ellett
Jun 1 '14 at 19:02
@A.Ellett Yes put another style that sets the list with two arguments; one argument for the outer list one for the inner
– percusse
Jun 1 '14 at 19:03
Could you illustrate the nested lists?
– A.Ellett
Jun 1 '14 at 19:06
Also, according to the documentation,/.listis processed within aforeachloop. But when I tried usingforeachto settikzset{...}, nothing seemed to take effect. I assumed that it was a scoping issue. Do you know whatforeachdoesn't seem to be localizing things under./list?
– A.Ellett
Jun 1 '14 at 19:09
@A.Ellett I need to delay you a little. (unless someone else picks it up) but shortly, it's the other way around; it is not run under aforeachscope but it usesforeachto generate the keys. Otherwise as you point out the key setting will be local to the foreach spin.
– percusse
Jun 1 '14 at 19:16
What if I wanted to apply to multiple row? Can list be nested?
– A.Ellett
Jun 1 '14 at 19:02
What if I wanted to apply to multiple row? Can list be nested?
– A.Ellett
Jun 1 '14 at 19:02
@A.Ellett Yes put another style that sets the list with two arguments; one argument for the outer list one for the inner
– percusse
Jun 1 '14 at 19:03
@A.Ellett Yes put another style that sets the list with two arguments; one argument for the outer list one for the inner
– percusse
Jun 1 '14 at 19:03
Could you illustrate the nested lists?
– A.Ellett
Jun 1 '14 at 19:06
Could you illustrate the nested lists?
– A.Ellett
Jun 1 '14 at 19:06
Also, according to the documentation,
/.list is processed within a foreach loop. But when I tried using foreach to set tikzset{...}, nothing seemed to take effect. I assumed that it was a scoping issue. Do you know what foreach doesn't seem to be localizing things under ./list?– A.Ellett
Jun 1 '14 at 19:09
Also, according to the documentation,
/.list is processed within a foreach loop. But when I tried using foreach to set tikzset{...}, nothing seemed to take effect. I assumed that it was a scoping issue. Do you know what foreach doesn't seem to be localizing things under ./list?– A.Ellett
Jun 1 '14 at 19:09
@A.Ellett I need to delay you a little. (unless someone else picks it up) but shortly, it's the other way around; it is not run under a
foreach scope but it uses foreach to generate the keys. Otherwise as you point out the key setting will be local to the foreach spin.– percusse
Jun 1 '14 at 19:16
@A.Ellett I need to delay you a little. (unless someone else picks it up) but shortly, it's the other way around; it is not run under a
foreach scope but it uses foreach to generate the keys. Otherwise as you point out the key setting will be local to the foreach spin.– percusse
Jun 1 '14 at 19:16
add a comment |
This isn't quite the solution that I want, but it does kind of achieve the effect I want.
Using the etoolbox package, I define two macros: foreachcell and foreachcolumn
usepackage{etoolbox}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
Then in a MWE:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{etoolbox}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
foreachcell from (2,2) to (4,6) do
{tikzset{row aerowspace column aecol/.style={my node style}}}
foreachcolumn from 2 to 5 do
{tikzset{column aecol/.style={column sep=4pt}}}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
I get the desired output (except that draw does not take effect as I would like).
It would be nice to do this as keys. So, I'll not accept my own solution until someone shows how to effectively create the desired keys.
The problem with defining keys (as I attempted to do in the update to my question) is that this needs to be initialized (or so it seems to me) within deftikz@common@matrix@code in tikz@do@matrix. I don't want to rewrite all of that. And I'm not savvy enough with patching to understand how to patch this to embed my desired keys in there.
add a comment |
This isn't quite the solution that I want, but it does kind of achieve the effect I want.
Using the etoolbox package, I define two macros: foreachcell and foreachcolumn
usepackage{etoolbox}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
Then in a MWE:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{etoolbox}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
foreachcell from (2,2) to (4,6) do
{tikzset{row aerowspace column aecol/.style={my node style}}}
foreachcolumn from 2 to 5 do
{tikzset{column aecol/.style={column sep=4pt}}}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
I get the desired output (except that draw does not take effect as I would like).
It would be nice to do this as keys. So, I'll not accept my own solution until someone shows how to effectively create the desired keys.
The problem with defining keys (as I attempted to do in the update to my question) is that this needs to be initialized (or so it seems to me) within deftikz@common@matrix@code in tikz@do@matrix. I don't want to rewrite all of that. And I'm not savvy enough with patching to understand how to patch this to embed my desired keys in there.
add a comment |
This isn't quite the solution that I want, but it does kind of achieve the effect I want.
Using the etoolbox package, I define two macros: foreachcell and foreachcolumn
usepackage{etoolbox}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
Then in a MWE:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{etoolbox}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
foreachcell from (2,2) to (4,6) do
{tikzset{row aerowspace column aecol/.style={my node style}}}
foreachcolumn from 2 to 5 do
{tikzset{column aecol/.style={column sep=4pt}}}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
I get the desired output (except that draw does not take effect as I would like).
It would be nice to do this as keys. So, I'll not accept my own solution until someone shows how to effectively create the desired keys.
The problem with defining keys (as I attempted to do in the update to my question) is that this needs to be initialized (or so it seems to me) within deftikz@common@matrix@code in tikz@do@matrix. I don't want to rewrite all of that. And I'm not savvy enough with patching to understand how to patch this to embed my desired keys in there.
This isn't quite the solution that I want, but it does kind of achieve the effect I want.
Using the etoolbox package, I define two macros: foreachcell and foreachcolumn
usepackage{etoolbox}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
Then in a MWE:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{etoolbox}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
newcommandforeachcell{ae@foreach@cell}
defae@foreach@cell from (#1,#2) to (#3,#4) do #5{%%
defaecol{#2}%%
defaerow{#1}%%
whileboolexpr{ not ( test { ifnumcomp{aecol}{=}{#4}} and test{ifnumcomp{aerow}{=}{#3}} ) }
{
#5
edefaecol{numbernumexpraecol+1relax}
ifnumaecol>#4relax
edefaerow{numbernumexpraerow+1relax}
edefaecol{#2}
fi
}
}
newcommandforeachcolumn{ae@foreach@col}
defae@foreach@col from #1 to #2 do #3{%%
defaecol{#1}%%
whileboolexpr{ test {ifnumcomp{aecol}{<}{#2}} or test{ifnumcomp{aecol}{=}{#2}} }
{ #3
edefaecol{numbernumexpraecol+1relax}
}}
makeatother
foreachcell from (2,2) to (4,6) do
{tikzset{row aerowspace column aecol/.style={my node style}}}
foreachcolumn from 2 to 5 do
{tikzset{column aecol/.style={column sep=4pt}}}
tikzset{%%
>=Stealth,
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{document}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
I get the desired output (except that draw does not take effect as I would like).
It would be nice to do this as keys. So, I'll not accept my own solution until someone shows how to effectively create the desired keys.
The problem with defining keys (as I attempted to do in the update to my question) is that this needs to be initialized (or so it seems to me) within deftikz@common@matrix@code in tikz@do@matrix. I don't want to rewrite all of that. And I'm not savvy enough with patching to understand how to patch this to embed my desired keys in there.
answered Jun 1 '14 at 18:51
A.EllettA.Ellett
36.3k1067170
36.3k1067170
add a comment |
add a comment |
Here's a solution that works basically the way you suggest. In order to avoid having to hack the definition of the tikz matrix code to make it respond to extra styles, I define a custom handler, called .matrix style used like: row 2 to 3 column 2 to 4/.matrix style={blue}. The row and column can come in either order, either can be omitted and it's possible to have just one number rather than a range. For instance the following are all valid:
row 2 to 3 column 2 to 4/.matrix style={...}
row 2 to 3 column 2/.matrix style={...}
column 2 to 3 row 3/.matrix style={...}
row 2 to 3/.matrix style={...}
column 4 row 2/.matrix style={...}
Because I apparently have too much time on my hands, I wrote both a plain tex version and an expl3 version.
First the plain tex version:
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
Here's the expl3 version.
usepackage{expl3}
makeatletter
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
matrixstyle@parse{#1}
}
}
makeatother
ExplSyntaxOn
makeatletter
def matrixstyle@parse { matrixstyle_parse:n }
makeatother
tl_new:N l_matrixstyle_path_tl
tl_new:N l_matrixstyle_style_tl
tl_new:N l_matrixstyle_type_tl
tl_new:N l_matrixstyle_possible_space_tl
tl_new:N l_matrixstyle_columntext_tl
tl_new:N l_matrixstyle_columnmin_tl
tl_new:N l_matrixstyle_columnmax_tl
tl_new:N l_matrixstyle_rowtext_tl
tl_new:N l_matrixstyle_rowmin_tl
tl_new:N l_matrixstyle_rowmax_tl
cs_new:Nn matrixstyle_expectword:nn {
matrixstyle_expectword_aux:wn #1 q_stop { #2 }
}
cs_gset:Npn use_i_delimit_by_period:nw #1 #2 . { #1 }
cs_new:Npn matrixstyle_expectword_aux:wn #1 #2 q_stop #3 {
peek_meaning_remove_ignore_spaces:NTF { #1 } {
tl_if_empty:nTF { #2 } {
#3
}{
matrixstyle_expectword_aux:wn #2 q_stop { #3 }
}
}{ % An error. Escape out and throw an error.
use_i_delimit_by_period:nw { PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{} }
}
}
cs_new:Nn matrixstyle_parse:n {
tl_clear:N l_matrixstyle_columnmin_tl
tl_clear:N l_matrixstyle_rowmin_tl
tl_set:Nn l_matrixstyle_style_tl { #1 }%
tl_set:Nx l_matrixstyle_path_tl { pgfkeyscurrentpath }%
use:x { exp_not:N matrixstyle_parse_aux: pgfkeyscurrentname . }
}
cs_new:Npn matrixstyle_parse_aux: {
peek_meaning_remove_ignore_spaces:NTF . {
matrixstyle_finish_rows:
}{
peek_meaning_ignore_spaces:NTF { c }{
matrixstyle_expectword:nn { column } {
tl_set:Nn l_matrixstyle_type_tl {column}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}{%
matrixstyle_expectword:nn { row }{
tl_set:Nn l_matrixstyle_type_tl {row}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}%
}
}
newcounttempcount
cs_new:Npn matrixstyle_parse_rangegrab:nw #1 {
afterassignment #1
tempcount = int_eval:w
}
cs_new:Nn matrixstyle_parse_rangemin: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl min_tl }{ thetempcount }%
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
peek_meaning_ignore_spaces:NTF { t }{
matrixstyle_expectword:nn { to }{
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemax: }
}%
}{%
matrixstyle_parse_aux:
}
}
cs_new:Nn matrixstyle_parse_rangemax: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
matrixstyle_parse_aux:
}
cs_new:Nn matrixstyle_finish_rows: {%
tl_set_eq:NN l_matrixstyle_possible_space_tl c_space_tl
tl_if_empty:NTF l_matrixstyle_rowmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_rowtext_tl
matrixstyle_finish_columns:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_rowmin_tl }
{ tl_use:N l_matrixstyle_rowmax_tl }
{
tl_set:Nn l_matrixstyle_rowtext_tl { row ~ ##1 }
matrixstyle_finish_columns:
}
}
}
cs_new:Nn matrixstyle_finish_columns: {%
tl_if_empty:NTF l_matrixstyle_columnmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_columntext_tl
matrixstyle_finish_key:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_columnmin_tl }
{ tl_use:N l_matrixstyle_columnmax_tl }
{
tl_set:Nn l_matrixstyle_columntext_tl { column ~ ##1 }
matrixstyle_finish_key:
}
}
}
makeatletter
cs_new:Nn matrixstyle_finish_key: {
exp_args:Nx pgfkeys
{ tl_use:N l_matrixstyle_path_tl /
tl_use:N l_matrixstyle_rowtext_tl
tl_use:N l_matrixstyle_possible_space_tl
tl_use:N l_matrixstyle_columntext_tl
/. code = { exp_not:o { l_matrixstyle_style_tl } } }
}
makeatother
ExplSyntaxOff
The plain tex version in use:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
begin{document}
tikzset{%%
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2 to 3 row 2 to 3/.matrix style={blue}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
add a comment |
Here's a solution that works basically the way you suggest. In order to avoid having to hack the definition of the tikz matrix code to make it respond to extra styles, I define a custom handler, called .matrix style used like: row 2 to 3 column 2 to 4/.matrix style={blue}. The row and column can come in either order, either can be omitted and it's possible to have just one number rather than a range. For instance the following are all valid:
row 2 to 3 column 2 to 4/.matrix style={...}
row 2 to 3 column 2/.matrix style={...}
column 2 to 3 row 3/.matrix style={...}
row 2 to 3/.matrix style={...}
column 4 row 2/.matrix style={...}
Because I apparently have too much time on my hands, I wrote both a plain tex version and an expl3 version.
First the plain tex version:
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
Here's the expl3 version.
usepackage{expl3}
makeatletter
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
matrixstyle@parse{#1}
}
}
makeatother
ExplSyntaxOn
makeatletter
def matrixstyle@parse { matrixstyle_parse:n }
makeatother
tl_new:N l_matrixstyle_path_tl
tl_new:N l_matrixstyle_style_tl
tl_new:N l_matrixstyle_type_tl
tl_new:N l_matrixstyle_possible_space_tl
tl_new:N l_matrixstyle_columntext_tl
tl_new:N l_matrixstyle_columnmin_tl
tl_new:N l_matrixstyle_columnmax_tl
tl_new:N l_matrixstyle_rowtext_tl
tl_new:N l_matrixstyle_rowmin_tl
tl_new:N l_matrixstyle_rowmax_tl
cs_new:Nn matrixstyle_expectword:nn {
matrixstyle_expectword_aux:wn #1 q_stop { #2 }
}
cs_gset:Npn use_i_delimit_by_period:nw #1 #2 . { #1 }
cs_new:Npn matrixstyle_expectword_aux:wn #1 #2 q_stop #3 {
peek_meaning_remove_ignore_spaces:NTF { #1 } {
tl_if_empty:nTF { #2 } {
#3
}{
matrixstyle_expectword_aux:wn #2 q_stop { #3 }
}
}{ % An error. Escape out and throw an error.
use_i_delimit_by_period:nw { PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{} }
}
}
cs_new:Nn matrixstyle_parse:n {
tl_clear:N l_matrixstyle_columnmin_tl
tl_clear:N l_matrixstyle_rowmin_tl
tl_set:Nn l_matrixstyle_style_tl { #1 }%
tl_set:Nx l_matrixstyle_path_tl { pgfkeyscurrentpath }%
use:x { exp_not:N matrixstyle_parse_aux: pgfkeyscurrentname . }
}
cs_new:Npn matrixstyle_parse_aux: {
peek_meaning_remove_ignore_spaces:NTF . {
matrixstyle_finish_rows:
}{
peek_meaning_ignore_spaces:NTF { c }{
matrixstyle_expectword:nn { column } {
tl_set:Nn l_matrixstyle_type_tl {column}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}{%
matrixstyle_expectword:nn { row }{
tl_set:Nn l_matrixstyle_type_tl {row}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}%
}
}
newcounttempcount
cs_new:Npn matrixstyle_parse_rangegrab:nw #1 {
afterassignment #1
tempcount = int_eval:w
}
cs_new:Nn matrixstyle_parse_rangemin: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl min_tl }{ thetempcount }%
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
peek_meaning_ignore_spaces:NTF { t }{
matrixstyle_expectword:nn { to }{
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemax: }
}%
}{%
matrixstyle_parse_aux:
}
}
cs_new:Nn matrixstyle_parse_rangemax: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
matrixstyle_parse_aux:
}
cs_new:Nn matrixstyle_finish_rows: {%
tl_set_eq:NN l_matrixstyle_possible_space_tl c_space_tl
tl_if_empty:NTF l_matrixstyle_rowmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_rowtext_tl
matrixstyle_finish_columns:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_rowmin_tl }
{ tl_use:N l_matrixstyle_rowmax_tl }
{
tl_set:Nn l_matrixstyle_rowtext_tl { row ~ ##1 }
matrixstyle_finish_columns:
}
}
}
cs_new:Nn matrixstyle_finish_columns: {%
tl_if_empty:NTF l_matrixstyle_columnmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_columntext_tl
matrixstyle_finish_key:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_columnmin_tl }
{ tl_use:N l_matrixstyle_columnmax_tl }
{
tl_set:Nn l_matrixstyle_columntext_tl { column ~ ##1 }
matrixstyle_finish_key:
}
}
}
makeatletter
cs_new:Nn matrixstyle_finish_key: {
exp_args:Nx pgfkeys
{ tl_use:N l_matrixstyle_path_tl /
tl_use:N l_matrixstyle_rowtext_tl
tl_use:N l_matrixstyle_possible_space_tl
tl_use:N l_matrixstyle_columntext_tl
/. code = { exp_not:o { l_matrixstyle_style_tl } } }
}
makeatother
ExplSyntaxOff
The plain tex version in use:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
begin{document}
tikzset{%%
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2 to 3 row 2 to 3/.matrix style={blue}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
add a comment |
Here's a solution that works basically the way you suggest. In order to avoid having to hack the definition of the tikz matrix code to make it respond to extra styles, I define a custom handler, called .matrix style used like: row 2 to 3 column 2 to 4/.matrix style={blue}. The row and column can come in either order, either can be omitted and it's possible to have just one number rather than a range. For instance the following are all valid:
row 2 to 3 column 2 to 4/.matrix style={...}
row 2 to 3 column 2/.matrix style={...}
column 2 to 3 row 3/.matrix style={...}
row 2 to 3/.matrix style={...}
column 4 row 2/.matrix style={...}
Because I apparently have too much time on my hands, I wrote both a plain tex version and an expl3 version.
First the plain tex version:
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
Here's the expl3 version.
usepackage{expl3}
makeatletter
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
matrixstyle@parse{#1}
}
}
makeatother
ExplSyntaxOn
makeatletter
def matrixstyle@parse { matrixstyle_parse:n }
makeatother
tl_new:N l_matrixstyle_path_tl
tl_new:N l_matrixstyle_style_tl
tl_new:N l_matrixstyle_type_tl
tl_new:N l_matrixstyle_possible_space_tl
tl_new:N l_matrixstyle_columntext_tl
tl_new:N l_matrixstyle_columnmin_tl
tl_new:N l_matrixstyle_columnmax_tl
tl_new:N l_matrixstyle_rowtext_tl
tl_new:N l_matrixstyle_rowmin_tl
tl_new:N l_matrixstyle_rowmax_tl
cs_new:Nn matrixstyle_expectword:nn {
matrixstyle_expectword_aux:wn #1 q_stop { #2 }
}
cs_gset:Npn use_i_delimit_by_period:nw #1 #2 . { #1 }
cs_new:Npn matrixstyle_expectword_aux:wn #1 #2 q_stop #3 {
peek_meaning_remove_ignore_spaces:NTF { #1 } {
tl_if_empty:nTF { #2 } {
#3
}{
matrixstyle_expectword_aux:wn #2 q_stop { #3 }
}
}{ % An error. Escape out and throw an error.
use_i_delimit_by_period:nw { PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{} }
}
}
cs_new:Nn matrixstyle_parse:n {
tl_clear:N l_matrixstyle_columnmin_tl
tl_clear:N l_matrixstyle_rowmin_tl
tl_set:Nn l_matrixstyle_style_tl { #1 }%
tl_set:Nx l_matrixstyle_path_tl { pgfkeyscurrentpath }%
use:x { exp_not:N matrixstyle_parse_aux: pgfkeyscurrentname . }
}
cs_new:Npn matrixstyle_parse_aux: {
peek_meaning_remove_ignore_spaces:NTF . {
matrixstyle_finish_rows:
}{
peek_meaning_ignore_spaces:NTF { c }{
matrixstyle_expectword:nn { column } {
tl_set:Nn l_matrixstyle_type_tl {column}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}{%
matrixstyle_expectword:nn { row }{
tl_set:Nn l_matrixstyle_type_tl {row}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}%
}
}
newcounttempcount
cs_new:Npn matrixstyle_parse_rangegrab:nw #1 {
afterassignment #1
tempcount = int_eval:w
}
cs_new:Nn matrixstyle_parse_rangemin: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl min_tl }{ thetempcount }%
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
peek_meaning_ignore_spaces:NTF { t }{
matrixstyle_expectword:nn { to }{
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemax: }
}%
}{%
matrixstyle_parse_aux:
}
}
cs_new:Nn matrixstyle_parse_rangemax: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
matrixstyle_parse_aux:
}
cs_new:Nn matrixstyle_finish_rows: {%
tl_set_eq:NN l_matrixstyle_possible_space_tl c_space_tl
tl_if_empty:NTF l_matrixstyle_rowmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_rowtext_tl
matrixstyle_finish_columns:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_rowmin_tl }
{ tl_use:N l_matrixstyle_rowmax_tl }
{
tl_set:Nn l_matrixstyle_rowtext_tl { row ~ ##1 }
matrixstyle_finish_columns:
}
}
}
cs_new:Nn matrixstyle_finish_columns: {%
tl_if_empty:NTF l_matrixstyle_columnmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_columntext_tl
matrixstyle_finish_key:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_columnmin_tl }
{ tl_use:N l_matrixstyle_columnmax_tl }
{
tl_set:Nn l_matrixstyle_columntext_tl { column ~ ##1 }
matrixstyle_finish_key:
}
}
}
makeatletter
cs_new:Nn matrixstyle_finish_key: {
exp_args:Nx pgfkeys
{ tl_use:N l_matrixstyle_path_tl /
tl_use:N l_matrixstyle_rowtext_tl
tl_use:N l_matrixstyle_possible_space_tl
tl_use:N l_matrixstyle_columntext_tl
/. code = { exp_not:o { l_matrixstyle_style_tl } } }
}
makeatother
ExplSyntaxOff
The plain tex version in use:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
begin{document}
tikzset{%%
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2 to 3 row 2 to 3/.matrix style={blue}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
Here's a solution that works basically the way you suggest. In order to avoid having to hack the definition of the tikz matrix code to make it respond to extra styles, I define a custom handler, called .matrix style used like: row 2 to 3 column 2 to 4/.matrix style={blue}. The row and column can come in either order, either can be omitted and it's possible to have just one number rather than a range. For instance the following are all valid:
row 2 to 3 column 2 to 4/.matrix style={...}
row 2 to 3 column 2/.matrix style={...}
column 2 to 3 row 3/.matrix style={...}
row 2 to 3/.matrix style={...}
column 4 row 2/.matrix style={...}
Because I apparently have too much time on my hands, I wrote both a plain tex version and an expl3 version.
First the plain tex version:
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
Here's the expl3 version.
usepackage{expl3}
makeatletter
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
matrixstyle@parse{#1}
}
}
makeatother
ExplSyntaxOn
makeatletter
def matrixstyle@parse { matrixstyle_parse:n }
makeatother
tl_new:N l_matrixstyle_path_tl
tl_new:N l_matrixstyle_style_tl
tl_new:N l_matrixstyle_type_tl
tl_new:N l_matrixstyle_possible_space_tl
tl_new:N l_matrixstyle_columntext_tl
tl_new:N l_matrixstyle_columnmin_tl
tl_new:N l_matrixstyle_columnmax_tl
tl_new:N l_matrixstyle_rowtext_tl
tl_new:N l_matrixstyle_rowmin_tl
tl_new:N l_matrixstyle_rowmax_tl
cs_new:Nn matrixstyle_expectword:nn {
matrixstyle_expectword_aux:wn #1 q_stop { #2 }
}
cs_gset:Npn use_i_delimit_by_period:nw #1 #2 . { #1 }
cs_new:Npn matrixstyle_expectword_aux:wn #1 #2 q_stop #3 {
peek_meaning_remove_ignore_spaces:NTF { #1 } {
tl_if_empty:nTF { #2 } {
#3
}{
matrixstyle_expectword_aux:wn #2 q_stop { #3 }
}
}{ % An error. Escape out and throw an error.
use_i_delimit_by_period:nw { PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{} }
}
}
cs_new:Nn matrixstyle_parse:n {
tl_clear:N l_matrixstyle_columnmin_tl
tl_clear:N l_matrixstyle_rowmin_tl
tl_set:Nn l_matrixstyle_style_tl { #1 }%
tl_set:Nx l_matrixstyle_path_tl { pgfkeyscurrentpath }%
use:x { exp_not:N matrixstyle_parse_aux: pgfkeyscurrentname . }
}
cs_new:Npn matrixstyle_parse_aux: {
peek_meaning_remove_ignore_spaces:NTF . {
matrixstyle_finish_rows:
}{
peek_meaning_ignore_spaces:NTF { c }{
matrixstyle_expectword:nn { column } {
tl_set:Nn l_matrixstyle_type_tl {column}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}{%
matrixstyle_expectword:nn { row }{
tl_set:Nn l_matrixstyle_type_tl {row}
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemin: }
}%
}%
}
}
newcounttempcount
cs_new:Npn matrixstyle_parse_rangegrab:nw #1 {
afterassignment #1
tempcount = int_eval:w
}
cs_new:Nn matrixstyle_parse_rangemin: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl min_tl }{ thetempcount }%
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
peek_meaning_ignore_spaces:NTF { t }{
matrixstyle_expectword:nn { to }{
matrixstyle_parse_rangegrab:nw { matrixstyle_parse_rangemax: }
}%
}{%
matrixstyle_parse_aux:
}
}
cs_new:Nn matrixstyle_parse_rangemax: {
tl_set:co { l_matrixstyle_ tl_use:N l_matrixstyle_type_tl max_tl }{ thetempcount }
matrixstyle_parse_aux:
}
cs_new:Nn matrixstyle_finish_rows: {%
tl_set_eq:NN l_matrixstyle_possible_space_tl c_space_tl
tl_if_empty:NTF l_matrixstyle_rowmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_rowtext_tl
matrixstyle_finish_columns:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_rowmin_tl }
{ tl_use:N l_matrixstyle_rowmax_tl }
{
tl_set:Nn l_matrixstyle_rowtext_tl { row ~ ##1 }
matrixstyle_finish_columns:
}
}
}
cs_new:Nn matrixstyle_finish_columns: {%
tl_if_empty:NTF l_matrixstyle_columnmin_tl {
tl_clear:N l_matrixstyle_possible_space_tl
tl_clear:N l_matrixstyle_columntext_tl
matrixstyle_finish_key:
} {
int_step_inline:nnn
{ tl_use:N l_matrixstyle_columnmin_tl }
{ tl_use:N l_matrixstyle_columnmax_tl }
{
tl_set:Nn l_matrixstyle_columntext_tl { column ~ ##1 }
matrixstyle_finish_key:
}
}
}
makeatletter
cs_new:Nn matrixstyle_finish_key: {
exp_args:Nx pgfkeys
{ tl_use:N l_matrixstyle_path_tl /
tl_use:N l_matrixstyle_rowtext_tl
tl_use:N l_matrixstyle_possible_space_tl
tl_use:N l_matrixstyle_columntext_tl
/. code = { exp_not:o { l_matrixstyle_style_tl } } }
}
makeatother
ExplSyntaxOff
The plain tex version in use:
documentclass[border=6pt]{standalone}
usepackage{amsmath}
newcommandabs[1]{lvert#1rvert}
usepackage{tikz}
usetikzlibrary{calc}
usetikzlibrary{matrix}
usetikzlibrary{arrows.meta}
makeatletter
defmatrixstyle@ifempty#1{ifx$#1$expandafter@firstoftwoelseexpandafter@secondoftwofi}
defmatrixstyle@expectword#1#2{matrixstyle@expectword@#1nill{#2}}
defmatrixstyle@expectword@#1#2nill#3{%
@ifnextchar{#1}{%
matrixstyle@ifempty{#2}{%
defnext{#3}%
}{%
defnext{matrixstyle@expectword@#2nill{#3}}%
}%
expandafternext@gobble
}{%
matrixstyle@expectword@fail
}%
}
defmatrixstyle@expectword@fail#1.{PackageError{matrix style}{Unexpected row/column specifier "pgfkeyscurrentname"}{}}
pgfkeys{/handlers/.matrix style/.code={pgfkeys{pgfkeyscurrentpath/.matrix code=pgfkeysalso{#1}}}}
pgfkeys{/handlers/.matrix code/.code={
edefpgfkeyscurrentkey{pgfkeyscurrentpath}%
pgfkeys@split@path
edefmatrixstyle@thepath{pgfkeyscurrentpath}%
defmatrixstyle@thestyle{#1}%
edeftemp{noexpandmatrixstyle@parsepgfkeyscurrentname.}%
temp
}
}
defmatrixstyle@parse{%
letmatrixstyle@columnminempty%
letmatrixstyle@rowminempty%
matrixstyle@parse@
}
defmatrixstyle@parse@{%
@ifnextchar.{%
expandaftermatrixstyle@finish@rows@gobble
}{%
@ifnextchar{c}{%
matrixstyle@expectword{column}{deftype{column}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}{%
matrixstyle@expectword{row}{deftype{row}matrixstyle@parse@range@grab{matrixstyle@parse@range@min}}%
}%
}
}
newcounttempcount
newcounttempcountb
defmatrixstyle@parse@range@grab#1{afterassignment#1tempcount=numexpr}
defmatrixstyle@parse@range@min{%
expandafteredefcsname matrixstyle@type minendcsname{thetempcount}%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
@ifnextchar{t}{%
matrixstyle@expectword{to}{matrixstyle@parse@range@grab{matrixstyle@parse@range@max}}%
}{%
matrixstyle@parse@
}
}
defmatrixstyle@parse@range@max{%
expandafteredefcsname matrixstyle@type maxendcsname{thetempcount}%
matrixstyle@parse@
}
defmatrixstyle@finish@rows{%
letmatrixstyle@possiblespacespace
ifxmatrixstyle@rowminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@rowtextempty
matrixstyle@finish@columns
else
tempcount=matrixstyle@rowminrelax
loop
edefmatrixstyle@rowtext{row thetempcount}%
% Loops don't nest correctly because they both use the same iterate command
% either the inner loop needs to be in a scope, which won't work for us
% or we need to save the value of iterate
letmatrixstyle@saveiterateiterate
matrixstyle@finish@columns
letiteratematrixstyle@saveiterate
advancetempcount1relax
unlessifnumtempcount>matrixstyle@rowmaxrepeat
fi
}
defmatrixstyle@finish@columns{%
ifxmatrixstyle@columnminempty
letmatrixstyle@possiblespaceempty
letmatrixstyle@columntextempty
matrixstyle@finish@key
else
tempcountb=matrixstyle@columnminrelax
loop
edefmatrixstyle@columntext{column thetempcountb}%
matrixstyle@finish@key
advancetempcountb1relax
unlessifnumtempcountb>matrixstyle@columnmaxrepeat
fi
}
defmatrixstyle@finish@key{%
edeftemp{
matrixstyle@thepath/%
matrixstyle@rowtext matrixstyle@possiblespace matrixstyle@columntext
/.code={unexpandedexpandafter{matrixstyle@thestyle}}%
}%
expandafterpgfkeysexpandafter{temp}
}
begin{document}
tikzset{%%
my node style/.style={%%
minimum width=dimexpr0.60in+12ptrelax,
minimum height=dimexpr0.30cm+12ptrelax,
outer sep=0pt,
draw},
}
begin{tikzpicture}
matrix (TBL) [%%
matrix of nodes,
nodes={%%
align=center,
inner sep=0pt,
anchor=center},
column 2 to 3 row 2 to 3/.matrix style={blue}
]
{
& {$(-infty,-2)$} & {$(-2,-1)$} & {$(-1,1)$} & {$(1,2)$} & {$(2,infty)$} \[2pt]
{$abs{x^{2}-1}=,$} & |[my node style]| {$x^{2}-1 $} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$1-x^{2}$} & |[my node style]| {$x^{2}-1$} & |[my node style]| {$x^{2}-1$} \[-0.4pt]
{$abs{x^{2}-4}=,$} & {$x^{2}-4 $} & {$4-x^{2}$} & {$4-x^{2}$} & {$4-x^{2}$} & {$x^{2}-4$} \[2pt]
{$g(x) =$} & |[my node style]| {$ 3 $ } & |[my node style]| {$ 2x^{2}-5$} & |[my node style]| {$ -3 $} & |[my node style]| { $2x^{2}-5$} & |[my node style]| {$ 3 $} \
};
end{tikzpicture}
end{document}
answered 34 mins ago
Hood ChathamHood Chatham
4,2491428
4,2491428
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f182616%2fhow-can-i-create-keys-such-as-column-to-for-a-tikz-matrix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown