如何在长表的表头中进行不同的对齐

如何在长表的表头中进行不同的对齐

我想让最后一列longtable采用不同的对齐方式。我希望标题(中心频率列)左对齐,而该列中的数据右对齐。我应该从代码中修改什么?

\documentclass[12pt, a4paper, onecolumn, oneside, final]{report}
\makeatother

\usepackage{array}
\setlength\extrarowheight{4pt}

\usepackage{tabto}
\newenvironment{tabs}[1]
{\TabPositions{#1}}

\usepackage{longtable}  
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\usepackage{multicol}
\usepackage{multirow}
\usepackage{musicography}

\begin{document}

\begin{small}
    \renewcommand\thetable{A}
    \begin{longtable}{ |C{3cm}|p{3cm}|r|p{3cm}| }
        
        \caption{Nomor not MIDI, nama not, dan \textit{center frequency}}\\
        \hline
        \textbf{Nomor Not MIDI} & \textbf{Nama Not} & \textbf{\raggedleft\textit{Center frequency}} \\
        \endfirsthead
        
        \multicolumn{3}{c}{\textbf{\tablename~\thetable} Nomor not MIDI, nama not, dan \textit{center frequency} (Lanjutan)}\\
        \hline
        \textbf{Nomor Not MIDI} & \textbf{Nama Not} & \textbf{\raggedleft\textit{Center frequency}}
        \endhead
        
        \hline
        127 & G9                        & 12,543.85 \\
        
        \hline
        126 & F$\sharp$9 / G$\flat$9    & 11,839.82 \\
        
        \hline
        125 & F9                        & 11,175.30 \\
        \label{tbl:MIDINotes}\\
        \hline
    \end{longtable}
\end{small}
\end{document}

这是当前的输出样本。
在此处输入图片描述

我试图修改\begin{longtable}{ |C{3cm}|p{3cm}|r|p{3cm}| }\begin{longtable}{ |C{3cm}|p{3cm}|p{3cm}| }(省略r),但是这会使所有数据都左对齐,包括标题。

答案1

您需要进行两项主要调整:

  • 将最终标题单元格的材料包裹在\multicolumn{1}{..}{...}包装器中以覆盖默认的列类型;并且

  • \caption\endhead材料也使用指令,例如,

    \caption{Nomor not MIDI, nama not, dan \textit{center frequency} (Lanjutan)}
    

此外,我会更改\LTcapwidth变量的值,以避免在标题中出现不必要的换行符。接下来,不要通过运行\renewcommand\thetable{A}; 来硬编码表格“编号”,而是运行\renewcommand\thetable{\Alph{table}}。我还会删除\small指令,因为它似乎不需要;当然,单元格内容无需切换到 就可以很好地适应\small

在此处输入图片描述

\documentclass[12pt, a4paper, onecolumn, oneside, final]{report}

\usepackage{array}
\setlength\extrarowheight{4pt}

\usepackage{longtable}  

\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\usepackage{musicography}

\begin{document}
\renewcommand\thetable{\Alph{table}} % better than "\renewcommand\thetable{A}"
\setlength\LTcapwidth{\textwidth} % default: 10cm

    \begin{longtable}{ | C{3cm} | L{3cm} | R{3cm} | }

    %% headers and footers:

    \caption{Nomor not MIDI, nama not, dan \textit{center frequency}}
    \label{tbl:MIDINotes}\\
    \hline
    \textbf{Nomor Not MIDI} & 
    \textbf{Nama Not} & 
    \multicolumn{1}{C{3cm}|}{\textbf{\textit{Center frequency}}} \\
    \hline
    \endfirsthead
        
    \multicolumn{3}{C{\dimexpr9cm+4\tabcolsep+2\arrayrulewidth\relax}}{%
        \textbf{\tablename~\thetable} Nomor not MIDI, nama not, dan
        \textit{center frequency} (Lanjutan)}\\
    \hline
    \textbf{Nomor Not MIDI} & 
    \textbf{Nama Not} & 
    \multicolumn{1}{C{3cm}|}{\textbf{\textit{Center frequency}}} \\
    \hline
    \endhead

    \hline
    \endlastfoot
        

    % body of table:

        127 & G9                        & 12,543.85 \\ \hline
        126 & F$\sharp$9 / G$\flat$9    & 11,839.82 \\ \hline
        125 & F9                        & 11,175.30 \\

    \end{longtable}

\end{document}

答案2

也许不是一个完美的解决方案,但它似乎有效:使用它,\multicolumn{1}{l|}{\textbf{\textit{Center frequency}}}因为它允许您选择对齐。

\documentclass[12pt, a4paper, onecolumn, oneside, final]{report}
\makeatother

\usepackage{array}
\setlength\extrarowheight{4pt}

\usepackage{tabto}
\newenvironment{tabs}[1]
{\TabPositions{#1}}

\usepackage{longtable}  
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\usepackage{multicol}
\usepackage{multirow}
\usepackage{musicography}

\begin{document}
   
   \begin{small}
      \renewcommand\thetable{A}
      \begin{longtable}{ |C{3cm}|p{3cm}|r|p{3cm}| }
         
         \caption{Nomor not MIDI, nama not, dan \textit{center frequency}}\\
         \hline
         \textbf{Nomor Not MIDI} & \textbf{Nama Not} & \multicolumn{1}{l|}{\textbf{\textit{Center frequency}}} \\
         \endfirsthead
         
         \multicolumn{3}{c}{\textbf{\tablename~\thetable} Nomor not MIDI, nama not, dan \textit{center frequency} (Lanjutan)}\\
         \hline
         \textbf{Nomor Not MIDI} & \textbf{Nama Not} & \textbf{\textit{Center frequency}}
         \endhead
         
         \hline
         127 & G9                        & 12,543.85 \\
         
         \hline
         126 & F$\sharp$9 / G$\flat$9    & 11,839.82 \\
         
         \hline
         125 & F9                        & 11,175.30 \\
         \label{tbl:MIDINotes}\\
         \hline
      \end{longtable}
   \end{small}
\end{document}

PS:您确定需要第四列吗?或者第四列就\begin{longtable}{ |C{3cm}|p{3cm}|r| }足够了吗?

相关内容