我正在尝试在 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.
发布评论
评论(1)
我相信这是 LaTeX 本身的一个限制,它需要一个标题来正确的表格编号和引用。请参阅此答案以获取讨论和替代机制:
https://tex.stackexchange.com/a/438267/16188
除此之外,我认为注意到这不是一个
modelsummary
特定的问题是有用的。您可能知道,modelsummary
支持多种生成不同输出格式的制表包:kableExtra
、gt
、flextable
>,或huxtable
。 LaTeX 的默认制表器是 kableExtra。下面是
kableExtra
中的一个最小示例,它重现了相同的问题:没有出现标签:
出现标签:
对于它的价值,在
modelsummary
中,我倾向于将标题直接嵌入到 < code>title 参数,通过使用\\
来“转义”反斜杠。例如: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
, orhuxtable
. The default table-maker for LaTeX iskableExtra
.Here’s a minimal example in
kableExtra
that reproduces the same problem:No label appears:
Label appears:
For what it’s worth, in
modelsummary
I tend to embed the caption directly in thetitle
argument, by using\\
to “escape” the backslash. For example: