PrettyTables.jl icon indicating copy to clipboard operation
PrettyTables.jl copied to clipboard

[FR]: maximum horizontal size of columns

Open BeastyBlacksmith opened this issue 3 years ago • 8 comments

Currently I am working with the latex backend and if entries get too long they don't fit the page. Thus it would be good to say, e.g. break lines if they exceed 3cm or similar.

I don't know how useful this would be for other backends.

BeastyBlacksmith avatar May 18 '21 17:05 BeastyBlacksmith

Hi @BeastyBlacksmith

Hum, it seems interesting! The problem is: I have no idea how to get the size of an entry.

One workaround should be changing the type of the column to p with a specific size. We need to think about a good API to do this. The implementation should be easy. Any ideas?

ronisbr avatar May 18 '21 17:05 ronisbr

I think we could extend alignment to accept things like :p => "3cm"

BeastyBlacksmith avatar May 18 '21 17:05 BeastyBlacksmith

It would not be nice because it will change the type of alignment. It is a keyword that is passed to all backends as a Vector{Symbol}. In this case, we might have a performance problem due to type instabilities.

ronisbr avatar May 18 '21 17:05 ronisbr

an alternative would be to introduce a new keyword column_widths being a Vector{String}

BeastyBlacksmith avatar May 18 '21 17:05 BeastyBlacksmith

Yes, it can be done! column_widths is not something common to all backends. What it should contain?

ronisbr avatar May 18 '21 18:05 ronisbr

So, I would map

pretty_table(df, backend = Val(:latex), alignment = :l, column_widths = "3cm") # \begin{tabular}{p{3cm}p{3cm}...}
pretty_table(df, backend = Val(:latex), alignment = :l, column_widths = ["4em", ""]) # \begin{tabular}{lp{4em}}
pretty_table(df, backend = Val(:latex), alignment = :r, column_widths = ["4em", ""]) # error, since p is left aligned

Does that make sense?

BeastyBlacksmith avatar May 18 '21 18:05 BeastyBlacksmith

Yes, seems good! I will just change the name to columns_width to keep it equal as in text backend.

ronisbr avatar May 19 '21 00:05 ronisbr

+1 for this! As a very hacky workaround, I am doing

# piracy
function PrettyTables._latex_alignment(s::Symbol)
    if (s == :l) || (s == :L)
        return "l"
    elseif (s == :c) || (s == :C)
        return "c"
    elseif (s == :r) || (s == :R)
        return "r"
    elseif s == :p2cm
        return "p{2cm}"
    else
        error("Invalid LaTeX alignment symbol: $s.")
    end
end

and then passing :p2cm as my alignment.

ericphanson avatar Jul 10 '23 12:07 ericphanson