R Booktabs表格不以pdf或doc格式显示

R Booktabs表格不以pdf或doc格式显示,r,latex,rstudio,tex,tinytex,R,Latex,Rstudio,Tex,Tinytex,尝试创建包含booktabs表的pdf时出错。如果我删除表代码,则创建pdf时不会出现问题 RStudio:最新版本 R:最新版本 tinytex:最新版本 下面是我的Rmd文件: --- title: "table" author: "Author" date: "2/13/2021" output: pdf_document: latex_engine: xelatex html_document: df_p

尝试创建包含
booktabs
表的pdf时出错。如果我删除表代码,则创建pdf时不会出现问题

RStudio
:最新版本

R
:最新版本

tinytex
:最新版本

下面是我的
Rmd
文件:

---
title: "table"
author: "Author"
date: "2/13/2021"
output:
  pdf_document:
    latex_engine: xelatex
  html_document:
    df_print: paged
includes:
  in_header: preamble.tex
---

Something here

\begin{table}[h]
    \centering
    \caption{Descriptive Statistics}
    \label{tab:descriptiveStatistics}
    {
        \begin{tabular}{lr}
            \toprule
             & Weight  \\
            \cmidrule[0.4pt]{1-2}
            Valid & 13  \\
            Missing & 0  \\
            Mean & 170.769  \\
            Std. Deviation & 29.255  \\
            Minimum & 140.000  \\
            Maximum & 235.000  \\
            \bottomrule
        \end{tabular}
    }
\end{table}
\usepackage{booktabs}
\usepackage{makecell}
下面是我的
序言.tex
文件:

---
title: "table"
author: "Author"
date: "2/13/2021"
output:
  pdf_document:
    latex_engine: xelatex
  html_document:
    df_print: paged
includes:
  in_header: preamble.tex
---

Something here

\begin{table}[h]
    \centering
    \caption{Descriptive Statistics}
    \label{tab:descriptiveStatistics}
    {
        \begin{tabular}{lr}
            \toprule
             & Weight  \\
            \cmidrule[0.4pt]{1-2}
            Valid & 13  \\
            Missing & 0  \\
            Mean & 170.769  \\
            Std. Deviation & 29.255  \\
            Minimum & 140.000  \\
            Maximum & 235.000  \\
            \bottomrule
        \end{tabular}
    }
\end{table}
\usepackage{booktabs}
\usepackage{makecell}
我得到的错误如下:

output file: test.knit.md

! Undefined control sequence.
l.81             \toprule

Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info.
In addition: Warning message:
In has_crop_tools() : 
Tool(s) not installed or not in PATH: pdfcrop, ghostcript
-> As a result, figure cropping will be disabled.
Execution halted

您的
序言.tex
不包括在内,因为您的yaml头是错误的。应该是:

---
title: "table"
author: "Author"
date: "2/13/2021"
output:
  pdf_document:
    latex_engine: xelatex
    includes:
      in_header: preamble.tex
  html_document:
    df_print: paged
---
然后它就起作用了


我还建议使用
kableExtra
。根据我的经验,它基本上生成了LaTeX代码,并为您节省了大量制作表格的时间。您可以设置
keep_tex:yes
(也可以在
pdf\u文档:
)以仔细检查正在生成的内容。

LaTeX表格的另一个解决方案是:谢谢您的建议。然而,我正在写一本关于如何教统计的书。JASP允许将表复制为LaTex(需要booktabs包),其格式如上所示。因此,使用上述格式对我来说比较省时。非常感谢。我注意到它适用于pdf文件,但不适用于文档或html。我是否需要在yaml标题中添加任何内容才能使其工作?提前谢谢。这是我对R Markdown的最大问题:表不是转换成所有格式的
knitr::kable
kableExtra
适用于PDF和HTML。但是如果你想在doc和docx中有好的表,你需要
flextable
或类似的东西。我使用
knitr:::pandoc_to()==“latex”
检查是否有东西被编织成PDF格式,并使用
knitr:::pandoc_to()==“docx”
检查word。