friendly / matlib

Matrix Functions for Teaching and Learning Linear Algebra and Multivariate Statistics
http://friendly.github.io/matlib/
65 stars 16 forks source link

`renderMatrix()`: render a matrix expression in RStudio viewer #64

Closed friendly closed 2 months ago

friendly commented 2 months ago

In dev/renderMatrix-notes.Rmd I raise this idea:

It would be nice to be able to render a LaTeX equation from the console for the purpose of testing an equation using Eqn() before putting the result into a document. Presently, I'm using the Lagrida LaTeX Equation Editor.

The idea is to be able to do something like:

matX <- latexMatrix()
renderMatrix(matX)

and see the result in an RStudio Viewer panel. However, I note that (unless I'm mistaken) Eqn() now no longer has an option to return the result other than via cat().

I sketch a few possible routes for this. What do you think?

philchalmers commented 2 months ago

Hi Michael,

If the results of Eqn() are copied into a markdown document enclosed with $$ then you'll see the previous of the equation without the need of extra functions. This could be added to a markdown file with some rstudioapi magic where the results are placed inside a document (con: users would need to delete this after they're done previewing). Otherwise, if you're not looking to temporarily pollute a working markdown file then texPreview would IMO be the most natural.

philchalmers commented 2 months ago

Alternatively, I could add something like Eqn(, preview=TRUE to build a temporary .Rmd file with only the isolated equation and have that compiled into an HTML/PDF file.

friendly commented 2 months ago

I don't quite understand

If the results of Eqn() are copied into a markdown document enclosed with $$ then you'll see the previous of the equation without the need of extra functions.

I think ideally that the preview should be a separate function, so that a pipe would work like

latexMatrix() |> Eqn() |> renderMatrix()

(or maybe renderEqn() is better.

philchalmers commented 2 months ago

Sorry, I wasn't clear. See the attached screenshot for what I meant with $$, which I copy-pasted from the console. If this is fine then a clip argument similar to what has appeared in latexMatrix() could suffice.

Screenshot 2024-09-06 115806

friendly commented 2 months ago

I wasn't clear either. I'm looking for some way to do this from the R console rather than in a document.

My 3rd idea was:

  1. A function renderMatrix() to:
    • Insert result of latexMatrix() or Eqn() into a document template (temporary file)
    • Compile that with knitr
    • Preview result with browseURL()

Are either of you using TexLive? If so, perhaps test the texPreview route?

friendly commented 2 months ago

I raised this as an issue in https://github.com/yonicd/texPreview/issues/58

philchalmers commented 2 months ago

Thanks for clarifying. I'd lean towards the 3rd idea as it would avoid adding further package dependencies, and could be modified on our end to include missing packages if local equations require them (e.g., amsmath, or bm if the output is a PDF). OS focus would change to a pop-out PDF or HTML, but I don't see a strong need to stay within the confines of RStudio if this is for testing purposes.

philchalmers commented 2 months ago

Added a preview arg to Eqn(), so now you can see renderings of the equations in the Rstudio pane.

Screenshot 2024-09-06 141836

friendly commented 2 months ago

Brilliant! Exactly what I was hoping for. Thanks so much :)

philchalmers commented 2 months ago

Quick update: preview is now TRUE by default, and the associated LaTeX code will also be printed to the console for side-by-side comparison.