ronisbr / PrettyTables.jl

Print data in formatted tables.
MIT License
404 stars 38 forks source link

Use with Latexify #180

Closed briochemc closed 2 years ago

briochemc commented 2 years ago

MWE:

using PrettyTables
using Latexify
c1 = [latexify("α"), latexify("β")]
c2 = [0.0, 1.0]
pretty_table([c1 c2], backend = Val(:latex))

where I have turned the "α" and "β" into LaTeXStrings so under the hood their backslashes are automatically escaped.

However this interferes with pretty_table, which replaces those escaped backslashes with \textbackslash{}, see output below:

\begin{tabular}{rr}
  \hline
  \textbf{Col. 1} & \textbf{Col. 2} \\\hline
  \$\textbackslash{}alpha\$ & 0.0 \\
  \$\textbackslash{}beta\$ & 1.0 \\\hline
\end{tabular}

Not sure what the fix is. Maybe this is just a matter of specializing something on LaTeXStrings for which "\\" should be left untouched?

ronisbr commented 2 years ago

Hi @briochemc!

Yes, sorry about that. This new behavior is a breaking change of v2. We needed to escape all LaTeX characters otherwise the output can break depending on what the user is inputing. To circumvent this, we created the type LatexCell. If you wrap a value using this structure, PrettyTables will not escape the output. However, you must ensure that it is a valid LaTeX sequence that can be used inside a table cell.

julia> c1 = LatexCell.([latexify("α"), latexify("β")]);

julia> c2 = [0.0, 1.0];

julia> pretty_table([c1 c2], backend = Val(:latex))
\begin{tabular}{rr}
  \hline
  \textbf{Col. 1} & \textbf{Col. 2} \\\hline
  $\alpha$ & 0.0 \\
  $\beta$ & 1.0 \\\hline
\end{tabular}
briochemc commented 2 years ago

Oh I see, great! What about adding this as an example in the docs about LaTeX?

ronisbr commented 2 years ago

Yes! Documentation is something we need to improve in this package. Can you submit a PR please?