2010-08-14 27 views
3

我完全不熟悉LaTeX,並且安裝了MacTeX 2009軟件包,意圖將R表格輸出到LaTeX中並格式化爲PDF格式。如何編譯從R導出的LaTeX表?

當我在R中運行示例(它在R中呈現正常,但我想使用TeXshop)時,我得到以下LaTeX代碼(下面)。然而,當我粘貼到的TeXShop窗口,我得到了以下錯誤:

./Untitled.tex:2: LaTeX Error: Environment table undefined

我敢肯定有一些非常基本的I'm在這裏失蹤。

% 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} 

回答

5
  • 你需要一些樣板來設置文檔--TexShop中的LaTeXTemplate應該做的。
  • \begin{table}與註釋掉在前%
  • 有傳說中的文本缺少一些反斜槓 - 可能這些都只是削減&粘貼文物?

這拋起一起版本的作品對我來說,雖然你可能需要調整它爲您的目的:

\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} 
+0

下一個百萬 – Misha 2010-08-14 23:24:55

1

聽起來您可能沒有定義文檔類,或者您沒有使用定義表環境的文檔類。

1

如果使用包夜光雲(上CRAN可用),您可以使用參數out =「filename.tex」將LaTeX代碼直接輸出到Tex文檔中。那個文件應該很容易編譯。