0

Please consider the following example. It is to color coordinate table cell contents.

| Name | Age | Color |
|------+-----+-------|
| John |  25 | red   |
| Mary |  30 | green |
| Tom  |  40 | blue  |

#+TBLFM: $3='(progn (cond ((string= $3 "red") "[:foreground \"red\"]")
                       ((string= $3 "green") "[:foreground \"green\"]")
                       ((string= $3 "blue") "[:foreground \"blue\"]")
                       (t "")))::

It does not seem to work. How can I get it to work?

What's the simplest way to colorize text in an org-mode table cell depending on say a threshold value in the cell? Alternative solutions welcome. It can't be this difficult.

Something along these lines does not seem to work either:

| Column 1 | Column 2 |
|----------+----------|
| foo      |       42 |
| bar      |       69 |
| baz      |     1337 |
|----------+----------|
#+TBLFM: $2='(format "[%s]" (propertize $2 'font-lock-face '(:foreground "red")))

The format of the cell content changes but not the color. WTH??!!

Rather than spending an inordinate amount of time on this seemingly trivial problem, I got the above examples from ChatGPT. None of which work. Hmm.. I now have a collection of such examples all from various queries of ChatGPT to try and none of them work.

  • 2
    Showing code that does not work does not tell what you are looking for. You need to say what you expect to happen. In particular, what does "color coordinate table cell contents" mean? It appears that you want something in the first row to be colored red, something in the second row to be colored green and so on. But it is not clear what that something is supposed to be: every cell in the row? the cell in the third column of the row? something else? – NickD Apr 30 '23 at 17:24
  • Isn't it obvious what this example ought to do? The table formula (TBLFM) applied to the 3rd column should yield colored text in the respective rows. It's not happening. –  Apr 30 '23 at 17:47
  • 2
    Not to me. As I said, showing non-working code without explaining what it is supposed to do is worse than useless: one has to figure out what you want (mostly by guessing at this point) and then figure out how to modify the code to do that; if one guesses wrong about what you want (as I did in your previous question), then I can go down the wrong path for quite a while before realizing that you meant something else. So by all means, include what you tried: but first explain what you want to accomplish as completely and as precisely as possible. – NickD Apr 30 '23 at 18:06
  • 1
    See https://emacs.stackexchange.com/questions/7375/can-i-format-cells-in-an-org-mode-table-differently-depending-on-a-formula and https://emacs.stackexchange.com/questions/66708/how-to-format-cells-of-orgmode-table-with-colors-according-to-its-string. – John Kitchin May 01 '23 at 12:55
  • I'm aware of those threads. The solutions are so dastardly complicated. I wish to give the question another shot. I'm looking for a very simple solution. –  May 01 '23 at 12:58
  • "WTH??!!" - automatic fontification of the table and its contents overrides any built-in fontification you do to the string. Try `(setq s (propertize "FOO" 'font-lock-face '(foreground :red)))` and then `M-: (insert s)` in various places: outside the table you get a red `FOO` but inside the table you get a `FOO` with the `org-table` face. – NickD May 11 '23 at 15:35
  • I want to apply this to a table formula TBLFM so that I can control which columns and rows get colored. I'm not too enthusiastic about setting global variables I also want to forgo having to enable the setting manually for each cell. –  May 11 '23 at 18:16
  • The problem is that no matter what you do with the `#+TBLFM` line, if somebody comes later and decides to paint over what you so carefully painted, there is nothing you can do. And that's what happens here: after the formulas have been evaluated, Org mode comes along and re-aligns the table (by calling `org-table-align`) and that . I don't know of any way to do what you ask, not without forking the Org mode code. – NickD May 13 '23 at 03:12
  • Indeed, if you disable font-lock-mode, then the second example, with font-lock-face replaced by face, works fine. Although it is nice that org-table gets fontified, I think it does not justify 'breaking' this quite nice functionality. At first sight, it anyway seems a little strange choice, to make fontification 'have priority over' custom face properties. Maybe I just don't fully understand things, but otherwise, I guess this would make a nice discussion/bug report. – dalanicolai May 13 '23 at 07:35

0 Answers0