【问题标题】:Stargazer R Package: Position of Title in Latex TableStargazer R 软件包:Latex 表中标题的位置
【发布时间】:2023-03-18 08:49:05
【问题描述】:

我正在使用 r 中的 stargazer 包来生成乳胶文档的摘要统计信息。但是,该函数会在表格顶部生成带有标题(标题)的乳胶代码。有没有一种简单的方法可以将标题放在表格下方?

require(stargazer)
df <- data.frame(a=c(1,2,3),
                 b=c(2,3,5),
                 c=c(8,8,9))

stargazer(df,summary = TRUE, type = "latex",
          title = "summary statistic")

函数的输出如下:

\begin{table}[!htbp] \centering 
  \caption{summary statistic} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}}lccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
\hline \\[-1.8ex] 
a & 3 & 2.000 & 1.000 & 1 & 3 \\ 
b & 3 & 3.333 & 1.528 & 2 & 5 \\ 
c & 3 & 8.333 & 0.577 & 8 & 9 \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

请注意,标题位于表格顶部。我想要的是以下内容:

\begin{table}[!htbp] \centering 
\begin{tabular}{@{\extracolsep{5pt}}lccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
\hline \\[-1.8ex] 
a & 3 & 2.000 & 1.000 & 1 & 3 \\ 
b & 3 & 3.333 & 1.528 & 2 & 5 \\ 
c & 3 & 8.333 & 0.577 & 8 & 9 \\ 
\hline \\[-1.8ex] 
\end{tabular} 

  \caption{summary statistic} 
  \label{} 

\end{table} 

HTH 安德烈亚斯

【问题讨论】:

    标签: r latex stargazer


    【解决方案1】:

    我也想做同样的事情,于是来到 SO 寻找答案。由于没有现成的答案,这是我的方法。基本思想是编写一个函数来捕获并重新创建stargazer 输出,将标题和标签移动到底部:

    caption_at_bottom <- function(expr) {
      x <- capture.output(expr)
      cap <- grep("\\\\caption", x)
      lab <- grep("\\\\label", x)
      last <- grep("\\\\end\\{table", x)
      cat(
        paste(
          c(x[-last], x[cap], x[lab], x[last])[-c(cap, lab)]
        , collapse = "\n")
      , "\n")
    }
    

    使用您的示例:

    caption_at_bottom(
      stargazer(df, summary = TRUE, type = "latex", header = F,
              title = "summary statistic")
    )
    
    # \begin{table}[!htbp] \centering 
    # \begin{tabular}{@{\extracolsep{5pt}}lccccc} 
    # \\[-1.8ex]\hline 
    # \hline \\[-1.8ex] 
    # Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\ 
    # \hline \\[-1.8ex] 
    # a & 3 & 2.000 & 1.000 & 1 & 3 \\ 
    # b & 3 & 3.333 & 1.528 & 2 & 5 \\ 
    # c & 3 & 8.333 & 0.577 & 8 & 9 \\ 
    # \hline \\[-1.8ex] 
    # \end{tabular} 
    #   \caption{summary statistic} 
    #   \label{} 
    # \end{table}
    

    【讨论】:

      【解决方案2】:

      您想将调用中的note 参数更改为stargazer() 之类的

      stargazer(df,summary = TRUE, type = "latex",
                title = "summary statistic",
                notes = "put your summary statistic note here"
      )
      

      【讨论】:

      • 感谢您的回答。然而,这不是我想要的。我希望标题出现在表格下方而不是上方。为了实现这一点,我需要在表格元素下方设置标题和标签参数。
      猜你喜欢
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 2016-10-23
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多