# [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

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!

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))

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

line=""
for j in 1:ncols
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

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

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

2 Likes

Finally we have a initial version to work with.

@pdeffebach I will appreciate if you can provide some feedback

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.

13 Likes

This is awesome! I am glad it is being useful

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