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

Mac系统下R Studio中latex命令无法运行问题求助

解决rms包latex()函数输出回归公式的报错问题

我尝试用rms包的latex()函数输出Cox比例风险模型的回归公式,代码如下:

require(rms)
f <- cph(Surv(...) ~ ...)
latex(f)

但运行后出现无法理解的错误,已安装Xcode和MacTeX,怀疑是数据问题,相关日志、错误信息及17条警告如下:

This is pdfTeX, Version 3.141592653-2.6-1.40.25 (TeX Live 2023) (preloaded format=latex)
restricted \write18 enabled.
entering extended mode

(/var/folders/y8/dcqwxmkn4qgd15mj86hyt1sr0000gp/T//RtmpN1ROBO/file1416d26bf8cef
.tex
LaTeX2e <2022-11-01> patch level 1
L3 programming layer <2023-02-22>
(/usr/local/texlive/2023/texmf-dist/tex/latex/base/report.cls
Document Class: report 2022/07/02 v1.4n Standard LaTeX document class
(/usr/local/texlive/2023/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2023/texmf-dist/tex/latex/geometry/geometry.sty
(/usr/local/texlive/2023/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2023/texmf-dist/tex/generic/iftex/ifvtex.sty
(/usr/local/texlive/2023/texmf-dist/tex/generic/iftex/iftex.sty)))
(/usr/local/texlive/2023/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)
No file file1416d26bf8cef.aux.
geometry driver: auto-detecting
geometry detected driver: dvips

Overfull \hbox (1097.7373pt too wide) in paragraph at lines 6--12

[1] (./file1416d26bf8cef.aux) )
(see the transcript file for additional information)
Output written on file1416d26bf8cef.dvi (1 page, 1144 bytes).
Transcript written on file1416d26bf8cef.log.
Error: Can't open display: :0
There were 17 warnings (use warnings() to see them)

17条警告信息:

Warning messages:
1: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
2: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
3: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
4: In is.na(xj) : is.na() applied to non-(list or vector) of type 'language'
5: In is.na(xk) : is.na() applied to non-(list or vector) of type 'language'
6: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
7: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
8: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
9: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
10: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
11: In is.na(xj) : is.na() applied to non-(list or vector) of type 'language'
12: In is.na(xk) : is.na() applied to non-(list or vector) of type 'language'
13: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
14: In is.na(xj) : is.na() applied to non-(list or vector) of type 'language'
15: In is.na(xk) : is.na() applied to non-(list or vector) of type 'language'
16: In cbind(cx, cxk) :
number of rows of result is not a multiple of vector length (arg 2)
17: In system(cmd, intern = TRUE, wait = TRUE) :
running command 'xdvi -paper 5.5x7in -s 0 /var/folders/y8/dcqwxmkn4qgd15mj86hyt1sr0000gp/T//RtmpN1ROBO/file1416d26bf8cef.dvi' had status 1


问题排查与解决方案

1. 解决核心显示错误:Can't open display: :0

这个错误是因为latex()函数默认调用xdvi预览DVI文件,但macOS未预装该工具,且无图形界面时DISPLAY变量也无法正确识别。直接跳过预览,生成文件即可:

# 生成TeX文件,不打开预览
latex(f, file = "model_formula.tex", preview = FALSE)

# 或直接生成PDF文件
latex(f, file = "model_formula.pdf", preview = FALSE, pdf = TRUE)

2. 处理数据相关警告

cbind和is.na类警告,多因模型公式或数据存在问题:

  • 检查Surv()参数:确保第一个参数是生存时间,第二个是0/1型结局变量
  • 清理公式变量:避免使用未解析的表达式(language类型对象),确保所有变量都是数据框中的数值/分类变量
  • 验证数据一致性:检查变量长度是否统一,是否存在异常缺失值
  • 极简模型测试:先用Surv(time, status) ~ age这类简单公式验证功能,再逐步添加变量

3. 修复LaTeX排版溢出警告:Overfull \hbox

生成的内容过宽超出页面,可通过两种方式解决:

  • 在latex()中缩小字体:latex(f, size = "small", preview = FALSE)
  • 手动修改生成的TeX文件,添加自动缩放包:
    \usepackage{adjustbox}
    \begin{adjustbox}{width=\textwidth}
    % 插入生成的公式/表格内容
    \end{adjustbox}
    

内容的提问来源于stack exchange,提问作者Julia Rose

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 10:52:31