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

**URL:** <https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550>\
**Category:** Package Announcements\
**Created:** [January 12, 2019, 3:49am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550 "2019-01-12T03:49:21Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![sbk](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@sbk](https://discourse.julialang.org/u/sbk)\
**Post date:** [April 24, 2019, 7:29pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/61 "2019-04-24T19:29:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [April 24, 2019, 7:54pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/62 "2019-04-24T19:54:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 24, 2019, 9:38pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/63 "2019-04-24T21:38:02Z")

</div>

Can you get the string representations and manipulate those?

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [April 24, 2019, 9:43pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/64 "2019-04-24T21:43:38Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [December 1, 2019, 9:12pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/65 "2019-12-01T21:12:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 1, 2019, 9:59pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/66 "2019-12-01T21:59:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [December 1, 2019, 10:55pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/67 "2019-12-01T22:55:33Z")

</div>

Great! I was trying something like:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 2, 2019, 12:03am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/68 "2019-12-02T00:03:16Z")

</div>

> [@BLI](#):
>
> Is the plan to translate directly to LATEX\LaTeX , or go via something like `latexify` ?

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

> [@BLI](#):
>
> 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).

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

> [@BLI](#):
>
> Anyway, great work.

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 2, 2019, 6:33am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/69 "2019-12-02T06:33:58Z")

</div>

> [@LaurentPlagne](#):
>
> get a latex output to be inserted in my latex manual

For a more low-level approach, there is

> **[GitHub - tpapp/LaTeXTabulars.jl: Write tabular data from Julia in LaTeX format.](https://github.com/tpapp/LaTeXTabulars.jl)**
>
> Write tabular data from Julia in LaTeX format. Contribute to tpapp/LaTeXTabulars.jl development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [December 6, 2019, 3:06pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/70 "2019-12-06T15:06:45Z")

</div>

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

 ![pt](https://global.discourse-cdn.com/julialang/original/3X/b/1/b135bbcb9ea8e8a49d05cfbbb6f9bb81a69e6f95.png)  
 ![latex](https://global.discourse-cdn.com/julialang/original/3X/b/d/bd739dfae7a984dc0eb423ff8f4b760574b6c23e.png)

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

> **Summary**
>
> ```julia
> 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
> 
> ```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [December 6, 2019, 8:43pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/71 "2019-12-06T20:43:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [December 6, 2019, 11:31pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/72 "2019-12-06T23:31:12Z")

</div>

> [@BLI](#):
>
> 3.215E-01

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 😉

 ![latex2](https://global.discourse-cdn.com/julialang/original/3X/1/c/1c601149afc4db5f204ab2af22c55be4afca0e9b.png)

> **Summary**
>
> ```julia
> 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 😉

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [December 7, 2019, 7:59am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/73 "2019-12-07T07:59:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [December 7, 2019, 9:15am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/74 "2019-12-07T09:15:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 7, 2019, 1:39pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/75 "2019-12-07T13:39:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 7, 2019, 1:57pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/76 "2019-12-07T13:57:13Z")

</div>

There is a Stata package for making nice latex tables from arbitrary matrices called [frmttable](https://www.pdx.edu/econ/sites/www.pdx.edu.econ/files/frmttable_sj.pdf). I used it a lot in my last job and think it’s a good place to look for implementation.

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [December 7, 2019, 4:53pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/77 "2019-12-07T16:53:52Z")

</div>

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](https://github.com/tpapp/LaTeXTabulars.jl/blob/master/src/LaTeXTabulars.jl#L198).

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 7, 2019, 5:05pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/78 "2019-12-07T17:05:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 7, 2019, 6:06pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/79 "2019-12-07T18:06:37Z")

</div>

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.

 ![Screenshot from 2019-12-07 13-04-52](https://global.discourse-cdn.com/julialang/original/3X/e/4/e469607f5ec2e635209385ab01f2a716842e92d4.png)

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 7, 2019, 6:11pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550/80 "2019-12-07T18:11:11Z")

</div>

Indeed, I have tried to implement this feature for text output ([https://github.com/ronisbr/PrettyTables.jl/issues/12](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 🙂

[Previous page](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550.md?page=3)

[Next page](https://discourse.julialang.org/t/ann-prettytables-jl-print-formatted-tables-in-julia/19550.md?page=5)
