4

I want to make the last column of my longtable with different alignment. I want the header (center frequency column) to be aligned left, and the data in that column is aligned right. What should I modify from my code?

\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}

This is the current output sample.
enter image description here

I've tried to modify \begin{longtable}{ |C{3cm}|p{3cm}|r|p{3cm}| } into \begin{longtable}{ |C{3cm}|p{3cm}|p{3cm}| } (omitting r), but it makes all the data is aligned left, including the header.

1
  • Why use fixed width columns? Wouldn't something like \begin{longtable}{ |c|l|r| } be sufficient? – leandriis Apr 10 at 10:52
4

You need to make two main adjustments:

  • encase the material of the final header cell in a \multicolumn{1}{..}{...} wrapper to override the default column type; and

  • use a \caption directive for the \endhead material as well, e.g.,

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

In addition, I'd change the value of the \LTcapwidth variable in order to avoid getting an unnecessary-looking line break in the caption. Next, don't hard-code the table "number" by running \renewcommand\thetable{A}; instead, please run \renewcommand\thetable{\Alph{table}}. I'd also get rid of the \small directive as it doesn't seem to be needed; for sure, the cell contents fit just fine without switching to \small.

enter image description here

\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}
3

Maybe not a beautiful solution, but it seems to work: Use \multicolumn{1}{l|}{\textbf{\textit{Center frequency}}} as it allows you to choose alignment.

\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: Are you sure that you need a fourth column or would \begin{longtable}{ |C{3cm}|p{3cm}|r| } be sufficient?

1
  • No, I don't need a fourth column for this table. Thank you for the insight! – Dionisius Pratama Apr 11 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.