长表中的文本超出单元格范围

长表中的文本超出单元格范围

我在 Latex 中使用了一个长表格,发现在两个页面的拼接处,单元格中的文字超出了表格的范围,有谁知道如何解决这个问题?如图所示,红圈中,“C26”应该在第一列的单元格中。

在此处输入图片描述

这是我使用的代码:

\documentclass{article}
\renewcommand{\baselinestretch}{1.25} 
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usetikzlibrary{shapes, arrows}
\graphicspath{{./images/}}
\usepackage[width=.8\textwidth]{caption}
\captionsetup{justification=centering}
\usepackage{subcaption}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathrsfs}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage{apacite}
\usepackage{longtable}  % for long table
\usepackage{multirow}
\usepackage{wrapfig}  % for wrapped text around tables or figures
\usepackage{xcolor}  % for highlights of text
\begin{document}
\begin{center}
    \begin{longtable}{p{1.8cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}}
        \caption{Improvements by using DPTP and fixed presets of all benchmark functions} \label{table:result_improvement}                                                                                                                                                                                                                                          \\
        \toprule \multicolumn{1}{p{1.8cm}|}{\textbf{Benchmark \newline function}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Experi. setup}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Initial fitness}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Final fitness}} & \multicolumn{1}{p{2.5cm}|}{\textbf{Improvements}} & \multicolumn{1}{p{2.1cm}}{\textbf{Improvement percentage}} \\ \hline
        \endfirsthead

        \multicolumn{6}{c}
        {{ \tablename\ \thetable{} -- continued from previous page}}                                                                                                                                                                                                                                                                                                \\

        \toprule \multicolumn{1}{p{1.8cm}|}{\textbf{Benchmark \newline function}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Experi. setup}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Initial fitness}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Final fitness}} & \multicolumn{1}{p{2.5cm}|}{\textbf{Improvements}} & \multicolumn{1}{p{2.1cm}}{\textbf{Improvement percentage}} \\ \hline
        \endhead

        \hline \multicolumn{6}{r}{{Continued on next page}}                                                                                                                                                                                                                                                                                                         \\
        \endfoot

        \bottomrule
        \endlastfoot
        % Benchmark function    & Experi. setup & Initial fitness & Final fitness & Improvements & Improvement percentage    \\ \hline
        \multirow{11}{*}{C23}
                                                                                  & DPTP                                               & 26992.13                                             & 2693.673                                           & 24298.46                                          & 90.02\%                                                    \\
                                                                                  & Preset 0                                           & 25789.55                                             & 2756.774                                           & 23032.78                                          & \colorbox{lightgray}{89.31\%}                              \\
                                                                                  & Preset 1                                           & 26236.34                                             & 2676.916                                           & 23559.42                                          & \colorbox{lightgray}{89.80\%}                              \\
                                                                                  & Preset 2                                           & 27330.98                                             & 2737.67                                            & 24593.31                                          & \colorbox{lightgray}{89.98\%}                              \\
                                                                                  & Preset 3                                           & 25465.2                                              & 2735.844                                           & 22729.35                                          & \colorbox{lightgray}{89.26\%}                              \\
                                                                                  & Preset 4                                           & 25977.27                                             & 2678.884                                           & 23298.38                                          & \colorbox{lightgray}{89.69\%}                              \\
                                                                                  & Preset 5                                           & 25361.18                                             & 2702.239                                           & 22658.95                                          & \colorbox{lightgray}{89.34\%}                              \\
                                                                                  & Preset 6                                           & 26466.49                                             & 2695.999                                           & 23770.49                                          & \colorbox{lightgray}{89.81\%}                              \\
                                                                                  & Preset 7                                           & 26699.03                                             & 2767.44                                            & 23931.59                                          & \colorbox{lightgray}{89.63\%}                              \\
                                                                                  & Preset 8                                           & 26699.03                                             & 2767.44                                            & 23931.59                                          & \colorbox{lightgray}{89.63\%}                              \\
                                                                                  & Preset 9                                           & 27365.9                                              & 2695.156                                           & 24670.75                                          & 90.15\%                                                    \\  \hline

        \multirow{11}{*}{C24}
                                                                                  & DPTP                                               & 41321.11                                             & 20639.41                                           & 20681.7                                           & 50.05\%                                                    \\
                                                                                  & Preset 0                                           & 41543.73                                             & 21680.46                                           & 19863.27                                          & \colorbox{lightgray}{47.81\%}                              \\
                                                                                  & Preset 1                                           & 41117.98                                             & 19592.93                                           & 21525.04                                          & 52.35\%                                                    \\
                                                                                  & Preset 2                                           & 41557.57                                             & 21347.44                                           & 20210.13                                          & \colorbox{lightgray}{48.63\%}                              \\
                                                                                  & Preset 3                                           & 41312.02                                             & 20930.71                                           & 20381.31                                          & \colorbox{lightgray}{49.34\%}                              \\
                                                                                  & Preset 4                                           & 40669.04                                             & 19755.83                                           & 20913.2                                           & 51.42\%                                                    \\
                                                                                  & Preset 5                                           & 41373.19                                             & 19990.72                                           & 21382.47                                          & 51.68\%                                                    \\
                                                                                  & Preset 6                                           & 41055.92                                             & 19605.2                                            & 21450.72                                          & 52.25\%                                                    \\
                                                                                  & Preset 7                                           & 41092.94                                             & 21357.93                                           & 19735.02                                          & \colorbox{lightgray}{48.03\%}                              \\
                                                                                  & Preset 8                                           & 40650.55                                             & 20069.71                                           & 20580.84                                          & 50.63\%                                                    \\
                                                                                  & Preset 9                                           & 40413.35                                             & 20360.37                                           & 20052.98                                          & \colorbox{lightgray}{49.62\%}                              \\ \hline

        \multirow{11}{*}{C25}
                                                                                  & DPTP                                               & 7360.97                                              & 3843.79                                            & 3517.18                                           & 47.78\%                                                    \\
                                                                                  & Preset 0                                           & 7349.66                                              & 3765.728                                           & 3583.933                                          & 48.76\%                                                    \\
                                                                                  & Preset 1                                           & 7301.543                                             & 3807.63                                            & 3493.913                                          & 47.85\%                                                    \\
                                                                                  & Preset 2                                           & 7236.729                                             & 3782.24                                            & 3454.489                                          & \colorbox{lightgray}{47.74\%}                              \\
                                                                                  & Preset 3                                           & 7296.342                                             & 3794.346                                           & 3501.996                                          & 48.00\%                                                    \\
                                                                                  & Preset 4                                           & 7384.173                                             & 3817.764                                           & 3566.41                                           & 48.30\%                                                    \\
                                                                                  & Preset 5                                           & 7389.313                                             & 3766.736                                           & 3622.577                                          & 49.02\%                                                    \\
                                                                                  & Preset 6                                           & 7507.889                                             & 3783.694                                           & 3724.196                                          & 49.60\%                                                    \\
                                                                                  & Preset 7                                           & 7239.824                                             & 3967.741                                           & 3272.083                                          & \colorbox{lightgray}{45.20\%}                              \\
                                                                                  & Preset 8                                           & 7078.604                                             & 3789.462                                           & 3289.143                                          & \colorbox{lightgray}{46.47\%}                              \\
                                                                                  & Preset 9                                           & 7255.945                                             & 3799.114                                           & 3456.832                                          & \colorbox{lightgray}{47.64\%}                              \\  \hline

        \multirow{11}{*}{C26}
                                                                                  & DPTP                                               & 24366.9                                              & 13030.68                                           & 11336.22                                          & 46.52\%                                                    \\
                                                                                  & Preset 0                                           & 24233.22                                             & 14123.02                                           & 10110.2                                           & \colorbox{lightgray}{41.72\%}                              \\
                                                                                  & Preset 1                                           & 24091.92                                             & 12402.09                                           & 11689.83                                          & 48.52\%                                                    \\
                                                                                  & Preset 2                                           & 24616.94                                             & 14049.56                                           & 10567.38                                          & \colorbox{lightgray}{42.93\%}                              \\
                                                                                  & Preset 3                                           & 24021.75                                             & 13447.69                                           & 10574.06                                          & \colorbox{lightgray}{44.02\%}                              \\
                                                                                  & Preset 4                                           & 24349.25                                             & 12634.16                                           & 11715.09                                          & 48.11\%                                                    \\
                                                                                  & Preset 5                                           & 24494.97                                             & 12632.68                                           & 11862.29                                          & 48.43\%                                                    \\
                                                                                  & Preset 6                                           & 24542.86                                             & 12623.4                                            & 11919.45                                          & 48.57\%                                                    \\
                                                                                  & Preset 7                                           & 24095.84                                             & 13085.9                                            & 11009.94                                          & \colorbox{lightgray}{45.69\%}                              \\
                                                                                  & Preset 8                                           & 24124.98                                             & 12615.13                                           & 11509.85                                          & 47.71\%                                                    \\
                                                                                  & Preset 9                                           & 24381.49                                             & 13221.99                                           & 11159.5                                           & \colorbox{lightgray}{45.77\%}                              \\  \hline
    \end{longtable}
