如何将 R(xtable)中的大数据框导入 LaTeX 以创建 PDF/PNG?

如何将 R(xtable)中的大数据框导入 LaTeX 以创建 PDF/PNG?

我对 R 有相当多的经验,但对 LaTeX 却一无所知。因此,任何帮助都将不胜感激。我在 R 中有一个数据框,并使用以下代码行将其导出到控制台:

print(xtable(plot_data),floating=FALSE,latex.environments=NULL,booktabs=TRUE)

输出结果如下:

\begin{tabular}{rlrrrrrl}
  \toprule
 & model & CFPs & model\_size & number\_of\_exchange\_reactions & viable\_environments & fraction & gs \\ 
  \midrule
1 & e\_coli\_core &   0 &  97 &  20 &   0 & 0.00 &  \\ 
  2 & iAF1260 &  53 & 2290 & 292 & 188 & 0.36 & generalist \\ 
  3 & iAF1260b &  57 & 2296 & 292 & 188 & 0.36 & generalist \\ 
  4 & iAF692 &   0 & 622 &  68 &  11 & 0.02 & specialist \\ 
  5 & iAF987 &   0 & 1222 &  88 &  13 & 0.02 & specialist \\ 
  6 & iAPECO1\_1312 &  59 & 2550 & 350 & 188 & 0.36 & generalist \\ 
  7 & iB21\_1397 &  56 & 2564 & 350 & 189 & 0.36 & generalist \\ 
  8 & iBWG\_1329 &  58 & 2566 & 350 & 188 & 0.36 & generalist \\ 
  9 & ic\_1306 &   0 & 2543 & 350 &  11 & 0.02 & specialist \\ 
  10 & iE2348C\_1286 &  57 & 2533 & 350 & 187 & 0.36 & generalist \\ 
  11 & iEC042\_1314 &  54 & 2545 & 350 & 184 & 0.35 & generalist \\ 
  12 & iEC55989\_1330 &  59 & 2568 & 350 & 192 & 0.37 & generalist \\ 
  13 & iECABU\_c1320 &  59 & 2548 & 350 & 187 & 0.36 & generalist \\ 
  14 & iECB\_1328 &  56 & 2571 & 350 & 190 & 0.36 & generalist \\ 
  15 & iECBD\_1354 &  56 & 2571 & 350 & 189 & 0.36 & generalist \\ 
  16 & iECD\_1391 &  56 & 2564 & 350 & 189 & 0.36 & generalist \\ 
  17 & iECDH10B\_1368 &   0 & 2567 & 350 &   8 & 0.02 & specialist \\ 
  18 & iEcDH1\_1363 &  59 & 2576 & 350 & 190 & 0.36 & generalist \\ 
  19 & iECDH1ME8569\_1439 &  59 & 2580 & 350 & 190 & 0.36 & generalist \\ 
  20 & iEcE24377\_1341 &  55 & 2568 & 350 & 191 & 0.36 & generalist \\ 
  21 & iECED1\_1282 &  57 & 2537 & 350 & 181 & 0.34 & generalist \\ 
  22 & iECH74115\_1262 &  53 & 2533 & 350 & 181 & 0.34 & generalist \\ 
  23 & iEcHS\_1320 &  55 & 2562 & 350 & 189 & 0.36 & generalist \\ 
  24 & iECIAI1\_1343 &  51 & 2552 & 344 & 185 & 0.35 & generalist \\ 
  25 & iECIAI39\_1322 &  51 & 2532 & 350 & 184 & 0.35 & generalist \\ 
  26 & iECNA114\_1301 &  57 & 2550 & 350 & 190 & 0.36 & generalist \\ 
  27 & iECO103\_1326 &  57 & 2573 & 350 & 192 & 0.37 & generalist \\ 
  28 & iECO111\_1330 &  57 & 2573 & 350 & 190 & 0.36 & generalist \\ 
  29 & iECO26\_1355 &  57 & 2592 & 350 & 193 & 0.37 & generalist \\ 
  30 & iECOK1\_1307 &  59 & 2544 & 350 & 186 & 0.35 & generalist \\ 
  31 & iEcolC\_1368 &  54 & 2581 & 350 & 191 & 0.36 & generalist \\ 
  32 & iECP\_1309 &  59 & 2557 & 350 & 192 & 0.37 & generalist \\ 
  33 & iECs\_1301 &  54 & 2555 & 350 & 189 & 0.36 & generalist \\ 
  34 & iECS88\_1305 &  57 & 2544 & 350 & 187 & 0.36 & generalist \\ 
  35 & iECSE\_1348 &  57 & 2581 & 350 & 193 & 0.37 & generalist \\ 
  36 & iECSF\_1327 &  59 & 2567 & 350 & 190 & 0.36 & generalist \\ 
  37 & iEcSMS35\_1347 &  59 & 2562 & 350 & 190 & 0.36 & generalist \\ 
  38 & iECSP\_1301 &  54 & 2546 & 350 & 189 & 0.36 & generalist \\ 
  39 & iECUMN\_1333 &  57 & 2568 & 350 & 191 & 0.36 & generalist \\ 
  40 & iECW\_1372 &  56 & 2585 & 350 & 192 & 0.37 & generalist \\ 
  41 & iEKO11\_1354 &  56 & 2583 & 350 & 192 & 0.37 & generalist \\ 
  42 & iETEC\_1333 &  57 & 2565 & 350 & 190 & 0.36 & generalist \\ 
  43 & iG2583\_1286 &  54 & 2537 & 350 & 185 & 0.35 & generalist \\ 
  44 & iHN637 &  13 & 759 &  94 &  57 & 0.11 & specialist \\ 
  45 & iIT341 &   0 & 529 &  73 &   6 & 0.01 & specialist \\ 
  46 & iJN678 &   8 & 712 &  47 &  34 & 0.06 & specialist \\ 
  47 & iJN746 &  14 & 1012 &  87 &  89 & 0.17 & specialist \\ 
  48 & iJO1366 &  58 & 2451 & 307 & 191 & 0.36 & generalist \\ 
  49 & iJR904 &  32 & 1049 & 140 & 141 & 0.27 & generalist \\ 
  50 & iLF82\_1304 &  60 & 2542 & 350 & 186 & 0.35 & generalist \\ 
  51 & iLJ478 &   0 & 606 &  74 &   0 & 0.00 &  \\ 
  52 & iMM904 &  31 & 1331 & 162 & 114 & 0.22 & generalist \\ 
  53 & iND750 &  22 & 1076 & 115 &  91 & 0.17 & specialist \\ 
  54 & iNJ661 &  16 & 984 &  84 &  78 & 0.15 & specialist \\ 
  55 & iNRG857\_1313 &  60 & 2549 & 350 & 186 & 0.35 & generalist \\ 
  56 & iPC815 &   0 & 1875 & 274 &   8 & 0.02 & specialist \\ 
  57 & iRC1080 &   0 & 1404 &  18 &  18 & 0.03 & specialist \\ 
  58 & iS\_1188 &  43 & 2456 & 350 & 172 & 0.33 & generalist \\ 
  59 & iSB619 &   6 & 675 &  83 &   8 & 0.02 & specialist \\ 
  60 & iSbBS512\_1146 &  43 & 2430 & 350 & 148 & 0.28 & generalist \\ 
  61 & iSBO\_1134 &  39 & 2436 & 350 & 158 & 0.30 & generalist \\ 
  62 & iSDY\_1059 &  43 & 2382 & 350 & 147 & 0.28 & generalist \\ 
  63 & iSF\_1195 &   0 & 2468 & 350 &   9 & 0.02 & specialist \\ 
  64 & iSFV\_1184 &  41 & 2457 & 350 & 167 & 0.32 & generalist \\ 
  65 & iSFxv\_1172 &  45 & 2475 & 350 & 168 & 0.32 & generalist \\ 
  66 & iSSON\_1240 &  47 & 2521 & 350 & 175 & 0.33 & generalist \\ 
  67 & iUMN146\_1321 &  59 & 2550 & 350 & 186 & 0.35 & generalist \\ 
  68 & iUMNK88\_1353 &  57 & 2581 & 350 & 191 & 0.36 & generalist \\ 
  69 & iUTI89\_1310 &  59 & 2541 & 350 & 186 & 0.35 & generalist \\ 
  70 & iWFL\_1372 &  56 & 2585 & 350 & 192 & 0.37 & generalist \\ 
  71 & iY75\_1357 &  59 & 2584 & 350 & 190 & 0.36 & generalist \\ 
  72 & iYL1228 &  29 & 2146 & 272 & 157 & 0.30 & generalist \\ 
  73 & iYO844 &  34 & 1062 & 186 & 191 & 0.36 & generalist \\ 
  74 & iZ\_1308 &  54 & 2556 & 350 & 189 & 0.36 & generalist \\ 
  75 & STM\_v1\_0 &  46 & 2407 & 326 & 170 & 0.32 & generalist \\ 
  76 & iSM199 &   0 & 278 &  36 &   0 & 0.00 &  \\ 
   \bottomrule
