Wrap title in PrettyTables v3?

Is it possible to wrap a long title in version 3 of PrettyTables.jl? I was able to do this in version 2 with the text backend by inserting newlines in the title where I wanted the line break. This no longer works in version 3. Instead “\n” is printed inline with the text of the title. I’ve also tried creating a vector of Strings for the desired multiple title lines, but this results in a MethodError.

Have you tried using title and subtitle?

See (closed) issue here: Newlines in title display differently in PrettyTables v2 vs v3 · Issue #268 · ronisbr/PrettyTables.jl · GitHub

Perhaps reopen?

1 Like

Thanks. I can make it work by adding the second line as the subtitle. Right now it produces

      CWG: m ∈ 1:3, a = 1 m, f = 200 MHz, σ = 58 MS m^-1,
           Rq = 1.0e-6 inch, ϵᵣ = 1.001, tanδ = 0.003
┌──────┬───┬───┬──────────────┬──────────────────┬─────────────┐
│ Type │ m │ n │ Cutoff Freq. │ Guide Wavelength │ Attenuation │
│      │   │   │        [MHz] │              [m] │      [dB/m] │
├──────┼───┼───┼──────────────┼──────────────────┼─────────────┤
│   TE │ 1 │ 1 │    87.805342 │           1.6675 │      0.0609 │
│   TE │ 2 │ 1 │    145.65538 │           2.1862 │      0.0799 │
│   TM │ 1 │ 1 │    182.73257 │           3.6857 │      0.1346 │
│   TE │ 3 │ 1 │    200.35307 │          63.5878 │      2.3265 │
│   TM │ 2 │ 1 │    244.91584 │         704.8769 │     25.7471 │
│   TE │ 1 │ 2 │    254.25444 │         781.9918 │     28.5928 │
│    ⋮ │ ⋮ │ ⋮ │            ⋮ │                ⋮ │           ⋮ │
└──────┴───┴───┴──────────────┴──────────────────┴─────────────┘

where the title is displayed bold and the subtitle isn’t. I’ll have a look at the docs to see how to control the appearance of the subtitle to match that of the title.

Edit: I used the following to embolden the subtitle:

using Crayons: @crayon_str
...
        p = pretty_table(
            modedata;
            style = TextTableStyle(; title = crayon"bold", subtitle = crayon"bold"),
            ...

It took me a while to understand that the many uses of the crayon string macro sprinkled throughout the PrettyTables.jl documentation required adding the Crayons package. I couldn’t find where that was mentioned.

Hi @PeterSimon !

This is a bug! PrettyTables.jl should re-export Crayons.jl. I will fix it.

Since this is the second time I was asked to allow for multiple lines in the title, I will check if it is ok to implement it.

3 Likes

Thanks! I suggest providing a link to the Crayons package documentation in the PrettyTables documentation, so that people can see what a Crayon is and how versatile they are.

That would be great! If possible, I suggest allowing the user to specify a vector of strings to represent multiple title lines, to be consistent with the way that multiple column headers can be specified.

1 Like

Couldn’t PrettyTables auto-wrap the title?

1 Like

Regarding Crayons.jl versus StyledStrings.jl, what is the recommended method to add colors to strings? I thought PrettyTables.jl was moving to StyledStrings.jl as it is now part of the language features.

You can use both! Crayons is a good way to color the cells using highlighters or with the default configuration options. If you use a StyledString, its color will be applied to the cell automatically.

Oh so Crayons.jl can also be used to color visual elements (e.g., cell) besides the contents of the cell (i.e., text), and that is the main reason to keep it as an option, right?

Actually Crayons is how we represent decorations in the text backends. Hence, all highlighters and table colors are represented using a Crayon. However, if a styled string is present inside a cell, the cell will be printed using its colors.

1 Like