[ANN] PrettyTables.jl now has LaTeX backend

Hi!

After a while, I finally added an initial version o LaTeX backend to 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:

\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> 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:

34 Likes

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.

1 Like

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!

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.

1 Like

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

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

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

# Markdown

This is a markdown table.

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

after

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 :smiley:

4 Likes

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?

Yes! It should grab the newer commits.

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

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.

1 Like

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.

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

Nice! Thanks for testing.

Indeed, documentation is my next task :slight_smile:

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.

1 Like

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!

1 Like

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 |

Well, not easily :smiley: You can do this using:

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

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

1 Like

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

2 Likes

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

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

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

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