\end{center}
\end{document}

答案1

我发现缺少两个包裹
看看这是否是你要找的东西!

您需要加载该booktabs包以便能够使用\toprule\midrule\bottomrule

\documentclass{article}
\renewcommand{\baselinestretch}{1.25} 
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz} % <--- Package Tikz was missing 
\usetikzlibrary{shapes, arrows}
\usepackage{booktabs} % <--- Package Booktabs was missing
\graphicspath{{./images/}}
\usepackage[width=.8\textwidth]{caption}
\captionsetup{justification=centering}
\usepackage{subcaption}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathrsfs}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage{apacite}
\usepackage{longtable}  % for long table
\usepackage{multirow}
\usepackage{wrapfig}  % for wrapped text around tables or figures
\usepackage{xcolor}  % for highlights of text
 
\begin{document}
\begin{center}
    \begin{longtable}{p{1.8cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}|p{1.5cm}}
        \caption{Improvements by using DPTP and fixed presets of all benchmark functions} \label{table:result_improvement}                                                                                                                                                                                                                                          \\
        \toprule 
        \multicolumn{1}{p{1.8cm}|}{\textbf{Benchmark \newline function}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Experi. setup}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Initial fitness}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Final fitness}} & \multicolumn{1}{p{2.5cm}|}{\textbf{Improvements}} & \multicolumn{1}{p{2.1cm}}{\textbf{Improvement percentage}} \\ \hline
        \endfirsthead

        \multicolumn{6}{c}
        {{ \tablename\ \thetable{} -- continued from previous page}}                                                                                                                                                                                                                                                                                                \\

        \toprule 
        \multicolumn{1}{p{1.8cm}|}{\textbf{Benchmark \newline function}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Experi. setup}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Initial fitness}} & \multicolumn{1}{p{1.5cm}|}{\textbf{Final fitness}} & \multicolumn{1}{p{2.5cm}|}{\textbf{Improvements}} & \multicolumn{1}{p{2.1cm}}{\textbf{Improvement percentage}} \\ \hline
        \endhead

        \hline \multicolumn{6}{r}{{Continued on next page}}                                                                                                                                                                                                                                                                                                         \\
        \endfoot

        \bottomrule
        \endlastfoot
        % Benchmark function    & Experi. setup & Initial fitness & Final fitness & Improvements & Improvement percentage    \\ \hline
        \multirow{11}{*}{C23}
                                                                                  & DPTP                                               & 26992.13                                             & 2693.673                                           & 24298.46                                          & 90.02\%                                                    \\
                                                                                  & Preset 0                                           & 25789.55                                             & 2756.774                                           & 23032.78                                          & \colorbox{lightgray}{89.31\%}                              \\
                                                                                  & Preset 1                                           & 26236.34                                             & 2676.916                                           & 23559.42                                          & \colorbox{lightgray}{89.80\%}                              \\
                                                                                  & Preset 2                                           & 27330.98                                             & 2737.67                                            & 24593.31                                          & \colorbox{lightgray}{89.98\%}                              \\
                                                                                  & Preset 3                                           & 25465.2                                              & 2735.844                                           & 22729.35                                          & \colorbox{lightgray}{89.26\%}                              \\
                                                                                  & Preset 4                                           & 25977.27                                             & 2678.884                                           & 23298.38                                          & \colorbox{lightgray}{89.69\%}                              \\
                                                                                  & Preset 5                                           & 25361.18                                             & 2702.239                                           & 22658.95                                          & \colorbox{lightgray}{89.34\%}                              \\
                                                                                  & Preset 6                                           & 26466.49                                             & 2695.999                                           & 23770.49                                          & \colorbox{lightgray}{89.81\%}                              \\
                                                                                  & Preset 7                                           & 26699.03                                             & 2767.44                                            & 23931.59                                          & \colorbox{lightgray}{89.63\%}                              \\
                                                                                  & Preset 8                                           & 26699.03                                             & 2767.44                                            & 23931.59                                          & \colorbox{lightgray}{89.63\%}                              \\
                                                                                  & Preset 9                                           & 27365.9                                              & 2695.156                                           & 24670.75                                          & 90.15\%                                                    \\  \hline

        \multirow{11}{*}{C24}
                                                                                  & DPTP                                               & 41321.11                                             & 20639.41                                           & 20681.7                                           & 50.05\%                                                    \\
                                                                                  & Preset 0                                           & 41543.73                                             & 21680.46                                           & 19863.27                                          & \colorbox{lightgray}{47.81\%}                              \\
                                                                                  & Preset 1                                           & 41117.98                                             & 19592.93                                           & 21525.04                                          & 52.35\%                                                    \\
                                                                                  & Preset 2                                           & 41557.57                                             & 21347.44                                           & 20210.13                                          & \colorbox{lightgray}{48.63\%}                              \\
                                                                                  & Preset 3                                           & 41312.02                                             & 20930.71                                           & 20381.31                                          & \colorbox{lightgray}{49.34\%}                              \\
                                                                                  & Preset 4                                           & 40669.04                                             & 19755.83                                           & 20913.2                                           & 51.42\%                                                    \\
                                                                                  & Preset 5                                           & 41373.19                                             & 19990.72                                           & 21382.47                                          & 51.68\%                                                    \\
                                                                                  & Preset 6                                           & 41055.92                                             & 19605.2                                            & 21450.72                                          & 52.25\%                                                    \\
                                                                                  & Preset 7                                           & 41092.94                                             & 21357.93                                           & 19735.02                                          & \colorbox{lightgray}{48.03\%}                              \\
                                                                                  & Preset 8                                           & 40650.55                                             & 20069.71                                           & 20580.84                                          & 50.63\%                                                    \\
                                                                                  & Preset 9                                           & 40413.35                                             & 20360.37                                           & 20052.98                                          & \colorbox{lightgray}{49.62\%}                              \\ \hline

        \multirow{11}{*}{C25}
                                                                                  & DPTP                                               & 7360.97                                              & 3843.79                                            & 3517.18                                           & 47.78\%                                                    \\
                                                                                  & Preset 0                                           & 7349.66                                              & 3765.728                                           & 3583.933                                          & 48.76\%                                                    \\
                                                                                  & Preset 1                                           & 7301.543                                             & 3807.63                                            & 3493.913                                          & 47.85\%                                                    \\
                                                                                  & Preset 2                                           & 7236.729                                             & 3782.24                                            & 3454.489                                          & \colorbox{lightgray}{47.74\%}                              \\
                                                                                  & Preset 3                                           & 7296.342                                             & 3794.346                                           & 3501.996                                          & 48.00\%                                                    \\
                                                                                  & Preset 4                                           & 7384.173                                             & 3817.764                                           & 3566.41                                           & 48.30\%                                                    \\
                                                                                  & Preset 5                                           & 7389.313                                             & 3766.736                                           & 3622.577                                          & 49.02\%                                                    \\
                                                                                  & Preset 6                                           & 7507.889                                             & 3783.694                                           & 3724.196                                          & 49.60\%                                                    \\
                                                                                  & Preset 7                                           & 7239.824                                             & 3967.741                                           & 3272.083                                          & \colorbox{lightgray}{45.20\%}                              \\
                                                                                  & Preset 8                                           & 7078.604                                             & 3789.462                                           & 3289.143                                          & \colorbox{lightgray}{46.47\%}                              \\
                                                                                  & Preset 9                                           & 7255.945                                             & 3799.114                                           & 3456.832                                          & \colorbox{lightgray}{47.64\%}                              \\  \hline

        \multirow{11}{*}{C26}
                                                                                  & DPTP                                               & 24366.9                                              & 13030.68                                           & 11336.22                                          & 46.52\%                                                    \\
                                                                                  & Preset 0                                           & 24233.22                                             & 14123.02                                           & 10110.2                                           & \colorbox{lightgray}{41.72\%}                              \\
                                                                                  & Preset 1                                           & 24091.92                                             & 12402.09                                           & 11689.83                                          & 48.52\%                                                    \\
                                                                                  & Preset 2                                           & 24616.94                                             & 14049.56                                           & 10567.38                                          & \colorbox{lightgray}{42.93\%}                              \\
                                                                                  & Preset 3                                           & 24021.75                                             & 13447.69                                           & 10574.06                                          & \colorbox{lightgray}{44.02\%}                              \\
                                                                                  & Preset 4                                           & 24349.25                                             & 12634.16                                           & 11715.09                                          & 48.11\%                                                    \\
                                                                                  & Preset 5                                           & 24494.97                                             & 12632.68                                           & 11862.29                                          & 48.43\%                                                    \\
                                                                                  & Preset 6                                           & 24542.86                                             & 12623.4                                            & 11919.45                                          & 48.57\%                                                    \\
                                                                                  & Preset 7                                           & 24095.84                                             & 13085.9                                            & 11009.94                                          & \colorbox{lightgray}{45.69\%}                              \\
                                                                                  & Preset 8                                           & 24124.98                                             & 12615.13                                           & 11509.85                                          & 47.71\%                                                    \\
                                                                                  & Preset 9                                           & 24381.49                                             & 13221.99                                           & 11159.5                                           & \colorbox{lightgray}{45.77\%}                              \\  \hline
    \end{longtable}
\end{center}
\end{document}

在此处输入图片描述 在此处输入图片描述

相关内容