R: Print two tables with xtable ()

前端 未结 2 921
情歌与酒
情歌与酒 2021-01-31 13:02

I have data tables (d1 and d2) which I would like to print side by side or on top of each other in latex with their own individual titles. Is it possible to do that directly wit

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 13:15

    I would recommend saving the results as two separate tables in different files (see the file= option to print.xtable()), and then input them into your LaTeX document with any command you find appropriate for your layout (tabular, subfloat, minipage, etc.). This is what I do in general, although I generally rely on LaTeX facilities in the Hmisc package. Should you want only to print them as a standalone PDF, use the standalone class for your document.

    So, here is an example:

    data(tli)
    fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
    print(xtable(fm1), file="ta.tex", floating=FALSE)
    print(xtable(head(tli, n=5)), file="tb.tex", floating=FALSE)
    

    then, a quick tex wrapper (compile with pdflatex):

    \documentclass{article}
    \usepackage{subfig}
    \usepackage{graphicx}
    
    \begin{document}
    
    \begin{table}[ht]
    \centering
    \subfloat[Table x(a)]{\label{tab:tab1a}\scalebox{.5}{\input{./ta}}}\quad
    \subfloat[Table x(b)]{\label{tab:tab1b}\scalebox{.5}{\input{./tb}}}
    \caption{Caption about here}
    \label{tab:tab1}
    \end{table}
    
    \end{document}
    

    Here is the result:

    enter image description here

    Remove the \scalebox command for default (stacked) layout, unless they are narrow enough to fit at their default size, as noted by @David.

    enter image description here

提交回复
热议问题