Forgot password
 Register account
View 304|Reply 0

用MMA制作带有背景色的表格

[Copy link]

461

Threads

953

Posts

4

Reputation

Show all posts

青青子衿 posted 2025-6-5 15:19 |Read mode
  1. aX[n_] :=
  2.   If[n < 1, 0,
  3.    1 + Sum[MoebiusMu[d]^2 n/d/12 - EulerPhi[GCD[d, n/d]]/2, {d,
  4.       Divisors@n}] -
  5.     Count[(#^2 - # + 1)/n & /@ Range[n], _?IntegerQ]/3 -
  6.     Count[(#^2 + 1)/n & /@ Range[n], _?IntegerQ]/4];
  7. data = Table[aX[n], {n, 1, 105}];
  8. (*创建两列表格,第一列是编号(从0开始),第二列是数据*)
  9. table = Transpose[{Range[1, Length[data]], data}];
  10. (*定义样式函数:值为1的行蓝色背景,值为2的行黄色背景*)
  11. styleFunction[row_] :=
  12.   If[row[[2]] == 1, {Item[row[[1]], Background -> None],
  13.     Item[row[[2]], Background -> LightBlue]},
  14.    If[row[[2]] == 2, {Item[row[[1]], Background -> None],
  15.      Item[row[[2]], Background -> Yellow]}, row (*其他情况不变*)]];
  16. (*应用样式并添加表头*)
  17. styledTable = styleFunction /@ table;
  18. fullTable =
  19.   Prepend[styledTable,
  20.    Item[#, Background -> LightGray] & /@ {"编号", "值"}];
  21. (*显示表格*)
  22. Grid[fullTable, Frame -> All, Spacings -> {1, 1} (*调整单元格间距*)]
Copy the Code

Quick Reply

Advanced Mode
B Color Image Link Quote Code Smilies
You have to log in before you can reply Login | Register account

$\LaTeX$ formula tutorial

Mobile version

2025-6-8 17:17 GMT+8

Powered by Discuz!

Processed in 0.015278 second(s), 22 queries

× Quick Reply To Top Edit