如何对通过 \input{file} 从文件导入的 longtable 进行 \caption 并使用较小的字体?

如何对通过 \input{file} 从文件导入的 longtable 进行 \caption 并使用较小的字体?

我正在从数据库生成一个无聊的长表并存入文件中。

我的目标是:

  1. 将生成的 longtable 导入到另一个文档中。longtable 数据是动态的,必须定期导入。

  2. 生成一个 \caption 和一个 \label,以便在“表格列表”中编号和列出,以便我稍后引用它。

  3. 以比默认字体更小的字体显示。

实际上,我是使用 \input{file} 命令导入文件的,但当我这样做时,长表在页面末尾被截断,而不是分割到其他页面上。由于 \caption 和字体,我使用的 {table} 环境将长表截断到仅一页。

有什么解决方法?

这是文档:

\documentclass[]{article}
\usepackage{longtable}
\usepackage{booktabs}
\begin{document}
\begin{table}
    \caption{This is my boring table!}
    \footnotesize
\input{long_table}
\end{table}
\end{document}

这是 longtable 文件:

\begin{longtable}{lrrrrl}
    \toprule
    {} &       coef &  std err &       t &  P>|t| &       [95\% Conf. Int.] \\
    \midrule
    \endhead
    \midrule
    \multicolumn{3}{r}{{Continued on next page}} \\
    \midrule
    \endfoot

    \bottomrule
    \endlastfoot
    xxxxxxxxxx &    190.053 &   45.007 &   4.223 &  0.000 &        101.838 278.267 \\
    xxxxxxxxxx &    234.725 &   33.668 &   6.972 &  0.000 &        168.735 300.715 \\
    xxxxxxxxxx &    241.393 &   13.121 &  18.398 &  0.000 &        215.676 267.110 \\
    xxxxxxxxxx &     76.047 &   20.556 &   3.699 &  0.000 &         35.757 116.338 \\
    xxxxxxxxxx &   -904.522 &  214.342 &  -4.220 &  0.000 &     -1324.633 -484.410 \\
    xxxxxxxxxx &   -212.049 &  105.600 &  -2.008 &  0.045 &        -419.027 -5.072 \\
    xxxxxxxxxx &    137.281 &   50.164 &   2.737 &  0.006 &         38.958 235.604 \\
    xxxxxxxxxx &    648.933 &   13.176 &  49.250 &  0.000 &        623.107 674.759 \\
    xxxxxxxxxx &    188.568 &   16.133 &  11.688 &  0.000 &        156.947 220.189 \\
    xxxxxxxxxx &   -261.218 &  133.247 &  -1.960 &  0.050 &        -522.383 -0.053 \\
    xxxxxxxxxx &   -812.618 &   34.558 & -23.515 &  0.000 &      -880.351 -744.885 \\
    xxxxxxxxxx &    359.126 &   55.636 &   6.455 &  0.000 &        250.079 468.172 \\
    xxxxxxxxxx &    -95.857 &   21.652 &  -4.427 &  0.000 &       -138.296 -53.419 \\
    xxxxxxxxxx &    404.747 &   17.077 &  23.701 &  0.000 &        371.275 438.219 \\
    xxxxxxxxxx &   -344.240 &   59.283 &  -5.807 &  0.000 &      -460.436 -228.044 \\
    xxxxxxxxxx &    214.835 &   12.168 &  17.655 &  0.000 &        190.985 238.686 \\
    xxxxxxxxxx &   -422.308 &  133.753 &  -3.157 &  0.002 &      -684.465 -160.150 \\
    xxxxxxxxxx &   -629.722 &   44.771 & -14.065 &  0.000 &      -717.474 -541.970 \\
    xxxxxxxxxx &   -461.403 &   69.544 &  -6.635 &  0.000 &      -597.710 -325.095 \\
    xxxxxxxxxx &   -792.248 &   85.435 &  -9.273 &  0.000 &      -959.701 -624.795 \\
    xxxxxxxxxx &    629.606 &   10.930 &  57.603 &  0.000 &        608.183 651.029 \\
    xxxxxxxxxx &   -196.250 &   59.253 &  -3.312 &  0.001 &       -312.387 -80.114 \\
    xxxxxxxxxx &    192.506 &   32.167 &   5.985 &  0.000 &        129.458 255.554 \\
    xxxxxxxxxx &   -167.596 &   35.931 &  -4.664 &  0.000 &       -238.021 -97.171 \\
    xxxxxxxxxx &     67.379 &   28.500 &   2.364 &  0.018 &         11.519 123.239 \\
    xxxxxxxxxx &    174.771 &   37.575 &   4.651 &  0.000 &        101.123 248.418 \\
    xxxxxxxxxx &    641.865 &   14.618 &  43.908 &  0.000 &        613.213 670.517 \\
    xxxxxxxxxx &   -186.023 &   36.877 &  -5.044 &  0.000 &      -258.301 -113.744 \\
    xxxxxxxxxx &   -294.646 &   68.989 &  -4.271 &  0.000 &      -429.865 -159.426 \\
    xxxxxxxxxx &   -275.843 &  100.096 &  -2.756 &  0.006 &       -472.032 -79.654 \\
    xxxxxxxxxx &   -450.954 &   48.880 &  -9.226 &  0.000 &      -546.759 -355.149 \\
    xxxxxxxxxx &   1079.449 &   19.299 &  55.932 &  0.000 &      1041.622 1117.276 \\
    xxxxxxxxxx &    577.176 &   26.934 &  21.429 &  0.000 &        524.385 629.968 \\
    xxxxxxxxxx &    280.603 &   16.496 &  17.010 &  0.000 &        248.270 312.935 \\
    xxxxxxxxxx &   -348.397 &   46.489 &  -7.494 &  0.000 &      -439.516 -257.278 \\
    xxxxxxxxxx &    130.607 &   49.436 &   2.642 &  0.008 &         33.711 227.503 \\
    xxxxxxxxxx &    830.032 &   18.140 &  45.758 &  0.000 &        794.478 865.587 \\
    xxxxxxxxxx &   -154.888 &   53.009 &  -2.922 &  0.003 &       -258.786 -50.990 \\
    xxxxxxxxxx &   -258.536 &   49.197 &  -5.255 &  0.000 &      -354.962 -162.110 \\
    xxxxxxxxxx & -46445.060 & 2627.676 & -17.675 &  0.000 &  -51595.329 -41294.792 \\
    xxxxxxxxxx &   1033.312 &   11.071 &  93.332 &  0.000 &      1011.612 1055.012 \\
    xxxxxxxxxx &   -112.735 &   23.041 &  -4.893 &  0.000 &       -157.896 -67.575 \\
    xxxxxxxxxx &   -389.974 &   36.853 & -10.582 &  0.000 &      -462.207 -317.742 \\
    xxxxxxxxxx &   -505.563 &   22.554 & -22.416 &  0.000 &      -549.768 -461.357 \\
    xxxxxxxxxx &   -502.710 &  149.070 &  -3.372 &  0.001 &      -794.888 -210.532 \\
    xxxxxxxxxx &    905.849 &   18.408 &  49.209 &  0.000 &        869.769 941.929 \\
    xxxxxxxxxx &   -157.947 &   42.758 &  -3.694 &  0.000 &       -241.754 -74.140 \\
    xxxxxxxxxx &    237.228 &   24.812 &   9.561 &  0.000 &        188.596 285.860 \\
    xxxxxxxxxx &    769.489 &   16.793 &  45.822 &  0.000 &        736.574 802.403 \\
    xxxxxxxxxx &    324.251 &   28.223 &  11.489 &  0.000 &        268.933 379.569 \\
    xxxxxxxxxx &    553.698 &   14.749 &  37.541 &  0.000 &        524.789 582.606 \\
    xxxxxxxxxx &   1172.947 &   11.738 &  99.931 &  0.000 &      1149.941 1195.953 \\
    xxxxxxxxxx &    720.981 &   18.257 &  39.490 &  0.000 &        685.197 756.766 \\
    xxxxxxxxxx &   -480.906 &  172.233 &  -2.792 &  0.005 &      -818.483 -143.328 \\
    xxxxxxxxxx &    372.967 &   17.497 &  21.316 &  0.000 &        338.673 407.260 \\
    xxxxxxxxxx &   -623.032 &  134.301 &  -4.639 &  0.000 &      -886.262 -359.802 \\
    xxxxxxxxxx &    292.422 &   17.282 &  16.921 &  0.000 &        258.550 326.295 \\
    xxxxxxxxxx &    183.805 &   70.209 &   2.618 &  0.009 &         46.195 321.415 \\
    xxxxxxxxxx &   -298.659 &  113.203 &  -2.638 &  0.008 &       -520.539 -76.780 \\
    xxxxxxxxxx &   -833.829 &  297.607 &  -2.802 &  0.005 &     -1417.141 -250.517 \\
    xxxxxxxxxx &   -240.700 &   29.978 &  -8.029 &  0.000 &      -299.458 -181.943 \\
    xxxxxxxxxx &     67.341 &   29.967 &   2.247 &  0.025 &          8.605 126.076 \\
    xxxxxxxxxx &    315.190 &   36.654 &   8.599 &  0.000 &        243.347 387.033 \\
    xxxxxxxxxx &     74.242 &   33.735 &   2.201 &  0.028 &          8.120 140.364 \\
    xxxxxxxxxx &   -122.100 &   18.276 &  -6.681 &  0.000 &       -157.920 -86.280 \\
    xxxxxxxxxx &    247.289 &   30.591 &   8.084 &  0.000 &        187.331 307.248 \\
    xxxxxxxxxx &   -147.680 &   46.793 &  -3.156 &  0.002 &       -239.395 -55.965 \\
    xxxxxxxxxx &   -123.183 &   16.960 &  -7.263 &  0.000 &       -156.424 -89.942 \\
    xxxxxxxxxx &   -113.750 &   34.625 &  -3.285 &  0.001 &       -181.615 -45.886 \\
    xxxxxxxxxx &   -220.143 &   55.661 &  -3.955 &  0.000 &      -329.239 -111.047 \\
    xxxxxxxxxx &    143.251 &   26.470 &   5.412 &  0.000 &         91.370 195.133 \\
    xxxxxxxxxx &   -234.088 &   37.923 &  -6.173 &  0.000 &      -308.418 -159.759 \\
    xxxxxxxxxx &   -206.797 &   21.796 &  -9.488 &  0.000 &      -249.518 -164.076 \\
    xxxxxxxxxx &   -199.208 &   36.613 &  -5.441 &  0.000 &      -270.970 -127.446 \\
    xxxxxxxxxx &    105.551 &    8.369 &  12.612 &  0.000 &         89.148 121.954 \\
    xxxxxxxxxx &    249.302 &   20.778 &  11.998 &  0.000 &        208.576 290.028 \\
    xxxxxxxxxx &   -160.100 &   56.655 &  -2.826 &  0.005 &       -271.144 -49.056 \\
    xxxxxxxxxx &    834.538 &   33.837 &  24.664 &  0.000 &        768.218 900.858 \\
    xxxxxxxxxx &   -445.808 &   34.386 & -12.965 &  0.000 &      -513.205 -378.411 \\
    xxxxxxxxxx &   -647.005 &   40.969 & -15.792 &  0.000 &      -727.306 -566.705 \\
    xxxxxxxxxx &   -300.388 &   44.807 &  -6.704 &  0.000 &      -388.211 -212.565 \\
    xxxxxxxxxx &    239.904 &   50.562 &   4.745 &  0.000 &        140.802 339.005 \\
    xxxxxxxxxx &    233.543 &   10.510 &  22.220 &  0.000 &        212.943 254.144 \\
    xxxxxxxxxx &    138.019 &   48.530 &   2.844 &  0.004 &         42.901 233.138 \\
    xxxxxxxxxx &    275.183 &   24.713 &  11.135 &  0.000 &        226.746 323.621 \\
    xxxxxxxxxx &    294.880 &   29.216 &  10.093 &  0.000 &        237.617 352.143 \\
    xxxxxxxxxx &   -171.848 &   51.323 &  -3.348 &  0.001 &       -272.442 -71.254 \\
    xxxxxxxxxx &      6.368 &    0.044 & 144.977 &  0.000 &            6.282 6.454 \\
    xxxxxxxxxx &      4.067 &    1.932 &   2.105 &  0.035 &            0.280 7.853 \\
    xxxxxxxxxx &    -30.902 &    1.138 & -27.148 &  0.000 &        -33.133 -28.671 \\
    xxxxxxxxxx &     20.010 &    3.489 &   5.736 &  0.000 &          13.173 26.848 \\
    xxxxxxxxxx &     46.163 &    4.671 &   9.883 &  0.000 &          37.008 55.319 \\
    xxxxxxxxxx &    -29.777 &    1.142 & -26.066 &  0.000 &        -32.016 -27.538 \\
    xxxxxxxxxx &     29.551 &    5.907 &   5.002 &  0.000 &          17.972 41.130 \\
    xxxxxxxxxx &     20.843 &    3.152 &   6.612 &  0.000 &          14.665 27.022 \\
    xxxxxxxxxx &     68.192 &    8.942 &   7.626 &  0.000 &          50.665 85.719 \\
    xxxxxxxxxx &     23.079 &    6.485 &   3.559 &  0.000 &          10.367 35.790 \\
    xxxxxxxxxx &    -21.649 &    5.389 &  -4.017 &  0.000 &        -32.212 -11.086 \\
    xxxxxxxxxx &    -50.322 &    4.447 & -11.316 &  0.000 &        -59.038 -41.606 \\
    xxxxxxxxxx &     49.817 &    5.629 &   8.851 &  0.000 &          38.785 60.849 \\
    xxxxxxxxxx &   -126.813 &    8.503 & -14.913 &  0.000 &      -143.479 -110.146 \\
    xxxxxxxxxx &     14.676 &    3.917 &   3.746 &  0.000 &           6.998 22.354 \\
    xxxxxxxxxx &    -31.124 &    8.302 &  -3.749 &  0.000 &        -47.396 -14.853 \\
    xxxxxxxxxx &    -33.252 &    3.689 &  -9.015 &  0.000 &        -40.482 -26.023 \\
    xxxxxxxxxx &    -27.461 &    8.635 &  -3.180 &  0.001 &        -44.386 -10.537 \\
    xxxxxxxxxx &    -16.992 &    6.410 &  -2.651 &  0.008 &         -29.556 -4.428 \\
    xxxxxxxxxx &     47.685 &    5.882 &   8.107 &  0.000 &          36.157 59.214 \\
    xxxxxxxxxx &    -71.662 &   10.369 &  -6.911 &  0.000 &        -91.985 -51.339 \\
    xxxxxxxxxx &    -32.361 &    6.809 &  -4.753 &  0.000 &        -45.706 -19.016 \\
    xxxxxxxxxx &     24.228 &    5.141 &   4.712 &  0.000 &          14.151 34.305 \\
    xxxxxxxxxx &     32.246 &    5.255 &   6.136 &  0.000 &          21.946 42.545 \\
    xxxxxxxxxx &     20.789 &    3.801 &   5.470 &  0.000 &          13.340 28.239 \\
    xxxxxxxxxx &     75.833 &   25.475 &   2.977 &  0.003 &         25.902 125.764 \\
    xxxxxxxxxx &     21.996 &    4.142 &   5.311 &  0.000 &          13.878 30.114 \\
    xxxxxxxxxx &     11.699 &    5.126 &   2.282 &  0.022 &           1.651 21.747 \\
    xxxxxxxxxx &     26.156 &    5.615 &   4.658 &  0.000 &          15.150 37.161 \\
    xxxxxxxxxx &      8.981 &    3.663 &   2.452 &  0.014 &           1.801 16.161 \\
    xxxxxxxxxx &    -27.397 &    8.876 &  -3.087 &  0.002 &        -44.793 -10.001 \\
    xxxxxxxxxx &   -140.205 &   44.420 &  -3.156 &  0.002 &       -227.268 -53.141 \\
    xxxxxxxxxx &      7.622 &    3.307 &   2.305 &  0.021 &           1.141 14.103 \\
    xxxxxxxxxx &     44.832 &    5.892 &   7.610 &  0.000 &          33.285 56.380 \\
    xxxxxxxxxx &    136.703 &    3.822 &  35.766 &  0.000 &        129.212 144.194 \\
    xxxxxxxxxx &    204.631 &    5.250 &  38.975 &  0.000 &        194.340 214.922 \\
    xxxxxxxxxx &    290.717 &    8.676 &  33.508 &  0.000 &        273.712 307.722 \\
    xxxxxxxxxx &    368.535 &   17.776 &  20.732 &  0.000 &        333.694 403.376 \\
    xxxxxxxxxx &  -2211.671 &  115.862 & -19.089 &  0.000 &    -2438.761 -1984.581 \\
    xxxxxxxxxx &     76.353 &    5.268 &  14.493 &  0.000 &          66.027 86.679 \\
    xxxxxxxxxx &    121.706 &    5.892 &  20.657 &  0.000 &        110.158 133.254 \\
    xxxxxxxxxx &    233.914 &    8.215 &  28.474 &  0.000 &        217.812 250.015 \\
    xxxxxxxxxx &     -0.029 &    0.007 &  -4.340 &  0.000 &          -0.043 -0.016 \\
    xxxxxxxxxx &      0.157 &    0.009 &  16.817 &  0.000 &            0.138 0.175 \\
    xxxxxxxxxx &  -5354.493 &  179.798 & -29.781 &  0.000 &    -5706.899 -5002.088 \\
    xxxxxxxxxx &      0.119 &    0.005 &  26.459 &  0.000 &            0.110 0.128 \\
    xxxxxxxxxx &     87.671 &    3.384 &  25.911 &  0.000 &          81.039 94.303 \\
    xxxxxxxxxx &    105.792 &    5.954 &  17.768 &  0.000 &         94.122 117.462 \\
    xxxxxxxxxx &    150.747 &    9.240 &  16.315 &  0.000 &        132.637 168.857 \\
    xxxxxxxxxx &    254.335 &   15.049 &  16.900 &  0.000 &        224.839 283.832 \\
