1

I have this code:

\documentclass[article]{elsarticle}
\begin{document}
\begin{table}[H]
     \centering
    \caption{Material properties of the PETG and PETG+CF for different designs}
    \resizebox{\columnwidth}{!}{%
    \begin{tabular}{c|ccccc|cc|}
        \cline{2-8}
        &                       &                      & \bf{PETG+CF}                       &                       &  &                    \bf{PETG}     &  \\ \cline{2-8} 
        & \multicolumn{1}{c|}{Un-Notched} &  \multicolumn{1}{c|}{Notched ($90^\circ$)} & \multicolumn{1}{c|}{Notched ($0^\circ$)} & \multicolumn{1}{c|}{Notched (Fluid)} & Notched (LSC)  & \multicolumn{1}{c|}{Un-Notched} & Notched ($0^\circ$)  \\ \hline
        \multicolumn{1}{|c|}{Modulus (GPa)} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} &  & \multicolumn{1}{c|}{} &  \\ \hline
        \multicolumn{1}{|c|}{Strength (MPa)} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} &  & \multicolumn{1}{c|}{} &  \\ \hline
        \multicolumn{1}{|c|}{Toughness (j.m$^{-3}$)} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} &  & \multicolumn{1}{c|}{} &  \\ \hline
        \multicolumn{1}{|c|}{Failure strain (mm/mm)} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} &  & \multicolumn{1}{c|}{} &  \\ \hline
    \end{tabular}
}
\label{table:3}
\end{table}
\end{document}

enter image description here

I want the text in the two columns of first row to be at the center. What should I do?

leandriis
  • 62,593
Turkan
  • 159
  • 2
    Unrelated t the alignment problem, but please don't use resizebox to make a table fit into the textwidth. This will lead to illegibly small and inconsistent font sizes. Instead you can have a look at the tabularx package. Also, replace \bf with \textbfsince the former is deprecated. (See also: “Correct” way to bold/italicize text?) – leandriis Feb 22 '20 at 17:58
  • 1
    Could you please edit your question and include the documentclass you use? Please also add the settings of the geometry package is you use it. – leandriis Feb 22 '20 at 17:59
  • 2
    To horizontally center the shared column headers in the first row, you can use \multicolumn as follows: & \multicolumn{5}{c|}{\textbf{PETG+CF}} & \multicolumn{2}{c|}{\textbf{PETG}}\\ \cline{2-8}. – leandriis Feb 22 '20 at 18:02
  • Thank you so much – Turkan Feb 22 '20 at 18:08
  • Thanks for the update. Could you please also add some example entries of the currently empty cells? Will there be numbers, text,...? – leandriis Feb 22 '20 at 18:08
  • Thank you. They would be numbers? Why they are important? are they automatically in the center? – Turkan Feb 22 '20 at 18:09
  • As I already mentioned, using resizebox to keep the table inside the margins definitely not the best method. In order to suggest a better approach I asked about the actual contents of the cells. – leandriis Feb 22 '20 at 18:11
  • Also unrelated, but please double check the class option that you used. I could not find any hint on an option called article in the elsarticle class documentation. The available class options of elsarticle heavily influence the width of the margins and the textblock. – leandriis Feb 22 '20 at 18:13

2 Answers2

