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

It’s not clear from the docs how to hcat tables.
If you mean to concat the arrays I’m passing as input, then I think that could be a good work around. But the two arrays have different shapes and I would end up with some zeros at certain locations ( not convenient but manageable).
Will try that for now, thanks.

Yes, the only way I see to make this work now is to hcat the arrays as @mkborregaard told. It will be really a huge modification to allow printing n-tables side by side because of how the printing system works.

If you really think this is a important feature to have, then, please, open an issue on Github.

1 Like

Can you get the string representations and manipulate those?

1 Like

Exactly. Should be pretty simple to split the strings on '\n' and then pad whichever array is shorter with strings of spaces of the length of the rest of the elments of the array. Then you can just smash those two arrays of strings together and use join.

2 Likes

Di you find a solution ? I have the same needs for a collection of practice sessions.
The Julia output is done via prettytables but I would like to get a latex output to be inserted in my latex manual.

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