You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R+Overleaf下modelsummary生成LaTeX表格适配及编译报错求助

求助:modelsummary生成的LaTeX表格无法适配Overleaf页面

我的工作流是R + Overleaf,常需要在R中生成回归表格后到Overleaf编译输出。最近用modelsummary包完成这项工作,功能虽便捷强大,但遇到了棘手问题:

直接用modelsummary生成的LaTeX表格过宽过长,Overleaf编译时无法适配单页。LaTeX中常用longtable解决此类问题,但modelsummary生成的.tex文件似乎与longtable命令不兼容。我尝试用kableExtra缩放表格并缩小字号(因为有数百个这类文件,手动逐个修改效率极低),但始终无法成功:

  • 指定output=kableExtra时,保存的.tex文件实际为HTML格式
  • 指定output=latex时,保存的文件为空

可复现的R代码

# update.packages()
library(kableExtra)
library(modelsummary)
library(tidyr)
options(knitr.table.format = "latex")
options("modelsummary_format_numeric_latex" = "plain")

url <- 'https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv'
dat <- read.csv(url)

models <- list(
  "OLS 1"     = lm(Donations ~ Literacy + Clergy, data = dat),
  "Poisson" = glm(Donations ~ Literacy + Commerce, family = poisson, data = dat),
  "OLS 2"     = lm(Crime_pers ~ Literacy + Clergy, data = dat)
)

# Generate modelsummary output as a kable object (for compatibility)
raw_output <- modelsummary(models = models, output = "kableExtra")

# Apply kableExtra styling to the kable object
styled_output <- raw_output %>%
  kable_styling(font_size = 7, latex_options = c('scale_down', 'HOLD_position'))

# Save the styled LaTeX output to a file
kableExtra::save_kable(
  styled_output,
  file = "test.tex"
)

编辑后的尝试

根据建议改用tinytable工具,修改后的R代码如下:

raw_output <- modelsummary(models = models, output = "tinytable") |>
  theme_tt("resize", width=0.8) |>
  save_tt('test.tex', overwrite = TRUE)

在Overleaf中使用以下环境编译该.tex文件:

\documentclass{article}
\usepackage{tabularray}
\usepackage{float}
\usepackage{graphicx}
\usepackage{codehigh}
\usepackage[normalem]{ulem}
\usepackage{tcolorbox}
\usepackage{adjustbox}
\usepackage{pdflscape}
\usepackage{subcaption}
\usepackage[skip=1ex, 
            font={small, sf},
            labelfont=bf
            ]{caption}  % new
\usepackage{tabularray}
\usepackage{tblr-extras}
\UseTblrLibrary{amsmath, booktabs,
                caption, siunitx}    % new
%\usepackage{amsmath}   %   Loaded by TblrLibrary
\usepackage{acronym}
\newcommand{\tinytableTabularrayUnderline}[1]{\underline{#1}}
\newcommand{\tinytableTabularrayStrikeout}[1]{\sout{#1}}
\NewTableCommand{\tinytableDefineColor}[3]{\definecolor{#1}{#2}{#3}}

\graphicspath{{./images}}
\begin{landscape}

\input{tables/test.tex}
\end{landscape}

编译时出现首个错误:

Missing $ inserted.
You need to enclose all mathematical expressions and symbols with special markers. These special markers create a ‘math mode’.

Use $...$ for inline math mode, and [...]or one of the mathematical environments (e.g., equation) for display math mode.

This applies to symbols such as subscripts ( _ ), integrals ( \int ), Greek letters ( \alpha, \beta, \delta ) and modifiers (\vec{x}, \tilde{x}).

通常这类错误与longtable相关,求可行的解决方案。


内容的提问来源于stack exchange,提问作者Jia Gao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 04:02:13