[ANN] PrettyTables.jl - Print formatted tables in Julia

We now have HTML support in PrettyTables (although needs some improvements), and LaTeX support should come next :slight_smile:

6 Likes

Great! I was trying something like:

pretty_table(df) |> x->latexify(x,env=:table)

where df is a dataframe, but no luck…

Is the plan to translate directly to \LaTeX, or go via something like latexify?

I look forward to this! If I can, say, set up data in csv files and import them into, say, dataframes, and turn them into \LaTeX tables, that would be really nice.

It would also be great if one has the option to choose between simple one page tables, and multi page tables (for, say, thermodynamic data).

Anyway, great work.

My actual plan is to provide a native translation to LaTeX like the HTML backend.

Good! This should be quite easy (I think) when the backend is done.

Thanks! :slight_smile:

1 Like

For a more low-level approach, there is

3 Likes

I wrote a very basic function to quickly duplicate the prettytables output with a latex one.


Just in case it could be useful, here is the code

Summary
using Formatting 

function format_string(a::String)
    sa=replace(a,"_"=>"\\_")
    "\\texttt{"*sa*"}"
end

format_string(a::Float64) = format_string(sprintf1("%5.3E",a))
format_string(a) = format_string(string(a))

function latex_table(table,header)
    result=Vector{String}()
    nrows,ncols=size(table)
    push!(result," \\begin{tabular}{"*repeat("l",ncols)*"}")
    push!(result,"\\toprule")

    line=""
    for j in 1:ncols
        line=line*format_string(header[j])
        if j!=ncols
            line*="&"
        else
            line*="\\\\"
        end
    end
    push!(result,line)
    push!(result,"\\midrule")

    # push!(result,line)
    for i=1:nrows
        line=""
        for j in 1:ncols
            line=line*format_string(table[i,j])
            if j!=ncols
                line*="&"
            else
                line*="\\\\"
            end
        end
        push!(result,line)

    end
    push!(result,"\\bottomrule")
    push!(result," \\end{tabular}")
    for rows in result
        println(rows)
    end
    result
end
14 Likes

Would it be simple to change floating point numbers, say 3.215E-01, to math notation like 3.215\cdot 10^{-1}, as an option?

I am not sure that this is what you had in mind… I tend to prefer the previous one reminding me the good old F77 output :wink:

Summary
function format_string(a::Float64)
    sa=sprintf1("%5.3E",a)
    sa=replace(sa,"E"=>"\\cdot 10^{")
    sa*="}"
    sa=replace(sa,"\\cdot 10^{+00}"=>"")
    sa=replace(sa,"{+0"=>"{")
    sa=replace(sa,"{+"=>"{")
    sa=replace(sa,"{-0"=>"{-")

    "\$\\mathtt{$sa}\$"
end

Anyway, the code must be considered as a quick&dirty workaround helping me wait until the actual PrettyTables latex output is ready :wink:

4 Likes

Looks good. At least, a nice option. (similar for 32bit? 1.3f-2, etc…?)

Sure: it should be straightforward to adapt this format function to your needs. It is also probable that Latexify.jl or Formatting.jl provides a more general solution for math notations.

I am wondering how can we support LaTeX in PrettyTables.jl. Should it use the default tabular environment or the more advanced tabularx environment?

1 Like

There is a Stata package for making nice latex tables from arbitrary matrices called frmttable. I used it a lot in my last job and think it’s a good place to look for implementation.

2 Likes

To my knowledge, longtable is the only one that will play nice with page breaks; https://github.com/tpapp/LaTeXTabulars.jl/blob/master/src/LaTeXTabulars.jl#L198.

2 Likes

OK, I can add an option to select which environment the user wants and support tabular and longtable.

2 Likes

Multiple, nested headers would probably the pie-in-the sky ideal functionality to see in a tables package. But it would probably be far too much to implement in a first pass at latex output.

1 Like

Indeed, I have tried to implement this feature for text output (https://github.com/ronisbr/PrettyTables.jl/issues/12). However, in that case, it is very difficult since you need to take care of the intersections. In LaTeX it will be much easier, but I think it is better to have a initial, simple version first :slight_smile:

2 Likes

Finally we have a initial version to work with.

@pdeffebach I will appreciate if you can provide some feedback :slight_smile:

4 Likes

I have used PrettyTables in my econometrics functions, which gives a nice standardized appearance, and I love the highlighters. At the moment, I’m highlighting p-values, but I may find more uses. :+1:

13 Likes

This is awesome! I am glad it is being useful :slight_smile:

I’d like to format integers to be better readable, e.g. 1,234,567 instead of 1234567. Is that somehow possible with PrettyTables?

1 Like

Yes! You can use the formatters with the correct configuration (see Formatting.jl):

julia> data = rand(10,4)*1_000_000;

julia> pretty_table(data, formatters = ft_printf("%'f"))
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Col. 1 β”‚         Col. 2 β”‚         Col. 3 β”‚         Col. 4 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  55,663.471352 β”‚ 565,113.348576 β”‚ 228,213.209548 β”‚ 596,933.831667 β”‚
β”‚ 598,850.511318 β”‚ 918,248.720496 β”‚ 808,481.418316 β”‚ 754,614.846689 β”‚
β”‚ 640,977.492383 β”‚ 626,533.558765 β”‚ 980,038.274199 β”‚ 610,184.850254 β”‚
β”‚ 287,743.186246 β”‚  76,687.541499 β”‚ 161,030.570764 β”‚ 240,375.325876 β”‚
β”‚ 857,151.015773 β”‚  11,965.878824 β”‚ 981,600.033788 β”‚ 477,896.870375 β”‚
β”‚ 221,924.099382 β”‚ 547,931.680008 β”‚ 747,265.226244 β”‚  98,563.283496 β”‚
β”‚ 248,303.297361 β”‚ 358,127.611747 β”‚ 823,934.355781 β”‚ 709,068.229827 β”‚
β”‚ 879,280.015089 β”‚ 916,188.034335 β”‚ 750,692.341263 β”‚ 635,150.198794 β”‚
β”‚ 170,127.288886 β”‚  74,977.650040 β”‚ 889,831.593324 β”‚ 250,795.355161 β”‚
β”‚ 304,763.211156 β”‚ 746,721.721851 β”‚ 220,722.897752 β”‚ 143,547.720656 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
9 Likes