0

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)

The results are shown below: enter image description here

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.

RBG
  • 39
  • 2
  • 1
    Welcome to stack overflow. The inclusion of a reproducible example would make it easier to help you. [Link for guidance on asking questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Mar 03 '22 at 14:02

1 Answers1

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.

Mikael Jagan
  • 6,005
  • 1
  • 9
  • 28