3
  • Use of \resizebox is very bad idea. With it you lost control on font size (in you case it is not readable. Beter is use smaller font, reduce width of \tabcolsep and write column header in two (or more) lines.
  • Unfortunately it is unknown, what is contents of empty cells. I they are numbers you may consider to use S column type defined in the siunitx package. If not, than can loger text expand table width. in this cas, is beer to use columns with prescribed width, for example p{<width>}.
  • More professional look of table you will get if you will remove vertical lines and use rules defined in the booktabs package (see second example).
  • For writing of units is sensible to use units as they are defined in the siunitx package`.

First example:

\documentclass{elsarticle}
\usepackage{geometry}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\usepackage{siunitx}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c|}{#2}}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[ht]
     \centering
\caption{Material properties of the PETG and PETG+CF for different designs}
\label{table:3}
    \small
    \setlength\tabcolsep{5pt}
    \setcellgapes{3pt}
    \makegapedcells
    \begin{tabular}{| *{8}{c|} }
    \cline{2-8}
\mcc{}
    & \mcc[5]{\thead{PETG+CF}}  
                        &  \mcc[2]{\thead{PETG}}             \\
    \cline{2-8}
\mcc{}
    & \makecell{Un-\\ Notched} 
        &   \makecell{Notched\\ (\SI{90}{\degree})} 
            &   \makecell{Notched\\ (\SI{0}{\degree})} 
                &   \makecell{Notched\\ (Fluid)}
                    &   \makecell{Notched\\ (LSC)}  
                        &   \makecell{Un-\\ Notched}
                            &   \makecell{Notched\\ (\SI{0}{\degree})}  \\
    \hline
Modulus (GPa)           &   &   &   &   &  &    &   \\ \hline
Strength (MPa)          &   &   &   &   &  &    &   \\ \hline
Toughness (j.m$^{-3}$)  &   &   &   &   &  &    &   \\ \hline
Failure strain (mm/mm)  &   &   &   &   &  &    &   \\ \hline
\end{tabular}
    \end{table}
\end{document}

enter image description here

Edit 1: Second example, where is considered, that document has two columns. Since table is about text width wide, it cannot be fir into column wide, so only reasonable solution is to write it over two columns. In this case the package stfloats can be handy, if you like to have table on the same page where is inserted in the text (if there is enough space for it):

\documentclass[twocolumn]{elsarticle}
\usepackage{stfloats}
\usepackage{booktabs, makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\usepackage{siunitx}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c}{#2}}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}

\begin{document}
    \begin{table}[ht]
     \centering
\caption{Material properties of the PETG and PETG+CF for different designs}
\label{table:3}
    \small
    \setlength\tabcolsep{5pt}
    \setcellgapes{3pt}
    \makegapedcells
    \begin{tabular}{ *{8}{c} }
    \cmidrule[\heavyrulewidth]{2-8}
    & \mcc[5]{\thead{PETG+CF}}  
                        &  \mcc[2]{\thead{PETG}}             \\
    \cmidrule{2-6}\cmidrule(l){7-8}
    & \makecell{Un-\\ Notched} 
        &   \makecell{Notched\\ (\SI{90}{\degree})} 
            &   \makecell{Notched\\ (\SI{0}{\degree})} 
                &   \makecell{Notched\\ (Fluid)}
                    &   \makecell{Notched\\ (LSC)}  
                        &   \makecell{Un-\\ Notched}
                            &   \makecell{Notched\\ (\SI{0}{\degree})}  \\
    \hline
Modulus (GPa)           &   &   &   &   &  &    &   \\ 
Strength (MPa)          &   &   &   &   &  &    &   \\ 
Toughness (j.m$^{-3}$)  &   &   &   &   &  &    &   \\ 
Failure strain (mm/mm)  &   &   &   &   &  &    &   \\ 
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

enter image description here

(red lines indicate page layout)

Zarko
  • 296,517
  • Thank you so much really. They are numbers. I used two column format like articles by the way – Turkan Feb 23 '20 at 05:52
  • @Tukan, please write one rows with numbers with maximal number of integer and decimal digits. – Zarko Feb 23 '20 at 06:00
  • Two column format is why I used resizebox also – Turkan Feb 23 '20 at 06:02
  • Zarko, thanks I will do that – Turkan Feb 23 '20 at 06:02
  • @Turkan, sorry, your MWE is one column document. There is not a way to squise table in one column (you cannot put elephant into lady bag). Table had to be over two columns (inside table* environment). – Zarko Feb 23 '20 at 06:55
  • No no zarko. I meant my article is two column that is why I utilized resizebox – Turkan Feb 23 '20 at 08:17
  • @Turkan, instead that you argued with me about number of columns rather provide content of your table, at least few rows, that we cab see, how it will influence on table design. BTW, your question is not very clear. MWE generate one column document, not two! – Zarko Feb 23 '20 at 10:11
0

You can use \multicolumn{...}{...} anywhere, not just in the table headers. Use it to redefine the alignment of text locally.

vonbrand
  • 5,473