# matlib::showEqn error

Hi,

My issue is that when I use matlib::showEqn in a pdf format, and latex = T, which would be the goal, I run into an error.

Enclosed an example markdown file, not a reprex, in order to not hide the chunk options. Just delete #.

---
title: "eq show test"
output: pdf_document
---

{r}
library(matlib)
(b <- c(1, -7))
(A1 <- matrix(c(2, 1, 1, 3), nrow = 2))


{r results='asis'}
showEqn(A1, b, latex = T)



Thank you for your help in advance,
Marcell

The error would be helpful.

Thank you for your time.

The error message:

! Missing inserted. <inserted text>
l.147 ...backslash begin{array}{lllll} 2 \cdot
x_1 &+& 1 \cdot x_2

! pdflatex: major issue: So far, you have not checked for MiKTeX updates.

Try to find the following text in eq_test.Rmd:
...backslash begin{array}{lllll} 2 \cdot

You may need to add around a certain inline R expression r  in eq_test.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile eq_test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See eq_test.log for more info.
Execution halted

r showEqn(A1, b, latex = T)


Thank you, but doesnt work

Are you putting it in a math environment?

The display of your error is messed up, you needed to paste it in a verbatim environment to maintain the formatting and special characters.

I believe the issue is your array needs to be inside a LaTeX environment for displaying mathematics.

A code chunk can't be nested inside of a display environment like that, so I told you to use an inline R reference. You still need to create the environment for it to sit in.

So, the complete line should probably look like this,

$r showEqn(A1, b, latex = TRUE)$


Tried, but still doesnt work.
The new result is the following:

That screenshot doesn't show a problem. Based on what I'm seeing though, my previous response should have worked.

This was annoying me, so I decided to dig into it a bit more after I got back to my computer. My take on it is that the showEqn() in matlib is fundamentally broken, which I'll explain after I tell you how to just do what you want to do.

So, first, what you want to do is wrap an R code chunk in an equation environment and set results = "asis" and echo = FALSE. Here is the content of a minimally working Rmd file which will do what you want.

---
output: pdf_document
---

{r matlib}
library(matlib)
A <- matrix(c(1, 2, -1, 2), 2, 2)
b <- c(2,1)


$${r, results = "asis", echo = FALSE} showEqn(A, b, latex = TRUE) $$


Now, the reason why I think showEqn() is fundamentally broken is because the function itself dumps the LaTeX code straight into the io stream with cat(). Side effects like this are best avoided and the choice of when and how to output to the stream are best left to the user. You can "fix" this with the capture.output(). Below is a simple example of how it works.

x <- 1:3
cat_fun <- function(x) {
cat("Hello!")
x
}
y <- cat_fun(x)
#> Hello!
y
#> [1] 1 2 3
z <- capture.output(x)
z
#> [1] "[1] 1 2 3"


Created on 2020-09-03 by the reprex package (v0.3.0)
Now, to see how this can be used with showEqn() here is a slightly different Rmd file.

---
output:  pdf_document
---

{r}
library(matlib)
A <- matrix(c(2, 1, -1,
-3, -1, 2,
-2,  1, 2), 3, 3, byrow=TRUE)
b <- c(8, -11, -3)
out <- paste(capture.output(showEqn(A, b, latex = TRUE)), collapse = "")



This allows us to save the output in an R object and use it anywhere else we want.

$r out$

$$r out$$
And we also now have the freedom to manipulate the output,
{r}
new_out <- gsub("array", "bmatrix", out)
new_out <- gsub("\\{[lrc]+\\}", "\\{\\}", new_out)


$$r new_out$$
Now, I don't know _why_ you would want or need to do this,
but you _can_ when you eliminate the side-effects of the function.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.