当type= " latex "时,如何将星图表格向左对齐?

时间:2022-03-21 14:58:04

I use stargazer package in my automated rmarkdown pdf documents to make nice looking tables. Stargazer places its tables in the center of the page, by default. How can I let stargazer generate latex code that aligns the table to the left?

我在我的自动rmarkdown pdf文档中使用stargazer包来制作好看的表格。默认情况下,Stargazer将其表放在页面的中心位置。我怎么能让占星者生成将表向左对齐的乳胶代码呢?

here is an example of what I mean:

我的意思是:

library(stargazer)

data_object <- structure(list(test = structure(c(1L, 1L, 2L, 2L), .Label = c("test1",  "test2"), class = "factor"), test2 = structure(1:4, .Label = c("1",        "2", "3", "4"), class = "factor")), .Names = c("test", "test2"), row.names = c(NA, -4L), class = "data.frame")

stargazer(data_object,title="table test",summary=FALSE,rownames=FALSE,type="latex",header=FALSE)

the code it produces is:

它产生的代码是:

\begin{table}[!htbp] \centering 
  \caption{table test} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
test & test2 \\ 
\hline \\[-1.8ex] 
test1 & 1 \\ 
test1 & 2 \\ 
test2 & 3 \\ 
test2 & 4 \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table}

Note the \centering. How can I change that without having to alter the latex code itself?

注意\定心。如何在不需要修改乳胶代码的情况下改变它呢?

1 个解决方案

#1


3  

It would appear \centering is hard coded into the function. What you could do is delete \centering using sub (e.g. sub(" \\\\centering", "", out)).

它看起来像是硬编码到函数中的\定心。你所能做的就是使用sub删除\定心(例如sub(“\\\\定心”、“out”)。

Here's the chunk I used. I used capture.output to prevent stargazer to output what I consider intermediate result.

这是我用过的那块。我使用截图所示。输出以防止占星者输出我认为的中间结果。

<<results = "asis">>=
library(stargazer)

data_object <- structure(list(test = structure(c(1L, 1L, 2L, 2L), .Label = c("test1",  "test2"), class = "factor"), test2 = structure(1:4, .Label = c("1",        "2", "3", "4"), class = "factor")), .Names = c("test", "test2"), row.names = c(NA, -4L), class = "data.frame")

out <- capture.output(stargazer(data_object,title="table test",summary=FALSE,rownames=FALSE,type="latex",header=FALSE))
out <- sub(" \\\\centering", "", out)
cat(out)
@

当type= " latex "时,如何将星图表格向左对齐?

#1


3  

It would appear \centering is hard coded into the function. What you could do is delete \centering using sub (e.g. sub(" \\\\centering", "", out)).

它看起来像是硬编码到函数中的\定心。你所能做的就是使用sub删除\定心(例如sub(“\\\\定心”、“out”)。

Here's the chunk I used. I used capture.output to prevent stargazer to output what I consider intermediate result.

这是我用过的那块。我使用截图所示。输出以防止占星者输出我认为的中间结果。

<<results = "asis">>=
library(stargazer)

data_object <- structure(list(test = structure(c(1L, 1L, 2L, 2L), .Label = c("test1",  "test2"), class = "factor"), test2 = structure(1:4, .Label = c("1",        "2", "3", "4"), class = "factor")), .Names = c("test", "test2"), row.names = c(NA, -4L), class = "data.frame")

out <- capture.output(stargazer(data_object,title="table test",summary=FALSE,rownames=FALSE,type="latex",header=FALSE))
out <- sub(" \\\\centering", "", out)
cat(out)
@

当type= " latex "时,如何将星图表格向左对齐?