1

I have been trying for a while now to fit my longtable within the page margins. Unfortunately no success. Any help would be highly appreciated. Thanks!

\begin{longtable}[c]{|l|llllll|}
\caption{ABC}
\label{tab:my-table}\\
\hline
\multicolumn{1}{|c|}{\multirow{2}{*}{Title and Reference}} &
  \multicolumn{6}{c|}{Match for "Operation Management of Urban Service Robots" with Sub-Topic} \\ \cline{2-7} 
\multicolumn{1}{|c|}{} &
  \multicolumn{1}{l|}{Smart City and Robotics} &
  \multicolumn{1}{l|}{Singular Robots} &
  \multicolumn{1}{l|}{Multiple Robots} &
  \multicolumn{1}{l|}{Assistance Infrastructure} &
  \multicolumn{1}{l|}{Responsible Energy Consumption} &
  Time Management \\ \hline
\endhead
%
ABC &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
   \\ \hline
 &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
   \\ \hline
\end{longtable}
1
  • 1
    Welcome to TeX.SE. What's the purpose of all the ` \multicolumn{1}` wrappers?
    – Mico
    Commented Apr 27, 2023 at 13:34

3 Answers 3

0
\documentclass[12pt,a4,twoside]{article}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{makecell}
\usepackage{float}
\usepackage{blindtext, pdflscape}
\usepackage{tabularx}
\usepackage{geometry}
\usepackage{ltablex}
\usepackage{caption}
\begin{document}
\setlength{\arrayrulewidth}{0.2mm} % To change table line thickness
\setlength{\tabcolsep}{1pt}
\renewcommand{\arraystretch}{1}
\begin{longtable}[c]{|l|llllll|}
\caption{ABC}
\label{tab:my-table}\\
\hline
\multicolumn{1}{|c|}{\multirow{2}{*}{\makecell{Title \\ and \\ Reference}}} &
  \multicolumn{6}{c|}{\makecell{Match for "Operation Management of Urban Service Robots" \\ with Sub-Topic}} \\ \cline{2-7} 
\multicolumn{1}{|c|}{} &
  \multicolumn{1}{l|}{\makecell{Smart City \\ and \\ Robotics}} &
  \multicolumn{1}{l|}{\makecell{Singular \\ Robots}} &
  \multicolumn{1}{l|}{\makecell{Multiple \\ Robots}} &
  \multicolumn{1}{l|}{\makecell{Assistance \\ Infrastructure}} &
  \multicolumn{1}{l|}{\makecell{Responsible \\ Energy \\ Consumption}} &
  \multicolumn{1}{l|}{\makecell{Time\\ Management}} \\ \hline

\endhead
%
ABC &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
   \\ \hline
 &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
  \multicolumn{1}{l|}{} &
   \\ \hline
\end{longtable}
\end{document}

enter image description here

2
  • Is there a way to modify your approach so that one could set an overall target width of the table directly?
    – Mico
    Commented Apr 27, 2023 at 15:21
  • '\setlength{\arrayrulewidth}{0.2mm}' To change table line thickness. '\renewcommand{\arraystretch}{1}' Changes overall table width and stretches horizontally. Commented Apr 27, 2023 at 17:29
0

I suggest you (a) get rid of all \multicolumn{1} "wrappers", (b) load the xltabular package and switch from a longtable to an xltabular environment -- the main advantage being that you can set a target width of an xltabular environment, and (c) employ the X column type in all 7 columns in order to allow automatic line breaking.

I would also give the table a more open and inviting look by (i) getting rid of all vertical rules and (ii) using fewer, but well-spaced, horizontal rules.

enter image description here

\documentclass{article}
\usepackage[english]{babel}
\usepackage{xltabular,ragged2e,amsmath,booktabs,makecell}
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X}
\newcolumntype{C}{>{\Centering\hspace{0pt}}X}
\hyphenation{infra-struc-ture con-sump-tion}

\begin{document}

\setlength\tabcolsep{2pt} % default: 6pt
\begin{xltabular}{\textwidth}{@{} L CCCCCC @{}}
\caption{ABC} \label{tab:my-table}\\
\toprule
Title and Reference &
\multicolumn{6}{c}{\makecell[t]{Match for ``Operation 
  Management of \\Urban Service Robots'' with Sub-Topic}} \\ 
\cmidrule(l){2-7} 
 &
  Smart City and Robotics &
  Singular Robots &
  Multiple Robots &
  Assistance Infrastructure &
  Responsible Energy Consumption &
  Time Management \\ 
\midrule
\endhead

ABC \\ 
\addlinespace
DEF \\ 
\bottomrule
\end{xltabular}
\end{document}
0
  • You not provide any information about your document set up. Consequently are unknown page layout, used font and it size. Both are have strong influence on page looks.
  • For your long table a I would use longtblr of tabularray package.
  • MWE (Minimal Working Example) below provide two cases of table design:
\documentclass{article}
\usepackage{geometry}   
\usepackage[english]{babel}
\hyphenation{infra-struc-ture con-sump-tion}
\usepackage{newtxtext, newtxmath}

\usepackage{ragged2e}
\usepackage{tabularray}
\UseTblrLibrary{amsmath,booktabs}

\begin{document}
With  cells in "jail" of horizontal and "vertical" rules:
    \begin{longtblr}[
caption = {ABC},
  label = {tab:my-table}
                    ]{hlines, vlines,
                      colsep  = 3pt,
                      colspec = { X[l] *{6}{X[cmd=\Centering\hspace{0pt}]} },
                        hspan = minimal,
                      rowhead = 2
                      }
\SetCell[r=2]{c}   Title and Reference
    &   \SetCell[c=6]{c}    Match for ``Operation Management of Urban Service Robots'' with Sub-Topic
        &   &   &   &   &                       \\
    &   Smart City and Robotics
        &   Singular Robots
            &   Multiple Robots
                &   Assistance Infrastructure
                    &   Responsible Energy Consumption
                        &   Time Management     \\
ABC & 1 & 2 & 3 & 4 & 5 & 6                     \\
DEF &   &   &   &   &   &                       \\
    \end{longtblr}

or with use \verb+booktabs+ rules:

    \begin{longtblr}[
caption = {ABC},
  label = {tab:my-table}
                    ]{colsep  = 3pt,
                      colspec = {@{} X[l] *{6}{X[cmd=\Centering\hspace{0pt}]} @{}},
                        hspan = minimal,
                      rowhead = 2
                      }
    \toprule
\SetCell[r=2]{c}   Title and Reference 
    &   \SetCell[c=6]{c}    Match for ``Operation Management of Urban Service Robots'' with Sub-Topic
        &   &   &   &   &                       \\ 
\midrule
    &   Smart City and Robotics 
        &   Singular Robots 
            &   Multiple Robots 
                &   Assistance Infrastructure 
                    &   Responsible Energy Consumption 
                        &   Time Management     \\
    \midrule
ABC & 1 & 2 & 3 & 4 & 5 & 6                     \\
DEF &   &   &   &   &   &                       \\
    \bottomrule
    \end{longtblr}
\end{document}

enter image description here

You must log in to answer this question.

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