tarleb / parse-latex

Filter that will parse raw LaTeX snippets and include the results in the output.
MIT License
22 stars 5 forks source link

Example, using table caption, table number & cross-ref? #6

Open friendly opened 3 months ago

friendly commented 3 months ago

This is a great filter!

It would be very helpful if you included in the README or elsewhere a complete quarto example, including a table caption, table number, and ability to cross-reference that works with quarto.

For example, a LaTeX table I'd like to include in a Quarto doc originally (in .Rnw) looked like below. I'm not sure how to use your extension for this in .qmd.

\begin{table}[htb]
\renewcommand{\arraystretch}{1.6}
\caption{Multivariate test statistics as functions of the eigenvalues $\lambda_i$ solving $\det{\mat{H} - \lambda \mat{E}}=0$
or eigenvalues $\rho_i$ solving  $\detbracket{\mat{H} - \rho (\mat{H}+\mat{E})}=0$.
}\label{tab:criteria}
\begin{center}
\begin{tabular}{|l|l|l|l|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  Criterion & Formula &  Partial $\eta^2$   \\
  \hline
  Wilks's $\Lambda$ & $\Lambda = \prod^s_i \frac{1}{1+\lambda_i}$ &  $\eta^2 = 1-\Lambda^{1/s}$   \\
  Pillai trace & $V = \sum^s_i \frac{\lambda_i}{1+\lambda_i}$ &  $\eta^2 = \frac{V}{s} $   \\
  Hotelling-Lawley trace & $H = \sum^s_i \lambda_i$ & $\eta^2 = \frac{H}{H+s}$   \\
  Roy maximum root & $R = \lambda_1$  &  $ \eta^2 = \frac{\lambda_1}{1+\lambda_1}  \\
  \hline
\end{tabular}
\end{center}
\end{table}
tarleb commented 3 months ago

The table must be valid LaTeX and must be parsable by pandoc. To check the latter you can use quarto pandoc --from=latex and pass the table as input. If you add --mathml to that command, then you'll also receive warnings about equations that pandoc had trouble parsing.

To fix the given case above, just add the missing $ after the last formula in the table. The \mat and \detbracket commands are not standard LaTeX, the definitions of those macros must be part of the table.

friendly commented 3 months ago

I tried to use this in my Quarto book document, but I get no output for the table. I'm building an HTML version

  1. Installed taleb/parse-latex using quarto
  2. Put in my _quarto.ylm: a top-level global filter (as your example suggests)
filters:
  - parse-latex
  1. Put the following in one chapter .qmd file
Does the `parse-latex` filter work?

```{=latex}
\begin{center}
\begin{tabular}{|l|l|l|l|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  Criterion & Formula &  Partial $\eta^2$   \\
  \hline
  Wilks's $\Lambda$ & $\Lambda = \prod^s_i \frac{1}{1+\lambda_i}$ &  $\eta^2 = 1-\Lambda^{1/s}$   \\
  Pillai trace & $V = \sum^s_i \frac{\lambda_i}{1+\lambda_i}$ &  $\eta^2 = \frac{V}{s} $   \\
  Hotelling-Lawley trace & $H = \sum^s_i \lambda_i$ & $\eta^2 = \frac{H}{H+s}$   \\
  Roy maximum root & $R = \lambda_1$  &  $ \eta^2 = \frac{\lambda_1}{1+\lambda_1}  \\
  \hline
\end{tabular}
\end{center}

What I get in the HTML output is shown below. There is now output from `parse-latex`

![image](https://github.com/user-attachments/assets/9e3424c3-50de-4adf-8124-dd575dcea42d)

**Caption / label**
Even if I can get this to work, it leaves me with the question of how to provide a table caption, label, and make it so I can refer to `@tbl-stats` in the text.

In quarto, I would just use a chunk like
#| label: tbl-stats
#| tbl-cap: Multivariate tests statistics
 ...
tarleb commented 3 months ago

I understand. The table must be fixed, as described in my previous comment. Most important is to add the missing $.

tarleb commented 3 months ago

As for the remaining issues: I don't know, honestly. It would be a good addition for the docs, and I'd be most happy to merge a PR that adds this info.

friendly commented 3 months ago

that pesky $! This now works:

Does the `parse-latex` filter work?

```{=latex}
\begin{center}
\begin{tabular}{|l|l|l|l|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  Criterion & Formula &  Partial $\eta^2$   \\
  \hline
  Wilks's $\Lambda$ & $\Lambda = \prod^s_i \frac{1}{1+\lambda_i}$ &  $\eta^2 = 1-\Lambda^{1/s}$   \\
  Pillai trace & $V = \sum^s_i \frac{\lambda_i}{1+\lambda_i}$ &  $\eta^2 = \frac{V}{s} $   \\
  Hotelling-Lawley trace & $H = \sum^s_i \lambda_i$ & $\eta^2 = \frac{H}{H+s}$   \\
  Roy maximum root & $R = \lambda_1$  &  $ \eta^2 = \frac{\lambda_1}{1+\lambda_1}$  \\
  \hline
\end{tabular}
\end{center}

And produces what I want:

![image](https://github.com/user-attachments/assets/21f4baf4-3731-483e-95b9-31c5c0801081)

** Caption / label**
However, I still can't figure out how to do this because it is a Latex, not an R chunk, so I can't use the Quarto comments like

' label: tab-stats

' tbl-caption: Test statistics for multivariate tests

' ...



It's your extension, so I thought you would have an idea how to do that.
friendly commented 3 months ago

I started this quarto-dev discussion: https://github.com/quarto-dev/quarto-cli/discussions/10432

friendly commented 3 months ago

OK: Here's the answer. I hope you will use it in your README or other examples.

To use parse-latex in a Quarto document embed the {=latex} chunk in a ::: div

::: {#tbl-mstats}

```{=latex}
\begin{center}
\begin{tabular}{|l|l|l|l|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  Criterion & Formula &  Partial $\eta^2$   \\
  \hline
  Wilks's $\Lambda$ & $\Lambda = \prod^s_i \frac{1}{1+\lambda_i}$ &  $\eta^2 = 1-\Lambda^{1/s}$   \\
  Pillai trace & $V = \sum^s_i \frac{\lambda_i}{1+\lambda_i}$ &  $\eta^2 = \frac{V}{s} $   \\
  Hotelling-Lawley trace & $H = \sum^s_i \lambda_i$ & $\eta^2 = \frac{H}{H+s}$   \\
  Roy maximum root & $R = \lambda_1$  &  $ \eta^2 = \frac{\lambda_1}{1+\lambda_1}$  \\
  \hline
\end{tabular}
\end{center}

Test statistics for multivariate tests combine the size of dimensions of $\mathbf{H}\mathbf{E}^{-1}$ into a single measure. :::