\end{longtable}

这是由 Latex 生成的 PDF。

这是生成的文档,预计会有更多页面。

答案1

您提到表格数据是动态的,偶尔会重新生成,并存储在外部文件中。然而,结构表格的列数以及表头和表尾的材料是恒定的,对吗?

如果是这种情况,请确保仅将表格数据写入外部文件。正如您所发现的,将 放在 里面并不是一个好主意longtabletablelongtable有自己的生成标题的机制。如果整个需要具有与周围材料不同的字体大小,请确保用和语句longtable包围整个并在 之后立即执行 fontsize-changing 命令。longtable\begingroup\endgroup\begingroup

以下是您可以采取的建议:

\documentclass{article}
\usepackage{longtable,booktabs}
\begin{document}

\begingroup
\footnotesize
\begin{longtable}{@{}lrrrrl@{}}
    \caption{This is my boring table!} \label{tab:really_long}\\
    \toprule
    & coef & std err & $t$-stat & $p$-value & [95\% Conf.\ Int.] \\
    \midrule
    \endfirsthead

    \multicolumn{6}{@{}l}{(Table \ref{tab:really_long}, continued)}\\
    \addlinespace   
    \toprule
    & coef & std err & $t$-stat & $p$-value & [95\% Conf.\ Int.] \\
    \midrule
    \endhead

    \midrule
    \multicolumn{6}{r@{}}{(Continued on next page)} \\
    \endfoot

    \bottomrule
    \endlastfoot

