长表头文字换行

长表头文字换行

我正在处理一个长表格,我想让表格头部的文本换行。我发现即使我使用\endfirsthead\endhead,头部的文本也无法换行。但是,如果我只写头部而不使用这两个命令,文本可以换行,但它们不会在每页的开头重复。有没有办法让头部的文本换行,同时头部可以在每页上重复?

这是使用命令制作页眉的代码。它可以在每一页上重复,但文本不能换行:

\documentclass{article}
\usepackage{graphicx}  % for management of figures
\usepackage{caption}
\usepackage{subcaption}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathrsfs}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage{apacite}
\usepackage{natbib}  % for "\citet" and "\citep"
\usepackage{mathtools}
\usepackage[a4paper, total={6in, 9in}]{geometry}
\usepackage{longtable}  % for long table
\usepackage{multirow}
\usepackage{wrapfig}  % for wrapped text around tables or figures
\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}{c|}{\textbf{Benchmark \newline function}} & \multicolumn{1}{c|}{\textbf{Experi. setup}} & \multicolumn{1}{c|}{\textbf{Initial fitness}} & \multicolumn{1}{c|}{\textbf{Final fitness}} & \multicolumn{1}{c|}{\textbf{Improvements}} & \multicolumn{1}{c}{\textbf{Percentage}} \\ \hline
        \endfirsthead

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

        \hline \multicolumn{1}{c|}{\textbf{Benchmark \newline function}}   & \multicolumn{1}{c|}{\textbf{Experi. setup}} & \multicolumn{1}{c|}{\textbf{Initial fitness}} & \multicolumn{1}{c|}{\textbf{Final fitness}} & \multicolumn{1}{c|}{\textbf{Improvements}} & \multicolumn{1}{c}{\textbf{Percentage}} \\ \hline
        \endhead

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

        \bottomrule
        \endlastfoot
        
        \multirow{11}{*}{C23}                                              & DPTP                                        & 26992.13                                      & 2693.673                                    & 24298.46                                   & 90.02\%                                 \\
                                                                           & Preset 0                                    & 25789.55                                      & 2756.774                                    & 23032.78                                   & 89.31\%                                 \\
                                                                           & Preset 1                                    & 26236.34                                      & 2676.916                                    & 23559.42                                   & 89.80\%                                 \\
                                                                           & Preset 2                                    & 27330.98                                      & 2737.67                                     & 24593.31                                   & 89.98\%                                 \\
                                                                           & Preset 3                                    & 25465.2                                       & 2735.844                                    & 22729.35                                   & 89.26\%                                 \\
                                                                           & Preset 4                                    & 25977.27                                      & 2678.884                                    & 23298.38                                   & 89.69\%                                 \\
                                                                           & Preset 5                                    & 25361.18                                      & 2702.239                                    & 22658.95                                   & 89.34\%                                 \\
                                                                           & Preset 6                                    & 26466.49                                      & 2695.999                                    & 23770.49                                   & 89.81\%                                 \\
                                                                           & Preset 7                                    & 26699.03                                      & 2767.44                                     & 23931.59                                   & 89.63\%                                 \\
                                                                           & Preset 8                                    & 26699.03                                      & 2767.44                                     & 23931.59                                   & 89.63\%                                 \\
                                                                           & Preset 9                                    & 27365.9                                       & 2695.156                                    & 24670.75                                   & 90.15\%                                 \\ \hline
        \multirow{11}{*}{C23}                                              & DPTP                                        & 26992.13                                      & 2693.673                                    & 24298.46                                   & 90.02\%                                 \\
                                                                           & Preset 0                                    & 25789.55                                      & 2756.774                                    & 23032.78                                   & 89.31\%                                 \\
                                                                           & Preset 1                                    & 26236.34                                      & 2676.916                                    & 23559.42                                   & 89.80\%                                 \\
                                                                           & Preset 2                                    & 27330.98                                      & 2737.67                                     & 24593.31                                   & 89.98\%                                 \\
                                                                           & Preset 3                                    & 25465.2                                       & 2735.844                                    & 22729.35                                   & 89.26\%                                 \\
                                                                           & Preset 4                                    & 25977.27                                      & 2678.884                                    & 23298.38                                   & 89.69\%                                 \\
                                                                           & Preset 5                                    & 25361.18                                      & 2702.239                                    & 22658.95                                   & 89.34\%                                 \\
                                                                           & Preset 6                                    & 26466.49                                      & 2695.999                                    & 23770.49                                   & 89.81\%                                 \\
                                                                           & Preset 7                                    & 26699.03                                      & 2767.44                                     & 23931.59                                   & 89.63\%                                 \\
                                                                           & Preset 8                                    & 26699.03                                      & 2767.44                                     & 23931.59                                   & 89.63\%                                 \\
                                                                           & Preset 9                                    & 27365.9                                       & 2695.156                                    & 24670.75                                   & 90.15\%                                 \\
    \end{longtable}
