leovan / quarto-pseudocode

Quarto Pseudocode Extension
MIT License
38 stars 2 forks source link

Latex Macros #5

Open gabrielvc opened 4 days ago

gabrielvc commented 4 days ago

Hello,

I was wondering what is the best way to have Latex macros to work with pseudocode.

This does not work

---
title: "Pseudocode and macros"
format:
  html:
    default
filters:
  - pseudocode
pseudocode:
  caption-prefix: "Algorithm"
  reference-prefix: "Algorithm"
  caption-number: true
---

::: {hidden}
\newcommand{\E}[1]{\mathbb{E}\left[#1\right]}
:::

This works
$$
\E{X}
$$

This does not
```pseudocode
#| html-indent-size: "1.2em"
#| html-comment-delimiter: "//"
#| html-line-number: true
#| html-line-number-punc: ":"
#| html-no-end: false
#| pdf-placement: "htb!"
#| pdf-line-number: true
\begin{algorithm}
\caption{Quicksort}
\begin{algorithmic}
\Procedure{Quicksort}{$A, p, r$}
  \If{$p < r$}
    \State $q = \E{X}$ \Call{Partition}{$A, p, r$}
    \State \Call{Quicksort}{$A, p, q - 1$}
    \State \Call{Quicksort}{$A, q + 1, r$}
  \EndIf
\EndProcedure
\Procedure{Partition}{$A, p, r$}
  \State $x = A[r]$
  \State $i = p - 1$
  \For{$j = p$ \To $r - 1$}
    \If{$A[j] < x$}
      \State $i = i + 1$
      \State exchange
      $A[i]$ with     $A[j]$
    \EndIf
    \State exchange $A[i]$ with $A[r]$
  \EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}

Is there another way of defining macros that can make this kind of example work?

leovan commented 4 days ago

I believe it's a limitation of pseudocode.js with HTML format output, it seems do not support macros. Macros works well with PDF format output.