Search code examples
latexoverleaf

Longtable with multirows and multicolumns in Latex


I have this table and I want to create it on Latex. I have tried a lot of table packages but with every package I use, I always get problems like the text exceeding the page width or can't seem to create multirows on the same columns.

Any suggestions would be helpful.

enter image description here

\begin{longtabu} to \textwidth {|X|X|X|X|}
    \caption{Data Dictionary}\label{tab:summary}\\ 
    \hline
    \textbf{Tables} &  \textbf{Columns}    &   \textbf{Designation} &   \textbf{Type} \\
    \hline
    \endfirsthead
    \multicolumn{4}{c}%
    {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
    \hline
    \textbf{Tables} &  \textbf{Columns}    &   \textbf{Designation} &   \textbf{Type} \\
    \hline
    \endhead
    \hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
    \endfoot
    \hline
    \endlastfoot

\textbf{Building} \newline &
RI Building Ref
(Primary Key)\newline
Building Name\newline
Development Name\newline
Address\newline
Submarket\newline
Latitude\newline
Longitude &
The building reference in Realinflo database\newline
The building name\newline
The development name to which the building belongs, if any.\newline
The building street address\newline
The submarket the building is located at.\newline
The building latitude\newline
The building longitude & 
text\newline
text\newline
text\newline
text\newline
text\newline
float\newline
float\\hline



\end{longtabu} 

Solution

  • You can try this way, you use the combination of multirow from multirow package and the rotatebox from graphicx package. Here, multirow was used this way: \multirow{<number of rows to span>}{<width>}[<vmove>]{<content>}. As you can see, for the Building subtable, you make the first cell in the first column spans 7 rows, pass * for the natural width of the content, set [<vmove>] = [-5ex] for allowing the text move more vertically down then it would since it spans over rows and not the number of lines in the adjacent cells. The negative value lowers the text, while the positive value raises the text. Finally you pass the content as the last argument. You rotate the content with rotatebox as \rotate[origin=c]{90}{text} where it rotates the text 90 degree anti-clockwise direction with respect to initial axis. You use p{<width>} in longtable environment to allow text wrap where width is the width of the column. You use cline{i-j} where i is the first column and j is the last column over which horizontal rule spans, in this case cline{2-4}. With that, I guess you can try out for the remaining rows. Note that, after the first row, the first elements in the subsequent rows were left empty to allow the text to span over multiple rows alone, hence you will see no text before & in those lines.

    Table

    \documentclass{article}
    \usepackage{longtable}
    \usepackage{multirow, graphicx}
    
    \begin{document}
    \begin{longtable}{|p{0.5in}|p{1.2in}|p{1.7in}|c|}
        \caption{Data Dictionary}\label{tab:summary}\\ 
        \hline
        \centering \textbf{Tables}&
        \centering \textbf{Columns}&
        \centering \textbf{Designation}&
        \textbf{Type}\\
        \hline
        \endfirsthead
        \multicolumn{4}{c}%
        {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
        \hline
        \textbf{Tables} &  \textbf{Columns}    &   \textbf{Designation} &   \textbf{Type} \\
        \hline
        \endhead
        \hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
        \endfoot
        \hline
        \endlastfoot
    
    \multirow{7}{*}[-5ex]{\rotatebox[origin=c]{90}{\textbf{Building}}} &
    \centering RI Building Ref
    (Primary Key)&
    \raggedright  The building reference in database&
    text\\
    
    \cline{2-4}
    
    &\centering Building Name&
    The building name&
    text\\
    
    \cline{2-4}
    
    &\centering Development Name&
    The development name to which the building belongs, if any.&
    text\\
    
    \cline{2-4}
    
    &\centering Address&
    The building street address&
    text\\
    
    \cline{2-4}
    
    &\centering Submarket&
    The submarket the building is located at.&
    text\\
    
    \cline{2-4}
    
    & \centering Latitude&
    The building latitude&
    float\\
    
    \cline{2-4}
    
    &\centering Longitude &
    The building longitude& 
    float\\
    
    \hline
    
    \end{longtable}
    \end{document}