\end{tabular}

现在,最难的部分来了。我知道,这必须导入到 LaTeX 中。我必须添加哪些代码行才能使其工作并以正确的方式格式化表格(以便我可以直接将其用作或.pdf.png

我已经下载了 TeXmaker,也可以使用在线版本的 LaTeX

答案1

为了使您的代码编译成 pdf 文档,您可以添加 documentclassstadalone 以及booktabs包(如您使用的\toprule\midrule\bottomrule来自这个包):

\documentclass{standalone}
\usepackage{booktabs}

\begin{document}
\begin{tabular}{rlrrrrrl}
  \toprule
 & model & CFPs & model\_size & number\_of\_exchange\_reactions & viable\_environments & fraction & gs \\ 
  \midrule
1 & e\_coli\_core &   0 &  97 &  20 &   0 & 0.00 &  \\ 
  2 & iAF1260 &  53 & 2290 & 292 & 188 & 0.36 & generalist \\ 
  3 & iAF1260b &  57 & 2296 & 292 & 188 & 0.36 & generalist \\ 
  4 & iAF692 &   0 & 622 &  68 &  11 & 0.02 & specialist \\ 
  5 & iAF987 &   0 & 1222 &  88 &  13 & 0.02 & specialist \\ 
  6 & iAPECO1\_1312 &  59 & 2550 & 350 & 188 & 0.36 & generalist \\ 
  7 & iB21\_1397 &  56 & 2564 & 350 & 189 & 0.36 & generalist \\ 
  8 & iBWG\_1329 &  58 & 2566 & 350 & 188 & 0.36 & generalist \\ 
  9 & ic\_1306 &   0 & 2543 & 350 &  11 & 0.02 & specialist \\ 
  10 & iE2348C\_1286 &  57 & 2533 & 350 & 187 & 0.36 & generalist \\ 
  11 & iEC042\_1314 &  54 & 2545 & 350 & 184 & 0.35 & generalist \\ 
  12 & iEC55989\_1330 &  59 & 2568 & 350 & 192 & 0.37 & generalist \\ 
  13 & iECABU\_c1320 &  59 & 2548 & 350 & 187 & 0.36 & generalist \\ 
  14 & iECB\_1328 &  56 & 2571 & 350 & 190 & 0.36 & generalist \\ 
  15 & iECBD\_1354 &  56 & 2571 & 350 & 189 & 0.36 & generalist \\ 
  16 & iECD\_1391 &  56 & 2564 & 350 & 189 & 0.36 & generalist \\ 
  17 & iECDH10B\_1368 &   0 & 2567 & 350 &   8 & 0.02 & specialist \\ 
  18 & iEcDH1\_1363 &  59 & 2576 & 350 & 190 & 0.36 & generalist \\ 
  19 & iECDH1ME8569\_1439 &  59 & 2580 & 350 & 190 & 0.36 & generalist \\ 
  20 & iEcE24377\_1341 &  55 & 2568 & 350 & 191 & 0.36 & generalist \\ 
  21 & iECED1\_1282 &  57 & 2537 & 350 & 181 & 0.34 & generalist \\ 
  22 & iECH74115\_1262 &  53 & 2533 & 350 & 181 & 0.34 & generalist \\ 
  23 & iEcHS\_1320 &  55 & 2562 & 350 & 189 & 0.36 & generalist \\ 
  24 & iECIAI1\_1343 &  51 & 2552 & 344 & 185 & 0.35 & generalist \\ 
  25 & iECIAI39\_1322 &  51 & 2532 & 350 & 184 & 0.35 & generalist \\ 
  26 & iECNA114\_1301 &  57 & 2550 & 350 & 190 & 0.36 & generalist \\ 
  27 & iECO103\_1326 &  57 & 2573 & 350 & 192 & 0.37 & generalist \\ 
  28 & iECO111\_1330 &  57 & 2573 & 350 & 190 & 0.36 & generalist \\ 
  29 & iECO26\_1355 &  57 & 2592 & 350 & 193 & 0.37 & generalist \\ 
  30 & iECOK1\_1307 &  59 & 2544 & 350 & 186 & 0.35 & generalist \\ 
  31 & iEcolC\_1368 &  54 & 2581 & 350 & 191 & 0.36 & generalist \\ 
  32 & iECP\_1309 &  59 & 2557 & 350 & 192 & 0.37 & generalist \\ 
  33 & iECs\_1301 &  54 & 2555 & 350 & 189 & 0.36 & generalist \\ 
  34 & iECS88\_1305 &  57 & 2544 & 350 & 187 & 0.36 & generalist \\ 
  35 & iECSE\_1348 &  57 & 2581 & 350 & 193 & 0.37 & generalist \\ 
  36 & iECSF\_1327 &  59 & 2567 & 350 & 190 & 0.36 & generalist \\ 
  37 & iEcSMS35\_1347 &  59 & 2562 & 350 & 190 & 0.36 & generalist \\ 
  38 & iECSP\_1301 &  54 & 2546 & 350 & 189 & 0.36 & generalist \\ 
  39 & iECUMN\_1333 &  57 & 2568 & 350 & 191 & 0.36 & generalist \\ 
  40 & iECW\_1372 &  56 & 2585 & 350 & 192 & 0.37 & generalist \\ 
  41 & iEKO11\_1354 &  56 & 2583 & 350 & 192 & 0.37 & generalist \\ 
  42 & iETEC\_1333 &  57 & 2565 & 350 & 190 & 0.36 & generalist \\ 
  43 & iG2583\_1286 &  54 & 2537 & 350 & 185 & 0.35 & generalist \\ 
  44 & iHN637 &  13 & 759 &  94 &  57 & 0.11 & specialist \\ 
  45 & iIT341 &   0 & 529 &  73 &   6 & 0.01 & specialist \\ 
  46 & iJN678 &   8 & 712 &  47 &  34 & 0.06 & specialist \\ 
  47 & iJN746 &  14 & 1012 &  87 &  89 & 0.17 & specialist \\ 
  48 & iJO1366 &  58 & 2451 & 307 & 191 & 0.36 & generalist \\ 
  49 & iJR904 &  32 & 1049 & 140 & 141 & 0.27 & generalist \\ 
  50 & iLF82\_1304 &  60 & 2542 & 350 & 186 & 0.35 & generalist \\ 
  51 & iLJ478 &   0 & 606 &  74 &   0 & 0.00 &  \\ 
  52 & iMM904 &  31 & 1331 & 162 & 114 & 0.22 & generalist \\ 
  53 & iND750 &  22 & 1076 & 115 &  91 & 0.17 & specialist \\ 
  54 & iNJ661 &  16 & 984 &  84 &  78 & 0.15 & specialist \\ 
  55 & iNRG857\_1313 &  60 & 2549 & 350 & 186 & 0.35 & generalist \\ 
  56 & iPC815 &   0 & 1875 & 274 &   8 & 0.02 & specialist \\ 
  57 & iRC1080 &   0 & 1404 &  18 &  18 & 0.03 & specialist \\ 
  58 & iS\_1188 &  43 & 2456 & 350 & 172 & 0.33 & generalist \\ 
  59 & iSB619 &   6 & 675 &  83 &   8 & 0.02 & specialist \\ 
  60 & iSbBS512\_1146 &  43 & 2430 & 350 & 148 & 0.28 & generalist \\ 
  61 & iSBO\_1134 &  39 & 2436 & 350 & 158 & 0.30 & generalist \\ 
  62 & iSDY\_1059 &  43 & 2382 & 350 & 147 & 0.28 & generalist \\ 
  63 & iSF\_1195 &   0 & 2468 & 350 &   9 & 0.02 & specialist \\ 
  64 & iSFV\_1184 &  41 & 2457 & 350 & 167 & 0.32 & generalist \\ 
  65 & iSFxv\_1172 &  45 & 2475 & 350 & 168 & 0.32 & generalist \\ 
  66 & iSSON\_1240 &  47 & 2521 & 350 & 175 & 0.33 & generalist \\ 
  67 & iUMN146\_1321 &  59 & 2550 & 350 & 186 & 0.35 & generalist \\ 
  68 & iUMNK88\_1353 &  57 & 2581 & 350 & 191 & 0.36 & generalist \\ 
  69 & iUTI89\_1310 &  59 & 2541 & 350 & 186 & 0.35 & generalist \\ 
  70 & iWFL\_1372 &  56 & 2585 & 350 & 192 & 0.37 & generalist \\ 
  71 & iY75\_1357 &  59 & 2584 & 350 & 190 & 0.36 & generalist \\ 
  72 & iYL1228 &  29 & 2146 & 272 & 157 & 0.30 & generalist \\ 
  73 & iYO844 &  34 & 1062 & 186 & 191 & 0.36 & generalist \\ 
  74 & iZ\_1308 &  54 & 2556 & 350 & 189 & 0.36 & generalist \\ 
  75 & STM\_v1\_0 &  46 & 2407 & 326 & 170 & 0.32 & generalist \\ 
  76 & iSM199 &   0 & 278 &  36 &   0 & 0.00 &  \\ 
   \bottomrule
\end{tabular}
\end{document}

在此处输入图片描述

为了改善表格的布局,我建议将列标题分成至少两行,并使用该siunitx包以便在包含数字的列中更好地对齐:

在此处输入图片描述

\documentclass{standalone}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{rlS[table-format=2]S[table-format=4]S[table-format=3]S[table-format=3]S[table-format=1.2]l}
  \toprule
 & model & CFPs & {model} & {number of} & {viable} & {fraction} & gs \\
 & & & {size} & {exchange reactions} & {environments} & & \\
  \midrule
  1 & e\_coli\_core &   0 &  97 &  20 &   0 & 0.00 &  \\ 
  2 & iAF1260 &  53 & 2290 & 292 & 188 & 0.36 & generalist \\ 
  3 & iAF1260b &  57 & 2296 & 292 & 188 & 0.36 & generalist \\ 
  4 & iAF692 &   0 & 622 &  68 &  11 & 0.02 & specialist \\ 
  5 & iAF987 &   0 & 1222 &  88 &  13 & 0.02 & specialist \\ 
  6 & iAPECO1\_1312 &  59 & 2550 & 350 & 188 & 0.36 & generalist \\ 
  7 & iB21\_1397 &  56 & 2564 & 350 & 189 & 0.36 & generalist \\ 
  8 & iBWG\_1329 &  58 & 2566 & 350 & 188 & 0.36 & generalist \\ 
  9 & ic\_1306 &   0 & 2543 & 350 &  11 & 0.02 & specialist \\ 
  10 & iE2348C\_1286 &  57 & 2533 & 350 & 187 & 0.36 & generalist \\ 
  11 & iEC042\_1314 &  54 & 2545 & 350 & 184 & 0.35 & generalist \\ 
  12 & iEC55989\_1330 &  59 & 2568 & 350 & 192 & 0.37 & generalist \\ 
  13 & iECABU\_c1320 &  59 & 2548 & 350 & 187 & 0.36 & generalist \\ 
  14 & iECB\_1328 &  56 & 2571 & 350 & 190 & 0.36 & generalist \\ 
  15 & iECBD\_1354 &  56 & 2571 & 350 & 189 & 0.36 & generalist \\ 
  16 & iECD\_1391 &  56 & 2564 & 350 & 189 & 0.36 & generalist \\ 
  17 & iECDH10B\_1368 &   0 & 2567 & 350 &   8 & 0.02 & specialist \\ 
  18 & iEcDH1\_1363 &  59 & 2576 & 350 & 190 & 0.36 & generalist \\ 
  19 & iECDH1ME8569\_1439 &  59 & 2580 & 350 & 190 & 0.36 & generalist \\ 
  20 & iEcE24377\_1341 &  55 & 2568 & 350 & 191 & 0.36 & generalist \\ 
  21 & iECED1\_1282 &  57 & 2537 & 350 & 181 & 0.34 & generalist \\ 
  22 & iECH74115\_1262 &  53 & 2533 & 350 & 181 & 0.34 & generalist \\ 
  23 & iEcHS\_1320 &  55 & 2562 & 350 & 189 & 0.36 & generalist \\ 
  24 & iECIAI1\_1343 &  51 & 2552 & 344 & 185 & 0.35 & generalist \\ 
  25 & iECIAI39\_1322 &  51 & 2532 & 350 & 184 & 0.35 & generalist \\ 
  26 & iECNA114\_1301 &  57 & 2550 & 350 & 190 & 0.36 & generalist \\ 
  27 & iECO103\_1326 &  57 & 2573 & 350 & 192 & 0.37 & generalist \\ 
  28 & iECO111\_1330 &  57 & 2573 & 350 & 190 & 0.36 & generalist \\ 
  29 & iECO26\_1355 &  57 & 2592 & 350 & 193 & 0.37 & generalist \\ 
  30 & iECOK1\_1307 &  59 & 2544 & 350 & 186 & 0.35 & generalist \\ 
  31 & iEcolC\_1368 &  54 & 2581 & 350 & 191 & 0.36 & generalist \\ 
  32 & iECP\_1309 &  59 & 2557 & 350 & 192 & 0.37 & generalist \\ 
  33 & iECs\_1301 &  54 & 2555 & 350 & 189 & 0.36 & generalist \\ 
  34 & iECS88\_1305 &  57 & 2544 & 350 & 187 & 0.36 & generalist \\ 
  35 & iECSE\_1348 &  57 & 2581 & 350 & 193 & 0.37 & generalist \\ 
  36 & iECSF\_1327 &  59 & 2567 & 350 & 190 & 0.36 & generalist \\ 
  37 & iEcSMS35\_1347 &  59 & 2562 & 350 & 190 & 0.36 & generalist \\ 
  38 & iECSP\_1301 &  54 & 2546 & 350 & 189 & 0.36 & generalist \\ 
  39 & iECUMN\_1333 &  57 & 2568 & 350 & 191 & 0.36 & generalist \\ 
  40 & iECW\_1372 &  56 & 2585 & 350 & 192 & 0.37 & generalist \\ 
  41 & iEKO11\_1354 &  56 & 2583 & 350 & 192 & 0.37 & generalist \\ 
  42 & iETEC\_1333 &  57 & 2565 & 350 & 190 & 0.36 & generalist \\ 
  43 & iG2583\_1286 &  54 & 2537 & 350 & 185 & 0.35 & generalist \\ 
  44 & iHN637 &  13 & 759 &  94 &  57 & 0.11 & specialist \\ 
  45 & iIT341 &   0 & 529 &  73 &   6 & 0.01 & specialist \\ 
  46 & iJN678 &   8 & 712 &  47 &  34 & 0.06 & specialist \\ 
  47 & iJN746 &  14 & 1012 &  87 &  89 & 0.17 & specialist \\ 
  48 & iJO1366 &  58 & 2451 & 307 & 191 & 0.36 & generalist \\ 
  49 & iJR904 &  32 & 1049 & 140 & 141 & 0.27 & generalist \\ 
  50 & iLF82\_1304 &  60 & 2542 & 350 & 186 & 0.35 & generalist \\ 
  51 & iLJ478 &   0 & 606 &  74 &   0 & 0.00 &  \\ 
  52 & iMM904 &  31 & 1331 & 162 & 114 & 0.22 & generalist \\ 
  53 & iND750 &  22 & 1076 & 115 &  91 & 0.17 & specialist \\ 
  54 & iNJ661 &  16 & 984 &  84 &  78 & 0.15 & specialist \\ 
  55 & iNRG857\_1313 &  60 & 2549 & 350 & 186 & 0.35 & generalist \\ 
  56 & iPC815 &   0 & 1875 & 274 &   8 & 0.02 & specialist \\ 
  57 & iRC1080 &   0 & 1404 &  18 &  18 & 0.03 & specialist \\ 
  58 & iS\_1188 &  43 & 2456 & 350 & 172 & 0.33 & generalist \\ 
  59 & iSB619 &   6 & 675 &  83 &   8 & 0.02 & specialist \\ 
  60 & iSbBS512\_1146 &  43 & 2430 & 350 & 148 & 0.28 & generalist \\ 
  61 & iSBO\_1134 &  39 & 2436 & 350 & 158 & 0.30 & generalist \\ 
  62 & iSDY\_1059 &  43 & 2382 & 350 & 147 & 0.28 & generalist \\ 
  63 & iSF\_1195 &   0 & 2468 & 350 &   9 & 0.02 & specialist \\ 
  64 & iSFV\_1184 &  41 & 2457 & 350 & 167 & 0.32 & generalist \\ 
  65 & iSFxv\_1172 &  45 & 2475 & 350 & 168 & 0.32 & generalist \\ 
  66 & iSSON\_1240 &  47 & 2521 & 350 & 175 & 0.33 & generalist \\ 
  67 & iUMN146\_1321 &  59 & 2550 & 350 & 186 & 0.35 & generalist \\ 
  68 & iUMNK88\_1353 &  57 & 2581 & 350 & 191 & 0.36 & generalist \\ 
  69 & iUTI89\_1310 &  59 & 2541 & 350 & 186 & 0.35 & generalist \\ 
  70 & iWFL\_1372 &  56 & 2585 & 350 & 192 & 0.37 & generalist \\ 
  71 & iY75\_1357 &  59 & 2584 & 350 & 190 & 0.36 & generalist \\ 
  72 & iYL1228 &  29 & 2146 & 272 & 157 & 0.30 & generalist \\ 
  73 & iYO844 &  34 & 1062 & 186 & 191 & 0.36 & generalist \\ 
  74 & iZ\_1308 &  54 & 2556 & 350 & 189 & 0.36 & generalist \\ 
  75 & STM\_v1\_0 &  46 & 2407 & 326 & 170 & 0.32 & generalist \\ 
  76 & iSM199 &   0 & 278 &  36 &   0 & 0.00 &  \\ 
   \bottomrule
\end{tabular}
\end{document}

答案2

正如@Fran 在她的评论中所建议的,您可以使用将您的表格直接发布为 pdf knitr

以下 MWE 提供了使用Rxtable和来格式化表格的演示kableExtra

R您可以通过在文件中集成代码块来实现这一点.tex,在这种情况下,您的代码可以保存在.Rnw文件中。

您可以在或的扩展名中编译此.Rnw文件。这将执行代码块并将输出集成到文档中。RStudioRWinEdtRpdf

请注意,您的模型名称包含下划线 ( _),我将其替换为:。下划线必须用 进行转义\LaTeX但在本例中,删除它们更简单。

\begin{filecontents}{mydata.csv}
 , model , CFPs , model_size , number_of_exchange_reactions , viable_environments , fraction , gs
1, e:coli:core ,0,97,20,0,0,
2, iAF1260 ,53,2290,292,188,0.36, generalist
3, iAF1260b ,57,2296,292,188,0.36, generalist
4, iAF692 ,0,622,68,11,0.02, specialist
5, iAF987 ,0,1222,88,13,0.02, specialist
6, iAPECO1:1312 ,59,2550,350,188,0.36, generalist
7, iB21:1397 ,56,2564,350,189,0.36, generalist
8, iBWG:1329 ,58,2566,350,188,0.36, generalist
9, ic:1306 ,0,2543,350,11,0.02, specialist
10, iE2348C:1286 ,57,2533,350,187,0.36, generalist
11, iEC042:1314 ,54,2545,350,184,0.35, generalist
12, iEC55989:1330 ,59,2568,350,192,0.37, generalist
13, iECABU:c1320 ,59,2548,350,187,0.36, generalist
14, iECB:1328 ,56,2571,350,190,0.36, generalist
15, iECBD:1354 ,56,2571,350,189,0.36, generalist
16, iECD:1391 ,56,2564,350,189,0.36, generalist
17, iECDH10B:1368 ,0,2567,350,8,0.02, specialist
18, iEcDH1:1363 ,59,2576,350,190,0.36, generalist
19, iECDH1ME8569:1439 ,59,2580,350,190,0.36, generalist
20, iEcE24377:1341 ,55,2568,350,191,0.36, generalist
21, iECED1:1282 ,57,2537,350,181,0.34, generalist
22, iECH74115:1262 ,53,2533,350,181,0.34, generalist
23, iEcHS:1320 ,55,2562,350,189,0.36, generalist
24, iECIAI1:1343 ,51,2552,344,185,0.35, generalist
25, iECIAI39:1322 ,51,2532,350,184,0.35, generalist
26, iECNA114:1301 ,57,2550,350,190,0.36, generalist
27, iECO103:1326 ,57,2573,350,192,0.37, generalist
28, iECO111:1330 ,57,2573,350,190,0.36, generalist
29, iECO26:1355 ,57,2592,350,193,0.37, generalist
30, iECOK1:1307 ,59,2544,350,186,0.35, generalist
31, iEcolC:1368 ,54,2581,350,191,0.36, generalist
32, iECP:1309 ,59,2557,350,192,0.37, generalist
33, iECs:1301 ,54,2555,350,189,0.36, generalist
34, iECS88:1305 ,57,2544,350,187,0.36, generalist
35, iECSE:1348 ,57,2581,350,193,0.37, generalist
36, iECSF:1327 ,59,2567,350,190,0.36, generalist
37, iEcSMS35:1347 ,59,2562,350,190,0.36, generalist
38, iECSP:1301 ,54,2546,350,189,0.36, generalist
39, iECUMN:1333 ,57,2568,350,191,0.36, generalist
40, iECW:1372 ,56,2585,350,192,0.37, generalist
41, iEKO11:1354 ,56,2583,350,192,0.37, generalist
42, iETEC:1333 ,57,2565,350,190,0.36, generalist
43, iG2583:1286 ,54,2537,350,185,0.35, generalist
44, iHN637 ,13,759,94,57,0.11, specialist
45, iIT341 ,0,529,73,6,0.01, specialist
46, iJN678 ,8,712,47,34,0.06, specialist
47, iJN746 ,14,1012,87,89,0.17, specialist
48, iJO1366 ,58,2451,307,191,0.36, generalist
49, iJR904 ,32,1049,140,141,0.27, generalist
50, iLF82:1304 ,60,2542,350,186,0.35, generalist
51, iLJ478 ,0,606,74,0,0,
52, iMM904 ,31,1331,162,114,0.22, generalist
53, iND750 ,22,1076,115,91,0.17, specialist
54, iNJ661 ,16,984,84,78,0.15, specialist
55, iNRG857:1313 ,60,2549,350,186,0.35, generalist
56, iPC815 ,0,1875,274,8,0.02, specialist
57, iRC1080 ,0,1404,18,18,0.03, specialist
58, iS:1188 ,43,2456,350,172,0.33, generalist
59, iSB619 ,6,675,83,8,0.02, specialist
60, iSbBS512:1146 ,43,2430,350,148,0.28, generalist
61, iSBO:1134 ,39,2436,350,158,0.3, generalist
62, iSDY:1059 ,43,2382,350,147,0.28, generalist
63, iSF:1195 ,0,2468,350,9,0.02, specialist
64, iSFV:1184 ,41,2457,350,167,0.32, generalist
65, iSFxv:1172 ,45,2475,350,168,0.32, generalist
66, iSSON:1240 ,47,2521,350,175,0.33, generalist
67, iUMN146:1321 ,59,2550,350,186,0.35, generalist
68, iUMNK88:1353 ,57,2581,350,191,0.36, generalist
69, iUTI89:1310 ,59,2541,350,186,0.35, generalist
70, iWFL:1372 ,56,2585,350,192,0.37, generalist
71, iY75:1357 ,59,2584,350,190,0.36, generalist
72, iYL1228 ,29,2146,272,157,0.3, generalist
73, iYO844 ,34,1062,186,191,0.36, generalist
74, iZ:1308 ,54,2556,350,189,0.36, generalist
75, STM:v1:0 ,46,2407,326,170,0.32, generalist
76, iSM199 ,0,278,36,0,0,
\end{filecontents}

\documentclass[a4paper,10pt]{article}
\usepackage{booktabs}
\usepackage[margin=2.5cm]{geometry}
\usepackage{longtable}
\usepackage{makecell}  % format column headings using kable

\begin{document}
<<knitrOptions,echo=FALSE>>=
# Clear the environment
 rm(list=ls())
# Global knitr options
 knitr::opts_chunk$set(
  results='asis',
  eval=TRUE,
  warning=FALSE,
  include=TRUE,
  echo=FALSE,
  message=FALSE
 )
@

<<Setup>>=
# Load libraries
 library(kableExtra) # format table as \LaTeX code
 library(xtable)     # format table as \LaTeX code
 library(knitr)      # Rnw --> .tex
 library(dplyr)      # rename missing column heading
@
\section{Read the data}
<<ReadTheData>>=
 mydata <- read.csv(file="mytable.csv",header=TRUE, fileEncoding = "UTF-8-BOM")
 cat("Data read")
 mydata <- dplyr::rename(mydata,No=X)
@
\section{Format and print the data}
\subsection{xtable}
<<xtable>>=
# Reformat the headings to remove _'s, which permits breaking the column names in p columns
  newcolumnheadings1 <-  c("No","model","CFPs","model size","number of exchange reactions","viable environments","fraction","gs")
  colnames(mydata) <- newcolumnheadings1
  xmydata <- xtable(mydata,
                   label = "tab:mytable",
                   caption = "My longtable"
)
 align(xmydata) <- "p{0cm}p{0.5cm}p{3cm}p{0.75cm}p{1cm}p{1cm}p{1cm}p{1cm}p{2cm}"

 # Because we have a longtable we need to setup the table header and footer
 #https://stackoverflow.com/a/9350586
 addtorow          <- list()
 addtorow$pos      <- list()
 addtorow$pos[[1]] <- c(0)
 addtorow$command  <- c(paste('\\toprule \n',
                             '\\endhead \n',
                             '\\midrule \n',
                             "\\multicolumn{", dim(xmydata)[2], "}{l}",
                             '{\\footnotesize Continued on next page} \n',
                             '\\endfoot \n',
                             '\\bottomrule \n',
                             '\\endlastfoot \n',sep=''))

 print(xmydata,
       size='small',
       caption.placement='top',
       floating=FALSE,
       include.rownames = FALSE,  # because addtorow will substitute the default row names
       booktabs=TRUE,
       hline.after=c(-1),         # because addtorow will substitute the default hline for the first row
       add.to.row = addtorow,     # this is where you actually make the substitution
       tabular.environment="longtable"
      )
@
\subsection{kable}
 <<kabletheData>>=
# Reformat the long column names with \n to give new lines at appropriate break points
 newcolumnheadings2 <-  c("No","model","CFPs","model\nsize","number of\nexchange\nreactions","viable\nenvironments","fraction","gs")

 mydata %>% dplyr::mutate_all(linebreak) %>%
            kable(format="latex",
                  booktabs = TRUE,
                  escape = F,
                  longtable = T,
                  label = "mytable",
                  caption = "My longtable",
                  col.names=linebreak(newcolumnheadings2,align="c")
                  ) %>%
            kable_styling(latex_options=c("repeat_header"),
                              latex_table_env="longtable")
@
\end{document}

在此处输入图片描述

相关内容