3

I have created the following table.

Image

I need rows "Text", "Same here" and "I want this to align vertically" to be vertically aligned. I have searched similar questions, and used suggestions from Vertical alignment in tabular cells with variable height and How to vertically-center the text of the cells?, but their solutions split my table. Please let me know where I am going wrong. My MWE is shown below:

\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[font=scriptsize,
        labelfont=bf]{caption}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\usepackage{pdflscape}
\usepackage{lipsum}
\UseTblrLibrary{siunitx}

\begin{document}
\begin{table}[]
\begin{tabular}{cccccccccccc}
\hline
\multicolumn{4}{|c|}{I want this to align vertically}                                                                                                                                                                                                                                    
& \multicolumn{4}{c|}{I want this to align vertically}                                                                                                                                                                                                                                    
& \multicolumn{4}{c|}{I want this to align vertically}                                                                                                                                                                                                                                    
\\ [20pt] \hline
\multicolumn{4}{|c|}{Text}                                                                                                                                                                                                                                                               
& \multicolumn{4}{c|}{Text}                                                                                                                                                                                                                                                               
& \multicolumn{4}{c|}{Text}                                                                                                                                                                                                                                                               
\\ [15 pt]\hline
\multicolumn{1}{|c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} & 
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} & 
\multicolumn{2}{c|}{\begin{tabular}[c]{@{}c@{}}Ambient\\ Conditions\end{tabular}} & 
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Vertically\\ align\end{tabular}}} & 
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} & 
\multicolumn{2}{l|}{\begin{tabular}[c]{@{}l@{}}Ambient\\ Conditions\end{tabular}} & 
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} & 
\multicolumn{1}{c|}{\multirow{\begin{tabular}[c]{@{}c@{}}Same\\ here\end{tabular}}} & 
\multicolumn{2}{c|}{\begin{tabular}[c]{@{}c@{}}Ambient\\ Conditions\end{tabular}} \\ \cline{3- 
4} \cline{7-8} \cline{11-12} 
\multicolumn{1}{|c|}{}                                                                            
& \multicolumn{1}{c|}{}                                                                            
& \multicolumn{1}{c|}{RH}                 & \multicolumn{1}{c|}{Temp}               & 
\multicolumn{1}{c|}{}                                                                            
& \multicolumn{1}{c|}{}                                                                            
& \multicolumn{1}{l|}{RH}                 & \multicolumn{1}{l|}{Temp}               & 
\multicolumn{1}{c|}{}                                                                            
& \multicolumn{1}{c|}{}                                                                            
& \multicolumn{1}{c|}{RH}                 & \multicolumn{1}{c|}{Temp}               \\ \hline
\multicolumn{1}{|c|}{A}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{A}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{A}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                \\ \hline
\multicolumn{1}{|c|}{B}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{B}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{B}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                \\ \hline
\multicolumn{1}{|c|}{C}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{C}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{C}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                \\ \hline
\multicolumn{1}{|c|}{D}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{D}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{D}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                \\ \hline
\multicolumn{1}{|c|}{E}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{E}                                                                           
& \multicolumn{1}{c|}{200}                                                                         
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                & 
\multicolumn{1}{c|}{E}                                                                           
& \multicolumn{1}{c|}{200}                                                                          
& \multicolumn{1}{c|}{200}                & \multicolumn{1}{c|}{200}                \\ \hline
\multicolumn{1}{l}{}                                                                              
& \multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                    & \multicolumn{1}{l}{}                    & 
\multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                    & \multicolumn{1}{l}{}                    & 
\multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                    & \multicolumn{1}{l}{}                    \\    
\multicolumn{1}{l}{}                                                                              
& \multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                    & \multicolumn{1}{l}{}                    & 
\multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                    & \multicolumn{1}{l}{}                    & 
\multicolumn{1}{l}{}                                                                             
& \multicolumn{1}{l}{}                                                                                 
& \multicolumn{1}{l}{}                    & \multicolumn{1}{l}{}                   
\end{tabular}
\end{table}
\end{document}      

I appreciate it may be a duplicate question and apologise for the inconvenience, but I am very new to Latex. Many thanks in advance.

2
  • What's with all the useless \multicolumn{1}{c}{} and \multicolumn{1}{l}{} directives? Did you maybe use tablegenerator.com to create this table?
    – Mico
    Aug 30, 2021 at 0:16
  • @Mico Yup I have. I have just started using Latex, and found that using it was convenient until now. Sorry (
    – HK94
    Aug 30, 2021 at 0:18

2 Answers 2

4
  • Your table is quite big: 12 columns! So it spill out of text area.
  • In preamble you load tabularray package, why you not use it in design of this table? It is powerful package! Code with it is very simple (regardless that is somehow different what you learn at table settings at other (classic) tables packages and require some effort to learn it)
  • Since you load tabularray package and already receive two answers using it, which you accepted) I assume, that you at least a little familiar with it, so I strongly encourage to further use it:
\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[font=scriptsize,
        labelfont=bf]{caption}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\usepackage{pdflscape}
\UseTblrLibrary{siunitx}
\usepackage{lipsum}

\begin{document}
    \begin{table}[ht]
\begin{tblr}{width=\linewidth,
             hlines, vlines,
             colspec={*{12}{X[c,m]}},
             colsep=4pt,
             row{1}={ht=7ex},
             row{2}={ht=5ex},
             hspan=minimal
            } 
