Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

r - stargazer left align LaTeX table columns

stargazer automatically centres values within tables. How can I left align the columns?

Put this code in an .Rnw file and use knitr to convert to .tex:

<<load, echo=FALSE, warning=FALSE, message=FALSE>>=
opts_chunk$set(eval=TRUE, echo=FALSE, warning=FALSE, message=FALSE, dpi=300)
@


documentclass[a4paper,11pt]{article}
usepackage{lipsum} % Required to insert dummy text

egin{document}
itle{}
author{}
date{oday}
maketitle

section{Header}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

<<iris, results = "asis">>=
library(stargazer)
stargazer(iris[1:10,4:5], summary  = FALSE)
@

end{document}

This is the PDF output:

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As has been pointed out in the comments, you could either post-process the output of stargazer, or use xtable. I'll demonstrate both approaches.

  1. post-processing: Replace your code chuck with the following two code chunks

    <<echo=FALSE, results=hide>>=
    library(stargazer)
    tab <- stargazer(iris[1:10,4:5], summary  = FALSE) 
    @
    
    <<results=tex, echo=FALSE>>=
    collapse <- function(st) paste(st, collapse="")
    st <- gsub(collapse(rep("c", 3)), collapse(rep("l",3)), tab)
    cat(st[4:24])
    @
    
  2. xtable: After installing the xtable package, you could use this as your code chuck

    <<iris, results="asis", echo=FALSE>>=
    library(xtable)
    print(xtable(iris[1:10,4:5], align="lll", caption=""))
    @
    

I think the xtable approach is probably easier though


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...