将 LaTeX 参考标签添加到 modelsummary 中的表中

发布于 2025-01-16 08:48:50 字数 1907 浏览 5 评论 0 原文

我正在尝试在 R 中生成表格(不是 RMarkdown,如 这个问题 帮助回答)与 modelsummary ,我很难添加参考标签(例如,tab:hello)。正如文森特在 这个答案中指出的< /a>,modelsummary 的额外参数应该自动推送,但这似乎不适用于 LaTeX 标签。下面是一个例子:

library(modelsummary)
library(kableExtra)

x <- rnorm(100)
y <- rnorm(100)

modelsummary(lm(y ~ x),
             output = "latex",
             caption = "test",
             label = "hello")

这会产生:

\begin{table}
\caption{test}
\centering
\begin{tabular}[t]{lc}
\toprule
  & Model 1\\
\midrule
(Intercept) & \num{-0.143}\\
 & (\num{0.100})\\
x & \num{-0.023}\\
 & (\num{0.092})\\
\midrule
Num.Obs. & \num{100}\\
R2 & \num{0.001}\\
R2 Adj. & \num{-0.010}\\
AIC & \num{283.4}\\
BIC & \num{291.2}\\
Log.Lik. & \num{-138.698}\\
F & \num{0.061}\\
\bottomrule
\end{tabular}
\end{table}

同时,这与调用 kable 配合得很好:

df <- cbind.data.frame(x, y)
kable(head(df), "latex",
      caption = "test",
      label = "hello")

会产生:

\begin{table}
\caption{\label{tab:hello}test}
\centering
\begin{tabular}[t]{r|r}
\hline
x & y\\
\hline
0.8078318 & -0.0219732\\
\hline
0.4660209 & -0.9973773\\
\hline
-1.0620694 & -0.1360954\\
\hline
0.5639881 & 0.0185161\\
\hline
0.3459854 & 0.1333345\\
\hline
-0.8035314 & -0.0759982\\
\hline
\end{tabular}
\end{table}

奇怪的是,这仅在指定标题时才有效。在 kable 中仅定义 label 不会产生标签。

理想情况下,我只想生成一个标签,但如果我也必须添加标题,我可以处理它(因为这似乎也是 kable 的问题---或者只是一般的 LaTeX 表)。

预先感谢您的帮助!非常感谢。

I'm trying to produce tables in R (not RMarkdown, as this question helps answer) with modelsummary and I'm having a tough time adding reference labels (e.g., tab:hello). As Vincent points out in this answer, extra arguments to modelsummary should be pushed forward automatically, but it seems this does not work with LaTeX labels. Here's an example:

library(modelsummary)
library(kableExtra)

x <- rnorm(100)
y <- rnorm(100)

modelsummary(lm(y ~ x),
             output = "latex",
             caption = "test",
             label = "hello")

This produces:

\begin{table}
\caption{test}
\centering
\begin{tabular}[t]{lc}
\toprule
  & Model 1\\
\midrule
(Intercept) & \num{-0.143}\\
 & (\num{0.100})\\
x & \num{-0.023}\\
 & (\num{0.092})\\
\midrule
Num.Obs. & \num{100}\\
R2 & \num{0.001}\\
R2 Adj. & \num{-0.010}\\
AIC & \num{283.4}\\
BIC & \num{291.2}\\
Log.Lik. & \num{-138.698}\\
F & \num{0.061}\\
\bottomrule
\end{tabular}
\end{table}

Meanwhile, this works fine with a call to kable:

df <- cbind.data.frame(x, y)
kable(head(df), "latex",
      caption = "test",
      label = "hello")

Which produces:

\begin{table}
\caption{\label{tab:hello}test}
\centering
\begin{tabular}[t]{r|r}
\hline
x & y\\
\hline
0.8078318 & -0.0219732\\
\hline
0.4660209 & -0.9973773\\
\hline
-1.0620694 & -0.1360954\\
\hline
0.5639881 & 0.0185161\\
\hline
0.3459854 & 0.1333345\\
\hline
-0.8035314 & -0.0759982\\
\hline
\end{tabular}
\end{table}

Weirdly, this only works when specifying a caption too. Defining only a label in kable doesn't produce a label.

Ideally, I'd like to only produce a label, but if I have to add a caption too, I can deal with that (since it seems like this is an issue with kable as well---or just with LaTeX tables in general).

Thank you in advance for the help! It's much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2025-01-23 08:48:50

我相信这是 LaTeX 本身的一个限制,它需要一个标题来正确的表格编号和引用。请参阅此答案以获取讨论和替代机制:

https://tex.stackexchange.com/a/438267/16188

除此之外,我认为注意到这不是一个 modelsummary 特定的问题是有用的。您可能知道,modelsummary 支持多种生成不同输出格式的制表包:kableExtragtflextable >,或huxtable。 LaTeX 的默认制表器是 kableExtra。

下面是 kableExtra 中的一个最小示例,它重现了相同的问题:

library(kableExtra)
dat <- mtcars[1:3, 1:3]

没有出现标签:

kbl(dat, format = "latex", label = "fig:test") |>
    kable_styling()
