TeX - LaTeX Asked on March 26, 2021
I would like to highlight the rows n*(n+3)/2 = 0, 2, 5, 9, 14, 20, 27, 35,...
How could I automate that best way using the term n*(n+3)/2
?
documentclass{standalone}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}
begin{document}
pgfplotstableset{
highlightrow/.style={
postproc cell content/.append code={
count0=pgfplotstablerow
advancecount0 by0
ifnumcount0=#1
% pgfkeysalso{@cell content/.add={$bf}{$}}
pgfkeysalso{@cell content=textbf{##1}}%
fi}, }, }
%defList{7,9,10}
pgfplotstabletypeset[string type,
highlightrow=0, % works
highlightrow/.list={2,5,9}, % works
%highlightrow/.list=List, % works not
]{
A B C
0 x x
1 x x
2 x x
3 x x
4 x x
5 x x
6 x x
7 x x
8 x x
9 x x
10 x x
11 x x
}
end{document}
handlers/.list
uses foreach
to unfold the list:
foreach pgf@keys@key in{#1}%
while pgffor
handle the list in different ways by checking if there is a leading bgroup
:
defpgffor@@vars@end in{%
pgfutil@ifnextcharbgroup{pgffor@normal@list}{pgffor@macro@list}%
}
As you see macro list (unbraced) is expanded to a normal list (braced) first:
defpgffor@macro@list#1{%
expandafterpgffor@normal@listexpandafter{#1}}
So when you write highlightrow=List
and expect it to be processed as a macro list, but actually List
is braced and be processed as a normal list and then error occurs.
The fast way to fix the problem is what Ulrike Fischer has mentioned in her comment: use highlightrow/.list/.expand once=List
.
I provide another way to make it possible to use a function to get the row index with expl3
.
documentclass[margin=1cm]{standalone}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}
usepackage{xparse}
pgfplotstableset{
highlightrow/.style 2 args={
postproc cell content/.append code={
ifnumpgfplotstablerow=#1
pgfkeysalso{@cell content={#2##1}}%
fi
},
},
}
ExplSyntaxOn
NewDocumentCommand { rowstyle } { O{30} O{#1} m }
{
row_style:nnn { #1 } { #2 } { #3 }
}
int_new:N l__i_int
int_new:N l__index_int
cs_new_protected:Nn row_style:nnn
{
cs_set:Nn __parse_expr:n
{
fp_to_int:n { floor(#2) }
}
fp_compare:nT { #1 >= 0 }
{
int_zero:N l__i_int
fp_while_do:nn { __parse_expr:n { l__i_int } <= #1 }
{
tl_set:Nx l_tempa_tl {
exp_not:N pgfplotstableset{
highlightrow={__parse_expr:n { l__i_int }}{ exp_not:n { #3 } }
}
}
tl_use:N l_tempa_tl
int_incr:N l__i_int
}
}
}
ExplSyntaxOff
begin{document}
rowstyle[11][#1*(#1 + 3)/2]{color{red}}
rowstyle[11][(#1 + 1)*4]{color{blue}bfseries}
pgfplotstabletypeset[string type]{
A B C
0 x x
1 x x
2 x x
3 x x
4 x x
5 x x
6 x x
7 x x
8 x x
9 x x
10 x x
11 x x
}
end{document}
Correct answer by ZhiyuanLck on March 26, 2021
With the tip highlightrow/.list/.expand once=List
I use a pgfmath-solution:
documentclass{article}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}
pgfplotstableread[]{
A B C
0 x x
1 x x
2 x x
3 x x
4 x x
5 x x
6 x x
7 x x
8 x x
9 x x
10 x x
11 x x
}{mytable}
begin{document}
pgfplotstablegetrowsof{mytable}
pgfmathsetmacroRows{int(pgfplotsretval-1)}
Rows: Rows.
% Solution of Rows = n*(n+3)/2
pgfmathsetmacroN{int(floor(0.5*(sqrt(8*Rows+9)-3)))}
Maximal n: N=N
% List:
letList=empty% create List
foreach n in {0,...,N}
{%
pgfmathparse{int(n*(n+3)/2}%
ifxemptyList{} xdefList{pgfmathresult}%
else xdefList{List,pgfmathresult}%
fi
}
List: List
pgfplotstableset{
highlightrow/.style={
postproc cell content/.append code={
count0=pgfplotstablerow
advancecount0 by0
ifnumcount0=#1
% pgfkeysalso{@cell content/.add={$bf}{$}}
pgfkeysalso{@cell content=color{red}textbf{##1}}%
fi}, },
}
pgfplotstabletypeset[string type,
%highlightrow=0,%
highlightrow/.list/.expand once=List,
]{mytable}
end{document}a
Answered by cis on March 26, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP