zhiburt / tabled

An easy to use library for pretty print tables of Rust structs and enums.
MIT License
1.95k stars 79 forks source link

Table to LaTeX #330

Open koenichiwa opened 1 year ago

koenichiwa commented 1 year ago

I was curious if the table output could be in LaTeX form.

Taking some inspiration from pandas DataFrame.to_latex(), you could have an output similar to:

\begin{tabular}{lrr}
    \toprule
    {} &  A & B \\
    \midrule
    X &  1 &  2 \\
    Y &  3 &  4 \\
    \bottomrule
\end{tabular}

This is just a quick example, and you might want to add some spacing in some cases in between the columns to make it humanly readable.

It does expect the end user to use booktabs as a LaTeX package because of \toprule and friends. But, if pandas can get away with it, I don't see why this library wouldn't either. We could also swap them for \hline.

NB that this creates a tabular. To use it in a LaTeX file, the end user would generally pack it in a table like so:

\begin{table}[]
    \centering
    \begin{tabular}{rr}
        \toprule
        A &  B \\
        \midrule
        1 &  2 \\
        \bottomrule
    \end{tabular}
    \caption{My super simple example}
    \label{tab:simple-example}
\end{table}

My gut feeling says that it's best to leave the more serious aspects of styling to the end user, and thus stick to creating tabulars. Namely because it's impossible to know which packages the user is already using and extra styling might create extra (possibly incompatible) dependencies. This would just leave the library in control of the amount of rows/columns, and the alignment of the cells.

On the other hand this is a library for pretty printing tables, and it would also be possible to create the full table.

But let me know your thoughts on that last part.

zhiburt commented 1 year ago

@scirin

zhiburt commented 1 year ago

Hi @koenichiwa

Thanks for the proposal. (I didn't answered back then cause your description was more than enough).

So pandas has a switch to chose between longtable and booktabs we can do it too (maybe there more popular extensions?).

My gut feeling says that it's best to leave the more serious aspects of styling to the end user, and thus stick to creating tabulars. Namely because it's impossible to know which packages the user is already using and extra styling might create extra (possibly incompatible) dependencies. This would just leave the library in control of the amount of rows/columns, and the alignment of the cells.

On the other hand this is a library for pretty printing tables, and it would also be possible to create the full table.

Exactly; We could add some things (probably index/colors/formatting/padding/margin/span maybe more?) but it would be hard to do a comprehensive styling.

I just wonder how it would be possible. In HTML for example we can add id, class tags so a user could tweak styles in its own CSS block. But I guess it's not possible with LaTeX.

As an option custom visitor function could be exposed to change built latex tree.

Otherwise I guess editing it manually does not make a lot of sense.


Implementation details;

I believe it shall be something similar to https://github.com/zhiburt/tabled/tree/master/table_to_html

PS: Not an expert in LaTex.