# \[ANN\] PrettyTables.jl now has LaTeX backend

**URL:** <https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990>\
**Category:** Community\
**Created:** [December 7, 2019, 11:05pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990 "2019-12-07T23:05:19Z")\
**Posts on this page:** 20\
**Page:** 1

<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, 11:05pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/1 "2019-12-07T23:05:19Z")

</div>

Hi!

After a while, I finally added an initial version o LaTeX backend to [PrettyTables.jl](https://github.com/ronisbr/PrettyTables.jl). It is not published yet. Thus you will have to use `dev`.

I want some feedback about it. Unfortunately there is not a documentation yet, but you can see the functions `pretty_tables` and `_pt_latex` to see.

I also added a nice feature to automatically add tables to files. For example, let’s say you have this LaTeX file:

```plaintext
\documentclass{article}
\usepackage{xcolor}

\begin{document}

The table~\ref{tab:table} is a table "printed" with the Julia package PrettyTable.

\begin{table}[h]
\caption{A table.}
\centering
\label{tab:table}
% <PrettyTables Table 1>
% </PrettyTables>
\end{table}

\end{document}

```

Then, you can add a table using PrettyTables.jl to the marked location by:

```julia
julia> using PrettyTables

julia> using Statistics

julia> data = rand(15,3)*1e8;

julia> m = mean(data);

julia> hl1 = LatexHighlighter((data,i,j)->data[i,j] > m, "color{blue}");

julia> hl2 = LatexHighlighter((data,i,j)->data[i,j] < m, "color{red}");

julia> include_pt_in_file("example.tex", "Table 1", data, backend = :latex, formatter = ft_latex_sn(3), alignment = :l, highlighters = (hl1,hl2))

```

Leading to the following PDF if `example.tex` is compiled:

 ![Captura de Tela 2019-12-07 às 20.02.57](https://global.discourse-cdn.com/julialang/original/3X/8/6/867616d4cd089e7107e47abcfb9e23f788995670.png)

---

<div class="post-metadata">

**Author:** ![venuur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/venuur/32/6493_2.png) [@venuur](https://discourse.julialang.org/u/venuur)\
**Post date:** [December 9, 2019, 10:26am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/2 "2019-12-09T10:26:06Z")

</div>

Very cool! Do you have something like `include_pt_in_file` for the HTML backend? It’s be handy for some of my markdown and HTML reports.

---

<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 9, 2019, 11:11am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/3 "2019-12-09T11:11:17Z")

</div>

Thanks!

It works with any backend! Notice that it is still experimental. I am having some problems with `libuv` in Windows. Please, let me know if you need some modifications!

---

<div class="post-metadata">

**Author:** ![venuur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/venuur/32/6493_2.png) [@venuur](https://discourse.julialang.org/u/venuur)\
**Post date:** [December 11, 2019, 8:26am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/4 "2019-12-11T08:26:27Z")

</div>

Super cool! I just tried it today. There are two features that’d be helpful. The first is most important, the second is just a convenience.

1. The HTML backend outputs a self-contained HTML page. It’d be nice to just get the `<table>...</table>` element. My use case often requires me to copy the HTML directly into a wiki system that only lets me access the body element of an HTML document.
2. Rather than an HTML style tag for marking in `include_pt_in_file`, it’d be nice to specify a literal string to replace the whole mark with the table. This is handy for Markdown where there’s no comment syntax. Then I could create a deploy function that copies my file and then runs the appropriate `include_pt_in_file` commands.

I’ll still try to use it as is if you don’t get time to implement these. I can probably implement both of these functions as post processing with some thought.

I can also open an issue on your repo if that’s more convenient for you.

---

<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 11, 2019, 1:22pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/5 "2019-12-11T13:22:13Z")

</div>

> [@venuur](#):
>
> The HTML backend outputs a self-contained HTML page. It’d be nice to just get the `<table>...</table>` element. My use case often requires me to copy the HTML directly into a wiki system that only lets me access the body element of an HTML document.

Done! If you use `master`, then you will see that the HTML back-end now has a keyword `minimal` that suppress the entire header, leaving only the content between `<table>...</table>` (with those tags).

> [@venuur](#):
>
> Rather than an HTML style tag for marking in `include_pt_in_file` , it’d be nice to specify a literal string to replace the whole mark with the table. This is handy for Markdown where there’s no comment syntax. Then I could create a deploy function that copies my file and then runs the appropriate `include_pt_in_file` commands.

Done, but in a slight different way. The function `include_pt_to_file` now has the option `remove_tags` that include the table and remove the tags. Then:

```markdown
# Markdown

This is a markdown table.

<PrettyTables Table 1> This should be removed.
This should be removed.
This should be removed.
This should be removed.
This should be removed.
</PrettyTables>

```

becomes

```mardown
# Markdown

This is a markdown table.

| Col. 1 | Col. 2 | Col. 3 |
|--------|--------|--------|
| 1 | 2 | 3 |
| 4 | 5 | 6 |

```

after

```julia
include_pt_in_file(path, "Table 1", data_table_1, tf = markdown, remove_tags = true)

```

I would appreciate if you can test those functionality before a tag a new release 😃

---

<div class="post-metadata">

**Author:** ![venuur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/venuur/32/6493_2.png) [@venuur](https://discourse.julialang.org/u/venuur)\
**Post date:** [December 11, 2019, 2:04pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/6 "2019-12-11T14:04:05Z")

</div>

Sure, I’ll give it a stab at work in the morning. I already did `dev PrettyTables` will `up PrettyTables` be enough to get the new version?

---

<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 11, 2019, 2:54pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/7 "2019-12-11T14:54:28Z")

</div>

Yes! It should grab the newer commits.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [December 11, 2019, 3:47pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/8 "2019-12-11T15:47:36Z")

</div>

> [@Ronis\_BR](#):
>
> minimal

maybe consider `standalone=false`? that would match what I’ve seen elsewhere for this kind of feature and maybe is a bit more descriptive

---

<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 11, 2019, 4:13pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/9 "2019-12-11T16:13:26Z")

</div>

Fair enough! I just submitted a commit changing `minimal` to `standalone`. The boolean logic is also inverted, to have only the table, the user must pass `standalone = false`. The default is `true`.

---

<div class="post-metadata">

**Author:** ![venuur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/venuur/32/6493_2.png) [@venuur](https://discourse.julialang.org/u/venuur)\
**Post date:** [December 11, 2019, 11:16pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/10 "2019-12-11T23:16:30Z")

</div>

Works like a charm!

You should document that `include_pt_in_file` creates a backup file. I was surprised when I found the extra `FILE_backup` in my directory.

It’s a good idea especially with the remove tags feature being a destructive edit.

---

<div class="post-metadata">

**Author:** ![venuur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/venuur/32/6493_2.png) [@venuur](https://discourse.julialang.org/u/venuur)\
**Post date:** [December 12, 2019, 12:15am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/11 "2019-12-12T00:15:27Z")

</div>

Also, how did you get your markdown table. I couldn’t figure it out.

---

<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 12, 2019, 12:39am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/12 "2019-12-12T00:39:38Z")

</div>

> [@venuur](#):
>
> Works like a charm!

Nice! Thanks for testing.

> [@venuur](#):
>
> You should document that `include_pt_in_file` creates a backup file. I was surprised when I found the extra `FILE_backup` in my directory.

Indeed, documentation is my next task 🙂

> [@venuur](#):
>
> Also, how did you get your markdown table. I couldn’t figure it out.

You can just use the text back-end (default) with `tf = markdown`. However, I am planning to release an special back-end of Markdown to handle alignments and modifiers such as bold, italics, etc.

---

<div class="post-metadata">

**Author:** ![venuur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/venuur/32/6493_2.png) [@venuur](https://discourse.julialang.org/u/venuur)\
**Post date:** [December 12, 2019, 1:01am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/13 "2019-12-12T01:01:53Z")

</div>

> [@Ronis\_BR](#):
>
> You can just use the text back-end (default) with `tf = markdown` . However, I am planning to release an special back-end of Markdown to handle alignments and modifiers such as bold, italics, etc.

Thanks! I had tried to mix the HTML backend with `tf = markdown` which obviously didn’t work. A dedicated Markdown backend sounds really convenient. Looking forward to the next iteration!

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [January 12, 2020, 2:05am UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/14 "2020-01-12T02:05:37Z")

</div>

Is there any way this could be made to display a matrix of matrices in a notebook?  
Something like 3 matrices A B and C with cols and rows aligned across all matrices:  
\quad A  
B \, C

or maybe a table with boxes around the matrices:  
\quad | A |  
| B | C |

---

<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:** [January 12, 2020, 1:35pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/15 "2020-01-12T13:35:09Z")

</div>

Well, not easily 😃 You can do this using:

```julia
using PrettyTables
data = [1 1; 1 1]

io = IOBuffer()
pretty_table(io, data, backend = :html, standalone = false)
str1 = String(take!(io))

io = IOBuffer()
pretty_table(io, data, backend = :html, standalone = false)
str2 = String(take!(io))

io = IOBuffer()
pretty_table(io, data, backend = :html, standalone = false)
str3 = String(take!(io))

mat = ["" str1; str2 str3]
mat = replace.(mat, "\n" => "")

pretty_table(mat, noheader = true, backend = :html)

```

which is more or less printing 3 HTML tables to `String` and merging them into a matrix of string. Finally, this matrix is printed to the `stdout`.

This leads to the following result in Jupyter:

![Captura de Tela 2020-01-12 às 10.34.44](https://global.discourse-cdn.com/julialang/original/3X/9/c/9cf62bb4468329a573b32354ff4ba1964d1ccdfe.png)

EDIT: Much better would be to remove all formatting in the last print to avoid having lines with different colors.

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [January 12, 2020, 5:29pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/16 "2020-01-12T17:29:11Z")

</div>

Neat! That is better than what I had before! Thank you

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [January 12, 2020, 6:07pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/17 "2020-01-12T18:07:24Z")

</div>

Is there an easy way to render fractions in a table?

---

<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:** [January 12, 2020, 6:31pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/18 "2020-01-12T18:31:18Z")

</div>

In this case you must use LaTeX or Mathjax in HTML. However, I have no idea if this will work in a Jupyter notebook.

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [January 12, 2020, 6:33pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/19 "2020-01-12T18:33:23Z")

</div>

One package I am aware of is Latexify.jl  
I’ll need to experiment whether I can combine this!

---

<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:** [January 12, 2020, 6:35pm UTC](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990/20 "2020-01-12T18:35:27Z")

</div>

AFAIK Latexify.jl converts Julia object to LaTeX strings. In this case you can use PrettyTables with LaTeX backend.

[Next page](https://discourse.julialang.org/t/ann-prettytables-jl-now-has-latex-backend/31990.md?page=2)