\SetCell[c=4]{c} I want this to align vertically
    &   &   &   &   \SetCell[c=4]{c} I want this to align vertically
        &   &   &   &   \SetCell[c=4]{c} I want this to align vertically
            &   &   &                                                   \\
\SetCell[c=4]{c} Text
    &   &   &   &   \SetCell[c=4]{c} Text
        &   &   &   &   \SetCell[c=4]{c} Text
            &   &   &                                                   \\
\SetCell[r=2]{c} {Same\\ here}
    &   \SetCell[r=2]{c} {Same\\ here}
        &   \SetCell[c=2]{c} {Ambient\\ Conditions}
            &   &   \SetCell[r=2]{c} {Same\\ here}
                    &   \SetCell[r=2]{c} {Same\\ here}
                        &   \SetCell[c=2]{c} {Ambient\\ Conditions}
                            &   &   \SetCell[r=2]{c} {Same\\ here}
                                    &   \SetCell[r=2]{c} {Same\\ here}
                                        &   \SetCell[c=2]{c} {Ambient\\ Conditions}
                                            &                           \\
    &     & RH  & Temp &   &    & RH  & Temp &  &   & RH  & Temp        \\
A   & 200 & 200 & 200  & A & 200 & 200  & 200 & A   & 200 & 200 & 200   \\ 
B   & 200 & 200 & 200  & B & 200 & 200  & 200 & B   & 200 & 200 & 200   \\
C   & 200 & 200 & 200  & C & 200 & 200  & 200 & C   & 200 & 200 & 200   \\
D   & 200 & 200 & 200  & D & 200 & 200  & 200 & D   & 200 & 200 & 200   \\
E   & 200 & 200 & 200  & E & 200 & 200  & 200 & E   & 200 & 200 & 200   \\
\end{tblr}
    \end{table}
\end{document}

enter image description here

Note, I would consider to use in table \small font size. It can be determined by adding \small before \begin{tblr}.

Edit: improved is table specification and added more explanation.

2
  • You are amazing. Thank you so much! I tried your recommendation of reducing the text size, and the table now sits in the centre. Again, thank you!
    – HK94
    Aug 30, 2021 at 1:11
  • @HK94, I edit answer and a wee bit improve table specification and add more explanation. I wonder, why you after starting with use tabularray package now return to classic table packages ...
    – Zarko
    Aug 30, 2021 at 1:36
3

Something like this? Observe that I've used two separate strategies to enforce vertical centering across two or three lines: (a) insertion of typographic struts and (b) use of \multirow (with a non-integer argument).

I've also tried to clean up the worst coding excesses of tablegenerator.com. The LaTeX code of the table still isn't good. In particular, the code continues to make no attempt to assure that the table will actually fit inside the width of the textblock. (For the table at hand, executing both \setlength\tabcolsep{3pt} -- the default is 6pt -- and \small after \begin{table} and before \begin{tabular} should do the trick of making the table fit inside the textblock.)

enter image description here

\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[font=scriptsize,labelfont=bf]{caption}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\usepackage{pdflscape}
\usepackage{multirow}

% define a typographic strut:
\newcommand\mystrut{\rule[-15pt]{0pt}{38pt}} % choose depth and height suitably
% handy shortcut macro:
\newcommand\mytab[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}

\begin{document}
\begin{table}
\begin{tabular}{ | *{12}{c|} }
\hline
 \multicolumn{4}{|c|}{I want this to align vertically \mystrut }      
& \multicolumn{4}{c|}{I want this to align vertically}      
& \multicolumn{4}{c|}{I want this to align vertically}      
\\ \hline
 \multicolumn{4}{|c|}{Text \mystrut }     
& \multicolumn{4}{c|}{Text}     
& \multicolumn{4}{c|}{Text}     
\\ \hline
\multirow{2.2}{*}{\mytab{Same\\ here}} & 
\multirow{2.2}{*}{\mytab{Same\\ here}} & 
\multicolumn{2}{c|}{\mytab{Ambient\\ Conditions}} & 
\multirow{2.2}{*}{\mytab{Vertically\\ align}} & 
\multirow{2.2}{*}{\mytab{Same\\ here}} & 
\multicolumn{2}{l|}{\mytab{Ambient\\ Conditions}} & 
\multirow{2.2}{*}{\mytab{Same\\ here}} & 
\multirow{2.2}{*}{\mytab{Same\\ here}} & 
\multicolumn{2}{c|}{\mytab{Ambient\\ Conditions}} \\ 
\cline{3-4} \cline{7-8} \cline{11-12} 
  & & RH & Temp & & & RH & Temp & & & RH & Temp \\ \hline
A & 200 & 200 & 200 & A & 200 & 200 & 200 & A & 200 & 200 & 200 \\ \hline
B & 200 & 200 & 200 & B & 200 & 200 & 200 & B & 200 & 200 & 200 \\ \hline
C & 200 & 200 & 200 & C & 200 & 200 & 200 & C & 200 & 200 & 200 \\ \hline
D & 200 & 200 & 200 & D & 200 & 200 & 200 & D & 200 & 200 & 200 \\ \hline
E & 200 & 200 & 200 & E & 200 & 200 & 200 & E & 200 & 200 & 200 \\ \hline    
\end{tabular}
\end{table}
\end{document} 

 
2
  • 1
    Almost at the same time :-) . Use of standard tabular also works fine (+1)!
    – Zarko
    Aug 30, 2021 at 1:06
  • 1
    @Mico. Thank you so much. I have tried yours as well, and it looks beautiful. Again, your prompt response is highly appreciated.
    – HK94
    Aug 30, 2021 at 1:14

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.