1

I have used multicolumn and tabularx for the table I intend to write in LaTex. But the resultant table is going out of the page width. I also added the textwidth in tabularx but it is not working as desired. Surely, I have did some mistake but cant figure out. Would be glad to have the correction. Thank you

\begin{table}
\centering
\caption{my table}
\label{mytable}
\begin{tabularx}{\textwidth}{|c|c|c|c|X|c|X|} 
\hline
\multicolumn{1}{|l|}{Type} & \multicolumn{1}{l|}{Product name} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Diameter\\(mm)\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Tensile strength\\(MPa)\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Young's modulus\\(GPa)\end{tabular}} & \multicolumn{1}{l|}{\begin{tabular}[c]{@{}l@{}}Elongation\\(\%)\end{tabular}} & Application \\ 
\hline
\multirow{5}{*}{\begin{tabular}[c]{@{}c@{}}class A\\~\\~\\~\\ \end{tabular}} & abc123 & 0.014 & 1500 & 36 & 7 & four words text here  \\ 
\cline{2-7} & abc123 & 0.040 & 1400 & 36 & 6.5 & four words text here\\ 
\cline{2-7} & abc123 & 0.20 & 975 & 27 & 9 & four words text here \\ 
\cline{2-7} & abc123 & 0.31 & 975 & 26 & 6 & four words text here \\ 
\cline{2-7} & abc123 & 0.67 & 900 & 23 & 9 & four words text here \\ \hline
\multicolumn{1}{|l|}{\multirow{3}{*}{class B}} & abc123 & 0.027 & 1560 & 39 & 6.5 & four words text here  \\ 
\cline{2-7} \multicolumn{1}{|l|}{} & abc123 & 0.040 & 1600 & 41 & 6.5 & four words text here \\ 
\cline{2-7} \multicolumn{1}{|l|}{} & abc123 & 0.10 & 1200 & 28 & 12.5 & four words text here \\
\hline
\end{tabularx}
\end{table}

Also, the merged column text of the first column, I want it to set the vertical alignment to center instead of top.

2

The reason the tabularx environment's width exceeds the width of the textblock is that you're not allowing line breaks in the header cells. I suggest that you (a) radically simplify the header material, mostly by getting rid of the \multicolumn "wrappers", and (b) use a centered version of the X column type for all six data columns. Optionally, consider using a separate row in the header to place the information about the units of measurement.

enter image description here

\documentclass{article}
\usepackage{tabularx}  % for "tabularx" environment and "X" column type
\usepackage{ragged2e}  % for "\Centering" macro
\newcolumntype{C}{>{\Centering\arraybackslash\hspace{0pt}}X}
%% "\hspace{0pt}" allows for hyphenation of first word in header cells

\begin{document}

\begin{table}
%%\centering % not needed
\caption{My table}
\label{tab:mytable}
\setlength\extrarowheight{2pt} % for a slightly more open "look"
\setlength\tabcolsep{3pt}      % default: 6pt

\begin{tabularx}{\textwidth}{|l|*{6}{C|}} 
\hline
Type & Product name & Diameter & Tensile strength 
     & Young's modulus & Elongation & Application \\ 
& & (mm) & (MPa) & (GPa) & (\%) & \\ % place units of measurement on a separate row
\hline
Class A  & abc123 & 0.014 & 1500 & 36 & 7   & four words text here \\ \cline{2-7} 
         & abc123 & 0.040 & 1400 & 36 & 6.5 & four words text here \\ \cline{2-7} 
         & abc123 & 0.20  &  975 & 27 & 9   & four words text here \\ \cline{2-7} 
         & abc123 & 0.31  &  975 & 26 & 6   & four words text here \\ \cline{2-7} 
         & abc123 & 0.67  &  900 & 23 & 9   & four words text here \\ 
\hline
Class B  & abc123 & 0.027 & 1560 & 39 & 6.5 & four words text here \\ \cline{2-7}  
         & abc123 & 0.040 & 1600 & 41 & 6.5 & four words text here \\ \cline{2-7}  
         & abc123 & 0.10  & 1200 & 28 & 12.5& four words text here \\
\hline
\end{tabularx}
\end{table}

\end{document}

Addendum: If this were my table, I'd strive to give it a far more open and "inviting" look. I'd do so mainly by (a) getting rid of all vertical rules and (b) using fewer, but well-spaced, horizontal rules. The booktabs package, and its macros \toprule, \midrule, and \bottomrule, are made for just this use case.

enter image description here

\documentclass{article}
\usepackage{tabularx,ragged2e,siunitx,booktabs}
\newcolumntype{C}{>{\Centering\arraybackslash\hspace{0pt}}X}

\begin{document}
\begin{table}
\caption{Second try}
\label{tab:secondtry}
\setlength\tabcolsep{4pt}      % default: 6pt

\begin{tabularx}{\textwidth}{@{} l *{6}{C} @{}} 
\toprule
Type & Product name & Diameter & Tensile strength 
     & Young's modulus & Elongation & Application \\ 
& & (\si{\milli\meter}) & (\si{\mega\pascal}) & (\si{\giga\pascal}) & (\%) & \\
\midrule
Class A  & abc123 & 0.014 & 1500 & 36 & 7   & four words text here \\ \addlinespace 
         & abc123 & 0.040 & 1400 & 36 & 6.5 & four words text here \\ \addlinespace 
         & abc123 & 0.20  &  975 & 27 & 9   & four words text here \\ \addlinespace 
         & abc123 & 0.31  &  975 & 26 & 6   & four words text here \\ \addlinespace 
         & abc123 & 0.67  &  900 & 23 & 9   & four words text here \\ 
\midrule
Class B  & abc123 & 0.027 & 1560 & 39 & 6.5 & four words text here \\ \addlinespace  
         & abc123 & 0.040 & 1600 & 41 & 6.5 & four words text here \\ \addlinespace  
         & abc123 & 0.10  & 1200 & 28 & 12.5& four words text here \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

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.