简体   繁体   English

如何编译从R导出的LaTeX表?

[英]How do I compile a LaTeX table exported from R?

I'm completely new to LaTeX and have the MacTeX 2009 package installed, with the intent of having tables from R output into LaTeX and formatted as PDF. 我是LaTeX的新手,安装了MacTeX 2009软件包,目的是将R输出的表格输入LaTeX并格式化为PDF格式。

I get the following LaTeX code (below) when I run an example in R (it renders ok in R, but I´d like to use TeXshop). 当我在R中运行一个例子时,我得到了下面的LaTeX代码(下面)(它在R中呈现好,但我想使用TeXshop)。 However, when I paste this into a TeXshop window, I get the following error: 但是,当我将其粘贴到TeXshop窗口时,我收到以下错误:

./Untitled.tex:2: LaTeX Error: Environment table undefined ./Untitled.tex:2:LaTeX错误:环境表未定义

I´m sure there is something very basic I´m missing here. 我确定这里有一些非常基本的东西。

% latex.default(cstats, title = title, caption = caption, rowlabel = rowlabel,
%               col.just = col.just, numeric.dollar = FALSE, insert.bottom = legend,
%               rowname = lab, dcolumn = dcolumn, extracolheads = extracolheads,
%               extracolsize = Nsize, ...) 
%
\begin{table}[!tbp]
 \caption{Descriptive Statistics by treatment\label{f}} 
 \begin{center}
 \begin{tabular}{lccc}\hline\hline
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Drug}&\multicolumn{1}{c}{Placebo}&\multicolumn{1}{c}{Test Statistic}\tabularnewline

&\multicolumn{1}{c}{{\scriptsize $N=263$}}&\multicolumn{1}{c}{{\scriptsize $N=237$}}&\tabularnewline
\hline
age&{\scriptsize 46.5~}{49.9 }{\scriptsize 53.2} &{\scriptsize 46.7~}{50.0 }{\scriptsize 53.4} &$ F_{1,498}=0.1 ,~ P=0.754 ^{1} $\tabularnewline
sex~:~m&47\%~{\scriptsize~(123)}&44\%~{\scriptsize~(104)}&$ \chi^{2}_{1}=0.42 ,~ P=0.517 ^{2} $\tabularnewline
Primary~Symptoms~:~Depressed&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Headache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Hangnail&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Muscle~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
~~~~Stomach~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$\tabularnewline
\hline
\end{tabular}

\end{center}

\noindent
   {\scriptsize $a$\ }{$b$\ }{\scriptsize $c$\ } represent the lower quartile $a$,
   the median $b$, and the upper quartile $c$\ for continuous variables.\\
   Numbers after percents are frequencies.\\\indent Tests used:\\
  \textsuperscript{\normalfont 1}Wilcoxon test; \textsuperscript{\normalfont 2}Pearson test
\end{table}
  • You need some boilerplate around this to set up the document -- the LaTeXTemplate in TexShop should do. 你需要一些样板来设置文档 - TexShop中的LaTeXTemplate应该这样做。
  • Your \\begin{table} is commented out with a preceding % 你的\\begin{table}被注释掉前面的%
  • There are some missing backslashes in the legend text -- possibly these are just cut & paste artefacts? 图例文本中有一些缺失的反斜杠 - 可能这些只是剪切和粘贴的文物?

This thrown-together version works for me, though you may need to tweak it for your purposes: 这个抛出的版本适用于我,但您可能需要根据您的目的进行调整:

\documentclass[11pt]{article}
\begin{document}

\begin{table}[!tbp]
\caption{Descriptive Statistics by treatment\label{f}}

\begin{center}
\begin{tabular}{lccc}
\hline
\hline
\multicolumn{1}{l}{}&\multicolumn{1}{c}{Drug}&\multicolumn{1}{c}{Placebo}&\multicolumn{1}{c}{Test Statistic}
\tabularnewline
&\multicolumn{1}{c}{{\scriptsize $N=263$}}&\multicolumn{1}{c}{{\scriptsize $N=237$}}&
\tabularnewline
\hline
age&{\scriptsize 46.5~}{49.9 }{\scriptsize 53.2} &{\scriptsize 46.7~}{50.0 }{\scriptsize 53.4} &$ F_{1,498}=0.1 ,~ P=0.754 ^{1}$
\tabularnewline
sex~:~m&47\%~{\scriptsize~(123)}&44\%~{\scriptsize~(104)}&$ \chi^{2}_{1}=0.42 ,~ P=0.517 ^{2} $
\tabularnewline
Primary~Symptoms~:~Depressed&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Headache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Hangnail&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Muscle~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
~~~~Stomach~Ache&0\%~{\scriptsize~(0)}&0\%~{\scriptsize~(0)}&$^{2}$
\tabularnewline
\hline
\end{tabular}
\end{center}

\noindent {\scriptsize $a$\ }{$b$\ }{\scriptsize $c$\ } represent the lower quartile $a$, the median $b$, and the upper quartile $c$ for continuous variables.\\
Numbers after percents are frequencies.\\
\indent Tests used:\\
\textsuperscript{\normalfont 1}Wilcoxon test;
\textsuperscript{\normalfont 2}Pearson test

\end{table}
\end{document}  

If you use the package stargazer (available on CRAN), you can use the argument out="filename.tex" to output the LaTeX code directly into a Tex document. 如果使用包stargazer(在CRAN上可用),则可以使用参数out =“filename.tex”将LaTeX代码直接输出到Tex文档中。 That document should then be easy to compile. 那么该文档应该易于编译。

听起来您可能没有定义文档类,或者您没有使用定义表环境的文档类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM