PrettyTables linebreak in title

How do I make linebreaks in the header of a pretty_table? Here is a MEW of my attempt:

julia> using PrettyTables

julia> pretty_table(linebreaks=true,
           body_hlines = [1, 2, 3],
           header = ([" "                    "Actual\npositives" "Actual\nnegatives"],),
               [
                   "Prediced\npositives"        1          2
                   "Prediced\nnegatives"        3          4
                   " "                         5         6
               ]
       )
┌───────────┬───────────────────┬───────────────────┐
│           │ Actual\npositives │ Actual\nnegatives │
├───────────┼───────────────────┼───────────────────┤
│  Prediced │                 1 │                 2 │
│ positives │                   │                   │
├───────────┼───────────────────┼───────────────────┤
│  Prediced │                 3 │                 4 │
│ negatives │                   │                   │
├───────────┼───────────────────┼───────────────────┤
│           │                 5 │                 6 │
└───────────┴───────────────────┴───────────────────┘

Just a trick like yours with the row headers:

pretty_table(linebreaks=true,
                  body_hlines = [1, 2, 3],
                  noheader=true,
                      [
                          " " "Actual\npositives" "Actual\nnegatives"
                          "Prediced\npositives"        1          2
                          "Prediced\nnegatives"        3          4
                          " "                          5          6
                      ]
              )

headers (row+col) are part of the data, so newline is printed.

1 Like

Seems hacky, but it works like a charm!

1 Like

Actually linebreaks in headers is what I called subheader (lines below the headers):

pretty_table(linebreaks=true,
                  body_hlines = [1, 2, 3],
                  header = (["", "Actual", "Actual"], ["", "npositives", "nnegatives"]),
                      [
                          "Prediced\npositives"        1          2
                          "Prediced\nnegatives"        3          4
                          " "                         5         6
                      ]
              )

Captura de Tela 2022-03-17 às 09.11.07

3 Likes

By the way, if you want the same decoration, you can use AnsiTextCell:

julia> cb = crayon"bold";

julia> cg = crayon"dark_gray";

julia> pretty_table(linebreaks=true,
                  body_hlines = [1, 2, 3],
                  header = (["", "Actual", "Actual"], ["", "positives", "negatives"]),
                      [
                          AnsiTextCell("$(cb)Prediced\n$(cg)positives")  1          2
                          AnsiTextCell("$(cb)Prediced\n$(cg)negatives") 3          4
                          " "                                            5          6
                      ]
              )

Captura de Tela 2022-03-17 às 11.28.07

1 Like