1

I want to create my table using package xltabular, since it has a nice compatibility to be able to autofit into page margin. My table has many columns, and by the rule from my institution, I may not make this table in a landscape format, so I have to stick with potrait orientation.

This is the table screenshot, generated from Microsoft Excel. table screenshot

For now, the MWE is using longtable package. Altough this is quite nice to look, it still goes off the margin. This is the code and I will attach the screenshot below.

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

\usepackage{geometry}
\geometry{
  left=4cm,
  top=3cm,
  right=3cm,
  bottom=3cm,
}

\begin{document}
\begingroup
\setlength{\LTleft}{-20cm plus -1fill} % set margin
\setlength{\LTright}{\LTleft} % set margin
\begin{small}
    \begin{longtable}{|c|m{0.15\textwidth}|r|m{0.15\textwidth}|m{0.1\textwidth}|m{0.1\textwidth}|r|r|r|r|}
        \caption{Hasil Perbandingan \textit{Pitch} pada lagu Twinkle-Twinkle Little Star di Model Pertama}
        \label{tab:diff-twinkle-model1}\\
        
        \hline
        
        \multicolumn{1}{|c|}{\textbf{No}} & 
        \multicolumn{1}{c|}{\textbf{Pitch Result}} & \multicolumn{1}{c|}{\textbf{Dist}} & \multicolumn{1}{c|}{\textbf{Match}} & \multicolumn{1}{c|}{\textbf{Diff1}} & \multicolumn{1}{c|}{\textbf{Diff2}} & \multicolumn{1}{c|}{\textbf{Len\_match}} & \multicolumn{1}{c|}{\textbf{len\_diff1}} & \multicolumn{1}{c|}{\textbf{len\_diff2}} & \multicolumn{1}{c|}{\textbf{percentage}} \bigstrut\\

        \textbf{1} & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58] & 2     & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58] & []    & []    & 14    & 0     & 0     & 100.00\% \bigstrut\\
        \hline
        \textbf{2} & [0, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57] & 521   & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57] & [58]  & [44, 45] & 13    & 1     & 2     & 71.43\% \bigstrut\\
        \hline
\textbf{3} & [0, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59] & 550   & [0, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58] & [46, 47] & [59]  & 12    & 2     & 1     & 64.29\% \bigstrut\\
        \hline
        \textbf{4} & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55] & 637   & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55] & [56, 57, 58] & []    & 11    & 3     & 0     & 57.14\% \bigstrut\\
        \hline
    \end{longtable}
\end{small}
\endgroup
\end{document}

This is the longtable output. Notice the red lines. It indicates the page margin. The longtable goes beyond the margin.
longtable output

When I try to implement xltabular, I add this to my preamble:

\usepackage{xltabular}
\newcolumntype{Z}{>{\RaggedRight}X}
\setlength\LTcapwidth{\textwidth}

However, the output does not look so nice. This is the sample current output, with overlapping column text. xltabular column output

This is the code I'm trying to use.

\begin{xltabular}{\textwidth}{@{} >{\bfseries}l | Z | r | Z | Z | Z | r | r | r | r @{}}
    
    \caption{Hasil Perbandingan \textit{Pitch} pada lagu Twinkle-Twinkle Little Star di Model Pertama}
    \label{tab:diff-twinkle-model1} \\
    \toprule
    {\textbf{No}} & {\textbf{unique-pitch}} & {\textbf{distance}} & {\textbf{Match}} & {\textbf{diff\_kurang}} & {\textbf{diff\_lebih}} & {\textbf{len\_match}} & {\textbf{len\_kurang}} & {\textbf{len\_lebih}} & {\textbf{percentage}} \bigstrut\\
    
    \midrule    
    \endhead
    
    \midrule
    \endfoot
    
    \bottomrule
    \endlastfoot
\end{xltabular}

What can I do to fix my table? Should I fix my longtable or create a new one using xltabular?

1
  • You could rotate the column headers in order to save some space and avoid the overlapping text you currently get with xltabular.
    – leandriis
    Apr 21, 2021 at 9:13

1 Answer 1

2

You may liked the following result:

enter image description here

(red lines indicate page layout)

It is produced by:

\documentclass[12pt, a4paper, onecolumn, oneside, final]{report}
\usepackage[hmargin={4cm, 3cm},
            vmargin={3cm, 3cm}
              ]{geometry}
\usepackage{array, makecell, multirow, xltabular}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}

%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}% For dummy text. Don't use in a real document

\begin{document}
\lipsum[1]

\begingroup
\footnotesize  % <--- size of fonts in the table
\setlength\tabcolsep{3pt}
\setcellgapes{2pt}
\makegapedcells
\setlength\LTcapwidth{\dimexpr\linewidth-3pt}
    \begin{xltabular}{\linewidth}{|>{\bfseries}c|L|r|L|L|L|r|r|r|r|}
\caption{Hasil Perbandingan \textit{Pitch} pada lagu Twinkle-Twinkle Little Star di Model Pertama}
\label{tab:diff-twinkle-model1} \\
    \Xhline{1.2pt}
\thead{No}  & \thead{Pitch\\ Result}& \thead{Dist}  & \thead{Match}
            & \thead{Diff1}         & \thead{Diff2} & \thead{Len\_\\ match}
            & \thead{len\_\\ diff1} & \thead{len\_\\ diff2} & \thead{\%}\\
    \Xhline{0.8pt}
1   & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58] 
    & 2     & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58] 
    & [ ]   & [ ]       & 14    & 0 & 0 & 100.00    \\
    \hline
2   & [0, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57] 
    & 521   & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57] 
    & [58]  & [44, 45]  & 13    & 1     & 2     & 71.43             \\
        \hline
3   & [0, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59] 
    & 550   & [0, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58] 
    & [46, 47] & [59]   & 12    & 2     & 1     & 64.29              \\
        \hline
4   & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55] 
    & 637   & [0, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55] 
    & [56, 57, 58] 
    & [ ]   & 11        & 3     & 0     & 57.14                     \\
    \Xhline{1.2pt}
    \end{xltabular}
\endgroup

\lipsum[2]
\end{document}

Note: size of fonts can be reduced to predefined sizes by commands \small or \footnotesize and \tiny, or increased to predefined sizes by commands \large, \Large or \huge and \Huge. Beside these sizes you can define (own) font size by command \fontsize{<font size>}{<baseline size>}\selectfont.

Mentioned commands are switches (not environments as you wrongly used in your document example), so after it used font are changed as long until you not return to normal font size by command \normalsize or to the end of group, in which you change font size, as is done in above MWE (Minimal Worlking Example).

4
  • out of topic question, can I make all the font size smaller in that table? If it is possible, how to do so? Apr 21, 2021 at 9:37
  • 1
    @DionisiusPratama, I already reduce font size from your \small to ˙\footnotesize. Even smaller font are \scriptsize` and \tiny. For use one of them you need to replace \footnotesize with selected one. However, bear in mind, that smaller font size can become hard to read.
    – Zarko
    Apr 21, 2021 at 10:34
  • 1
    @DionisiusPratama, see edited answer.
    – Zarko
    Apr 21, 2021 at 10:53
  • Thanks. I got the idea now. Apr 21, 2021 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .