rjournal / rjtools

Tools for AUTHORS to use for checking and submitting articles to the R Journal
https://rjournal.github.io/rjtools/
Other
31 stars 14 forks source link

Solution for formatting algorithms #103

Open hturner opened 1 year ago

hturner commented 1 year ago

I'm not aware of a tool to format algorithms in markdown, possibly something could be provided by rjtools as a consistent solution.

As an example, I tried to format the algorithm from here: https://tex.stackexchange.com/a/452565/21118.

---
title: "Formatting an algorithm"
output:
  html_document:
    highlight: tango
---

```{bash, attr.source='.numberLines', tidy = FALSE, eval = FALSE}
# Result: Write here the result
# Input: Write here the input
# Output: Write here the output
while while_condition do
  instructions
  if condition then
    instructions1
    instructions2
  else
    instructions3
  end
end


<img width="667" alt="image" src="https://github.com/rjournal/rjtools/assets/3343008/63a2e30c-f038-4117-9dbd-3260c40fda0c">

Apart from "do" and "end" not being highlighted it's not a bad solution. However it's not compatible with rjtools::rjournal_web_article:

<img width="645" alt="image" src="https://github.com/rjournal/rjtools/assets/3343008/f93c512f-d554-4333-8402-ba3e4d73d816">

What might be a better solution that could be recommended to authors?
hturner commented 1 year ago

LaTeX solutions for reference: https://www.overleaf.com/learn/latex/Algorithms. Some are more like code blocks and some are more like tables. I think a code-like version would be easier with R markdown, perhaps with a pseudocode language engine.

cderv commented 1 year ago

Quick demo of using pseudocode.js (https://saswat.padhi.me/pseudocode.js/) with R Markdown to mimic algorithm from LaTeX https://rpubs.com/cderv/pseudocodejs

Maybe it could help.

Otherwise texPreview is always an option to have an image version of a LaTeX rendering part. They have a nice engine. https://yonicd.github.io/texPreview/

Hope it helps

hturner commented 1 year ago

The pseudocode.js option looks like a good solution as returns proper HTML and MathJax. @ajgodfrey can you read the rendered algorithms in https://rpubs.com/cderv/pseudocodejs?

I guess the style could also be controlled via CSS to be more like the HTML tables.

cderv commented 1 year ago

Main JS library (https://saswat.padhi.me/pseudocode.js/) offers solution for more recent MathJaX or even KaTeX. So if one of those solution is better, I believe it can also be used with R Markdown.

The example on rPubs is quite old - so it could also have been updated since then regarding accessibility. I see there is now support for MathJax 4 even: https://github.com/SaswatPadhi/pseudocode.js