\end{center}
\end{document}

标题不换行: 在此处输入图片描述

这是不使用命令的代码,文本可以换行但是头部不会重复:

\documentclass{article}
\usepackage{graphicx}  % for management of figures
\usepackage{caption}
\usepackage{subcaption}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathrsfs}
\usepackage{hyperref}
\usepackage{parskip}
\usepackage{apacite}
\usepackage{natbib}  % for "\citet" and "\citep"
\usepackage{mathtools}
\usepackage[a4paper, total={6in, 9in}]{geometry}
\usepackage{longtable}  % for long table
\usepackage{multirow}
\usepackage{wrapfig}  % for wrapped text around tables or figures
\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} \\
        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     & 89.31\%                   \\
                              & Preset 1      & 26236.34        & 2676.916      & 23559.42     & 89.80\%                   \\
                              & Preset 2      & 27330.98        & 2737.67       & 24593.31     & 89.98\%                   \\
                              & Preset 3      & 25465.2         & 2735.844      & 22729.35     & 89.26\%                   \\
                              & Preset 4      & 25977.27        & 2678.884      & 23298.38     & 89.69\%                   \\
                              & Preset 5      & 25361.18        & 2702.239      & 22658.95     & 89.34\%                   \\
                              & Preset 6      & 26466.49        & 2695.999      & 23770.49     & 89.81\%                   \\
                              & Preset 7      & 26699.03        & 2767.44       & 23931.59     & 89.63\%                   \\
                              & Preset 8      & 26699.03        & 2767.44       & 23931.59     & 89.63\%                   \\
                              & Preset 9      & 27365.9         & 2695.156      & 24670.75     & 90.15\%                   \\ \hline
        \multirow{11}{*}{C23} & DPTP          & 26992.13        & 2693.673      & 24298.46     & 90.02\%                   \\
                              & Preset 0      & 25789.55        & 2756.774      & 23032.78     & 89.31\%                   \\
                              & Preset 1      & 26236.34        & 2676.916      & 23559.42     & 89.80\%                   \\
                              & Preset 2      & 27330.98        & 2737.67       & 24593.31     & 89.98\%                   \\
                              & Preset 3      & 25465.2         & 2735.844      & 22729.35     & 89.26\%                   \\
                              & Preset 4      & 25977.27        & 2678.884      & 23298.38     & 89.69\%                   \\
                              & Preset 5      & 25361.18        & 2702.239      & 22658.95     & 89.34\%                   \\
                              & Preset 6      & 26466.49        & 2695.999      & 23770.49     & 89.81\%                   \\
                              & Preset 7      & 26699.03        & 2767.44       & 23931.59     & 89.63\%                   \\
                              & Preset 8      & 26699.03        & 2767.44       & 23931.59     & 89.63\%                   \\
                              & Preset 9      & 27365.9         & 2695.156      & 24670.75     & 90.15\%                   \\
    \end{longtable}
\end{center}
\end{document}

文本可以换行但头部不会重复: 在此处输入图片描述

答案1

如果你想保持居中和换行,我建议

\multicolumn{1}{>{\centering}p{2cm}|}{\textbf{Benchmark \newline function}}

(2cm 似乎比 1.8cm 更合适)

相关内容