\input mylongtable  % input tabular data from external file

\end{longtable}
\endgroup  % be sure to leave a blank line after this statement

\end{document}

答案2

如果生成了表数据,您应该能够进行设置,以便输入文件的起始部分是

\begin{longtable}{lrrrrrr}
\caption{\thelongtablecaption}\\[\bigskipamount]
\toprule
{} & coef & std err & $t$ & $P>|t|$ & \multicolumn{2}{c}{[95\% Conf. Int.]} \\
\midrule
\endfirsthead
\toprule
{} & coef & std err & $t$ & $P>|t|$ & \multicolumn{2}{c}{[95\% Conf. Int.]} \\
\midrule
\endhead
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot

这样,当文件实际通过适当定义的命令输入到主文件中时,就可以定义标题文本

\newcommand{\inputlongtable}[3][]{%
  % #1 (optional) is for general declarations
  % #2 (mandatory) is the file name
  % #3 (mandatory) is the caption text
  \begingroup
  \newcommand\thelongtablecaption{#3}%
  #1\input{#2}%
  \endgroup
}

这是一个完整的例子,其中输入文件已经包含在示例中filecontents*,但这对您来说当然不是必需的:任何外部文件都可以通过这种方式输入,只要它具有预期的结构。

\begin{filecontents*}{\jobname_long_table.tex}
\begin{longtable}{lrrrrrr}
\caption{\thelongtablecaption}\\[\bigskipamount]
\toprule
{} & coef & std err & $t$ & $P>|t|$ & \multicolumn{2}{c}{[95\% Conf. Int.]} \\
\midrule
\endfirsthead
\toprule
{} & coef & std err & $t$ & $P>|t|$ & \multicolumn{2}{c}{[95\% Conf. Int.]} \\
\midrule
\endhead
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\midrule
\endfoot

\bottomrule
\endlastfoot
xxxxxxxxxx &  190.053 & 45.007 &  4.223 & 0.000 & 101.838 & 278.267 \\
xxxxxxxxxx &  234.725 & 33.668 &  6.972 & 0.000 & 168.735 & 300.715 \\
xxxxxxxxxx &  241.393 & 13.121 & 18.398 & 0.000 & 215.676 & 267.110 \\
xxxxxxxxxx &   76.047 & 20.556 &  3.699 & 0.000 &  35.757 & 116.338 \\
\end{longtable}
\end{filecontents*}


\documentclass[]{article}
\usepackage{longtable}
\usepackage{booktabs}

\newcommand{\inputlongtable}[3][]{%
  % #1 (optional) is for general declarations
  % #2 (mandatory) is the file name
  % #3 (mandatory) is the caption text
  \begingroup
  \newcommand\thelongtablecaption{#3}%
  #1\input{#2}%
  \endgroup
}

\begin{document}

\inputlongtable{\jobname_long_table}
  {This is my boring table!\label{boring}}

\end{document}

在此处输入图片描述

相关内容