Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stargazer left align LaTeX table columns

Tags:

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  \begin{document} \title{} \author{} \date{\today} \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

like image 666
luciano Avatar asked Sep 12 '14 07:09

luciano


People also ask

How do I left align a table column in latex?

You can also use r to align the text to the right and l for left alignment. This will insert a horizontal line on top of the table and at the bottom too. There is no restriction on the number of times you can use \hline .

What does Stargazer do in R?

stargazer is an R package that creates LATEX code, HTML code and ASCII text for well-formatted regression tables, with multiple models side-by-side, as well as for summary statistics tables, data frames, vectors and matrices.

How do you get a stargazer table in R?

This can be done by typing “install. packages(“stargazer”)”, and then “library(stargazer)” in the next line. Installing Stargazer will only need to be done once, but the second command, which loads the package will need to be typed each session you wish to use it.


1 Answers

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

like image 60
nathanesau Avatar answered Sep 18 '22 16:09

nathanesau