'R & Latex: I would like to export the eigen function results as how it is to Latex
I want to export a result from running a simple eigen() function to Latex as how it is in R.
For example
a <- c(0,1,0,0,0,0)
b <- c(1,0,1,1,0,0)
c <- c(0,1,0,0,0,1)
d <- c(0,1,0,0,1,1)
e <- c(0,0,0,1,0,1)
f <- c(0,0,1,1,1,0)
A <- data.frame(a,b,c,d,e,f)
eigen(A)
At the moment, I uploaded a screenshot onto the latex as an image but I would like to have a Latex table instead so that it's scaled and fitted better into the page.
I don't know how to show such results in Latex. If you have any suggestions, I would really appreciate them.
Solution 1:[1]
Create a file eigen.Rnw containing this text:
\documentclass{article}
\usepackage{booktabs}
\usepackage{lipsum}
\begin{document}
\lipsum[1][1-3]
<<eigen1>>=
A <- cbind(c(0, 1, 0, 0, 0, 0),
c(1, 0, 1, 1, 0, 0),
c(0, 1, 0, 0, 0, 1),
c(0, 1, 0, 0, 1, 1),
c(0, 0, 0, 1, 0, 1),
c(0, 0, 1, 1, 1, 0))
e <- eigen(A)
e
@
\lipsum[1][4-6]
<<eigen2>>=
knitr::kable(e$vectors, booktabs = TRUE, linesep = "")
@
\end{document}
Install package knitr with install.packages("knitr"), then do
knitr::knit2pdf("eigen.Rnw")
in R to generate eigen.tex and compile eigen.pdf. You can open eigen.pdf from R with
system(paste(getOption("pdfviewer"), "eigen.pdf"))
If you edit eigen.Rnw in RStudio, then you can use the Compile PDF button, which automates this workflow.
knitr syntax and options are documented extensively here. There is also package kableExtra, which extends the options available for formatting matrices and data frames and has documentation here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

