Ask Your Question
1

Colors in table

asked 2020-11-27 09:52:46 +0100

Cyrille gravatar image

updated 2020-11-29 04:22:01 +0100

slelievre gravatar image

This is a nice table with the help of two of you (many thanks).

A = [[10] * 10, [100] + [0] * 9, [11.1] * 9 + [0]]
A.extend([[20] * 5 + [0] * 5, [50/2^n for n in range(10)]])
D = deepcopy(A)  # copie A dans D en conservant A intacte
# on arrondit uniquement les lignes qui en ont besoin
D[2] = list(matrix(RDF, A[2]).round(3)[0])
D[4] = list(matrix(RDF, A[4]).round(3)[0])
# on construit la table
hr = [r"${\color{red} n}$" for n in (1 .. 10)]
hc = ["", r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Équirépartition}}$", r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Tout pour un}}$", r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Un déshérité}}$"]
hc += [r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Injustice pour 1/2 population}}$", r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Injustice croissante}}$"]
t = table(D, header_row=hr, header_column=hc)    
show(t)

I added colors in the header row. But adding them in the header column does not work since the elements of the header need to be formatted. So what can I do? And if I want some background colors in the cells and change the colors of the cell's fonts?

edit retag flag offensive close merge delete

Comments

I don't know how to get \definecolor... to work, but you need to put braces {} around the text to be colored, as in \color{blue}{\text{...}}. (Note the "{" immediately before "\text".)

John Palmieri gravatar imageJohn Palmieri ( 2020-11-28 18:06:19 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2020-11-29 04:43:52 +0100

slelievre gravatar image

updated 2020-11-29 05:02:25 +0100

Presumably this takes place in a browser?

Maybe in Jupyter or in SageCell?

The following works in these contexts.

Making it work in the Sage REPL or in SageTeX would require some changes.

Not sure that fully answers your question, but hopefully it is a step.

A = [[10] * 10, [100] + [0] * 9, [100/9] * 9 + [0]]
A.extend([[20] * 5 + [0] * 5, [51200/1023/2^n for n in range(10)]])
f = lambda x: x if x in ZZ else round(x, 4)
D = [[f(a) for a in r] for r in A]
latex_req = r"$\require{color}$"
latex_def = r"$\definecolor{energy}{RGB}{200, 8, 21}$"
box = lambda t: r"$\color{energy}{\colorbox{yellow}" + fr"{{{t}}}" + "}$"
hr = [r"$\color{red}" + f"{{{n}}}$" for n in (1 .. 10)]
cc = ["Équirépartition", "Tout pour un", "Un déshérité"]
cc += ["Injustice pour 1/2 population", "Injustice croissante"]
hc = [latex_req + latex_def] + [box(c) for c in cc]
t = table(D, header_row=hr, header_column=hc)
t

Illustration

Inspired by the answer to this previous question:

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-11-27 09:52:46 +0100

Seen: 372 times

Last updated: Nov 29 '20