#> \begin{table}
#> \centering
#> \begin{tabular}[t]{l|r|r|r}
#> \hline
#>   & mpg & cyl & disp\\
#> \hline
#> Mazda RX4 & 21.0 & 6 & 160\\
#> \hline
#> Mazda RX4 Wag & 21.0 & 6 & 160\\
#> \hline
#> Datsun 710 & 22.8 & 4 & 108\\
#> \hline
#> \end{tabular}
#> \end{table}

出现标签:

kbl(dat, format = "latex", caption = "", label = "fig:test") |>
    kable_styling()
#> \begin{table}
#> 
#> \caption{\label{tab:fig:test}}
#> \centering
#> \begin{tabular}[t]{l|r|r|r}
#> \hline
#>   & mpg & cyl & disp\\
#> \hline
#> Mazda RX4 & 21.0 & 6 & 160\\
#> \hline
#> Mazda RX4 Wag & 21.0 & 6 & 160\\
#> \hline
#> Datsun 710 & 22.8 & 4 & 108\\
#> \hline
#> \end{tabular}
#> \end{table}

对于它的价值,在 modelsummary 中,我倾向于将标题直接嵌入到 < code>title 参数,通过使用 \\ 来“转义”反斜杠。例如:

library(modelsummary)
mod <- lm(mpg ~ hp, mtcars)
modelsummary(mod, title = "\\label{fig:test}", output = "latex", escape = FALSE)
#> \begin{table}
#> 
#> \caption{\label{fig:test}}
#> \centering
#> \begin{tabular}[t]{lc}
#> \toprule
#>   & Model 1\\
#> \midrule
#> (Intercept) & \num{30.099}\\
#>  & (\num{1.634})\\
#> hp & \num{-0.068}\\
#>  & (\num{0.010})\\
#> \midrule
#> Num.Obs. & \num{32}\\
#> R2 & \num{0.602}\\
#> R2 Adj. & \num{0.589}\\
#> AIC & \num{181.2}\\
#> BIC & \num{185.6}\\
#> Log.Lik. & \num{-87.619}\\
#> F & \num{45.460}\\
#> RMSE & \num{3.86}\\
#> \bottomrule
#> \end{tabular}
#> \end{table}

I believe this is a limitation in LaTeX itself, which requires a caption for proper table numbering and referencing. See this answer for a discussion and an alternative mechanism:

https://tex.stackexchange.com/a/438267/16188

Beyond that, I think it is useful to note that this is not a modelsummary-specific issue. As you may know, modelsummary supports several table-making packages that produce different output formats: kableExtra, gt, flextable, or huxtable. The default table-maker for LaTeX is kableExtra.

Here’s a minimal example in kableExtra that reproduces the same problem:

library(kableExtra)
dat <- mtcars[1:3, 1:3]

No label appears:

kbl(dat, format = "latex", label = "fig:test") |>
    kable_styling()
#> \begin{table}
#> \centering
#> \begin{tabular}[t]{l|r|r|r}
#> \hline
#>   & mpg & cyl & disp\\
#> \hline
#> Mazda RX4 & 21.0 & 6 & 160\\
#> \hline
#> Mazda RX4 Wag & 21.0 & 6 & 160\\
#> \hline
#> Datsun 710 & 22.8 & 4 & 108\\
#> \hline
#> \end{tabular}
#> \end{table}

Label appears:

kbl(dat, format = "latex", caption = "", label = "fig:test") |>
    kable_styling()
#> \begin{table}
#> 
#> \caption{\label{tab:fig:test}}
#> \centering
#> \begin{tabular}[t]{l|r|r|r}
#> \hline
#>   & mpg & cyl & disp\\
#> \hline
#> Mazda RX4 & 21.0 & 6 & 160\\
#> \hline
#> Mazda RX4 Wag & 21.0 & 6 & 160\\
#> \hline
#> Datsun 710 & 22.8 & 4 & 108\\
#> \hline
#> \end{tabular}
#> \end{table}

For what it’s worth, in modelsummary I tend to embed the caption directly in the title argument, by using \\ to “escape” the backslash. For example:

library(modelsummary)
mod <- lm(mpg ~ hp, mtcars)
modelsummary(mod, title = "\\label{fig:test}", output = "latex", escape = FALSE)
#> \begin{table}
#> 
#> \caption{\label{fig:test}}
#> \centering
#> \begin{tabular}[t]{lc}
#> \toprule
#>   & Model 1\\
#> \midrule
#> (Intercept) & \num{30.099}\\
#>  & (\num{1.634})\\
#> hp & \num{-0.068}\\
#>  & (\num{0.010})\\
#> \midrule
#> Num.Obs. & \num{32}\\
#> R2 & \num{0.602}\\
#> R2 Adj. & \num{0.589}\\
#> AIC & \num{181.2}\\
#> BIC & \num{185.6}\\
#> Log.Lik. & \num{-87.619}\\
#> F & \num{45.460}\\
#> RMSE & \num{3.86}\\
#> \bottomrule
#> \end{tabular}
#> \end{table}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文