2

I'm trying to include a longtable in my work.

I'm getting all the borders I want except on the "main body" of the table the right border is missing. (Header is appearing on every page, so that is working well)

\documentclass[a4paper,12pt]{book}

\usepackage{ltablex}
\setlength{\parindent}{0pt} % never indent first line

\begin{document}

{\renewcommand{\arraystretch}{1.2}
\begin{longtable}{| c | m{5cm} | m{9.5cm} | }
\hline
&  \textbf{\scshape{What you observed}} &   \textbf{\scshape{Suggestions/reasons/examples}} \\
\hline
\hline
\endhead

\hline
A & \multicolumn{2}{l | }{\textbf{SC} }\\
\hline
& \textbf{1.    P} \newline
\null \hspace{3mm} a\newline
\null \hspace{3mm} b\newline
\null \hspace{3mm}c\newline
\null \hspace{3mm}d\\
\hline
&\textbf{2. -}\newline
\null \hspace{3mm}a\newline
\null \hspace{3mm}b\newline
\null \hspace{3mm}c\newline
\null \hspace{3mm}d\newline
\null \hspace{3mm}e\\
\end{longtable}
\end{document}

enter image description here

How do I need to change my code?

1

2 Answers 2

3

I would like to suggest a couple of things.

If you want to number any element, in your case rows, use counters so you don't have to remember the next value.

LaTeX already have lists, which you can use and format via standard methods or the extra package enumitem, which I did in the code below.

One disadvantage of using lists in tables is they leave extra empty line in cells. The remedy is to wrap the code for a list within \parbox or minipage. Then, whatever spacing is required has to be added manually. This is why there are two \vsppace{...} before and after the list.

\documentclass[a4paper,12pt]{book}
\usepackage{ltablex}
\usepackage{array}       % Adds m{...} type column
\usepackage{longtable}
\usepackage{enumitem}    % More options for lists
\usepackage{newtx}       % Change font for the example to have combine \scshape and \bfseries

\setlength{\parindent}{0pt} % never indent first line

\newcounter{mainlist}\renewcommand\themainlist{\arabic{mainlist}.}
\newlist{innerenum}{enumerate}{1}
\setlist[innerenum]{
  nosep,
  label=\alph*,
}


\begin{document}
{%
  \renewcommand{\arraystretch}{1.2}
  \begin{longtable}{| c| m{5cm}| m{8.5cm}|}
    \hline
    & \textbf{\textsc{What you observed}} & \textsc{\textbf{Suggestions/reasons/examples}} \\
    \hline\hline\endhead
    A & \multicolumn{2}{l|}{\textbf{SC}} \\
    \hline
    & \textbf{\stepcounter{mainlist}\themainlist{} P}\par
      \parbox{\linewidth}{%
      \vspace{0.5\baselineskip}%
      \begin{innerenum}
      \item
      \item
      \item
      \item
      \end{innerenum}\vspace{0.5\baselineskip}}
    & \\
    \hline
    & \textbf{\stepcounter{mainlist}\themainlist{} -}\par
      \parbox{\linewidth}{%
      \vspace{0.5\baselineskip}%
      \begin{innerenum}
      \item
      \item
      \item
      \item
      \end{innerenum}\vspace{0.5\baselineskip}}
    & \\
    \hline
  \end{longtable}}
\end{document}

enter image description here

3
  • How do I reset the arabic counter once done with Part A and enumeration and want to start part B with 1. again?
    – Chris Peh
    Jan 2, 2023 at 20:45
  • For resetting a counter, use \setcounter, e.g. \setcounter{mainlist}{1} instead of \stepcounter. Here's more info.
    – Celdor
    Jan 3, 2023 at 0:58
  • I didn't find the link helpful, but this tex.stackexchange.com/questions/66604/resetting-counter instead. This does the trick: \newcounter{sectionR} \setcounter{sectionR}{1} \renewcommand\thesectionR{\Alph{sectionR}} \newcounter{mainlist}[sectionR] \renewcommand\themainlist{\arabic{mainlist}.}
    – Chris Peh
    Jan 3, 2023 at 3:45
3
  • Instead of longtable I would rather use longtblr of tabularray package.
  • For lists in the second column seems to be better to use enumerate list with resume*=<list name> option.
  • In MWE below I anticipate, that table has table caption
\documentclass[a4paper,12pt]{book}
\usepackage{newtxtext, newtxmath}   % for scshape bfseries font
\usepackage{tabularray}
\UseTblrLibrary{varwidth}                       

\usepackage{ragged2e}
\usepackage{enumitem}
\usepackage{etoolbox}
\AtBeginEnvironment{longtblr}{%
    \setlist[enumerate,1]{nosep, 
                          leftmargin = *,
                          label      = \textbf{\arabic*.},
                          after =\end{minipage},                  
                          before=\begin{minipage}[t]{\linewidth}%\RagggedRight 
                         }
    \setlist[enumerate,2]{nosep,
                          leftmargin=*,
                          label = \alph*
                         }
                           }% end of AtBeginEnvironment
\setlength{\parindent}{0pt} % never indent first line

\begin{document}
    \begin{longtblr}[
caption = {Long table},
  label = {tab:longtblr}
                    ]{hlines, vlines,
                     colspec = {c X[j] X[2, j]},
                     row{1}  = {font=\bfseries\scshape},
                     measure = vbox,
                     rowhead = 1
                     }
    &  What you observed
            &   Suggestions/reasons/examples
                                        \\
\hline
A   & \SetCell[c=2]{l}  \textbf{SC}     \\
    &   \begin{enumerate}[series=tab]
    \item   \textbf{P}
            \begin{enumerate} 
        \item
        \item
        \item
        \item
            \end{enumerate}
        \end{enumerate}
        &                           \\
    &   \begin{enumerate}[resume*=tab]
    \item   \textbf{--}
            \begin{enumerate}
        \item
        \item
        \item
        \item
            \end{enumerate}
        \end{enumerate}
        &                           \\
\end{longtblr}
\end{document}

enter image description here

  • In the case, if you not like to have a captions in longtblr, than MWE had to be:
\documentclass[a4paper,12pt]{book}
\usepackage{lipsum}
% rest of preamble

\begin{document}
\begingroup
\DefTblrTemplate{firsthead, middlehead,lasthead}{default}{} % <---
\DefTblrTemplate{contfoot-text}{normal}{\scriptsize\textit{Continued on the next page}}
\SetTblrTemplate{contfoot-text}{normal}
    \begin{longtblr}[
                    ]{hlines, vlines,
                     colspec = {c X[j] X[2, j]},
                     row{1}  = {font=\bfseries\scshape},
                     measure = vbox,
                     rowhead = 1
                     }
% rest of table
\end{longtblr}
\endgroup
\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 .