2

I've managed to create a table with multirow and multicolumn with fixed widths in the columns. However, I cannot change the width of the column that contains 'Condition'. Ideally I want the text within the column to wrap. Does someone know how I can get the text to wrap and be a set width? Right now I gave it the default '*'. Thank you!

\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{table}
  \begin{tabular}{|c|c|c|c|c|c|c|}

    \hline
    \multirow{3}{*}{Condition} &

    \multicolumn{6}{|c|}{Metrics} \\
    \cline{2-7}

    & \multicolumn{2}{|C{3cm}|}{Length ($\mu$m)}
    & \multicolumn{2}{|C{3cm}|}{Width ($\mu$m)}
    & \multicolumn{2}{|C{3cm}|}{Area ($\mu$m\textsuperscript{2})} \\

    \cline{2-7}
    & \multicolumn{1}{|C{1.5cm}|}{Mean}
    & \multicolumn{1}{|C{1.5cm}|}{SD}
    & \multicolumn{1}{|C{1.5cm}|}{Mean}
    & \multicolumn{1}{|C{1.5cm}|}{SD}
    & \multicolumn{1}{|C{1.5cm}|}{Mean}
    & \multicolumn{1}{|C{1.5cm}|}{SD}  \\
    \hline
  \end{tabular}

\end{table}
  • Welcome to TeX.StackExchange. Your description is great... however a complete code with the \documentclass and \usepackages relevant to your example might help contributors even more precisely. – Partha D. Feb 16 at 7:49
  • Apart from your question, you might be interested in the siunitx package. It could be useful if you are going to fill your table with numbers as the package introduces a new S column that allows you to align numbers with respect to their decimal separator. Apart from that you might also want to have a look at the booktabs package. – leandriis Feb 16 at 8:47
  • Do you just want the word 'Condition' to get hyphenated if it shows up in the first column, or should all long words in the first column get hyphenated (if needed, of course)? Please advise. – Mico Feb 16 at 9:48
3

Instead of using fixed column widths and the m column type, I would load the tabularx package and use its tabularx environment. This approach makes sure that the table will fit inside the width of the text block. In addition, the X column type (and derived column types, such as C and L in the example below) allow automatic line-wrapping.

I would also omit all vertical bars (they're not needed! really!) and use the line-drawing macros of the booktabs package instead of \hline and \cline. Oh, and do look into the siunitx package and, in particular, this package's \si macro that simplifies the consistent writing of scientific units)

enter image description here

\documentclass{article}
\usepackage{tabularx,booktabs,ragged2e,siunitx}
\newcolumntype{C}{>{\Centering\arraybackslash}X}
\newcolumntype{L}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcommand\mytab[1]{\smash{%
  \begin{tabular}[t]{@{}L@{}} #1 \end{tabular}}}
\begin{document}
\begin{table}
\setlength\tabcolsep{5pt} % optional (default is 6pt)

\begin{tabularx}{\textwidth}{@{} L *{6}{C} @{}}
\toprule
\mytab{Condition} & \multicolumn{6}{c@{}}{Metrics} \\
\cmidrule(l){2-7}
& \multicolumn{2}{c}{Length (\si{\micro\meter})}
& \multicolumn{2}{c}{Width (\si{\micro\meter})}
& \multicolumn{2}{c@{}}{Area (\si{\micro\meter\squared})} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-7} 
& Mean & SD & Mean & SD & Mean & SD  \\
\midrule
Whatever & \dots & \dots & \dots & \dots & \dots & \dots \\
$\cdots$ \\
Whatever \\
\bottomrule
\end{tabularx}

\end{table}
\end{document}
1

You now have a {=} argument for \multirows inserted in fixed width columns.

Unrelated: the symbol for micro in units uses an upright mu, not the default italic mu from maths fonts. It's better to use siunitsx` to have a correct typing of units.

\documentclass{article}
\usepackage{array}
\usepackage{multirow}
\usepackage{siunitx}
\begin{document}


\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\setlength{\extrarowheight}{2pt}
\begin{table}
\begin{tabular}{|*{7}{C{1cm}|}}
    \hline
    \multirow{3}{=}{Condi\-tion} &
    \multicolumn{6}{c|}{Metrics} \\
    \cline{2-7}
    & \multicolumn{2}{c|}{Length (\si{\um})}
    & \multicolumn{2}{c|}{Width (\si{\um})}
    & \multicolumn{2}{c|}{Area (\si{\um\squared})} \\
    \cline{2-7}
    & Mean
    & SD
    & Mean
    & SD
    & Mean
    & SD \\
    \hline
\end{tabular}
\end{table}

\end{document} 

enter image description here

  • @Mico: You're right. Actually, I didn't check details of the original code, and mainly focused on \multirow. I'll fix that in a moment. Thank you for pointing the problem! – Bernard Feb 16 at 13:58
0

Without much expertise in multirow I changes the * to 0.6cm in the second parameter of \multirow{3}{0.6cm}{Con\-di\-tion} and added possible hyphenations to "Condition". This produced the following table, which might be close to what you want:

enter image description here

Here's the complete code I used:

\documentclass{article}
\usepackage{tabularx}
\usepackage{multirow}

\begin{document}


\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{table}
\begin{tabular}{|c|c|c|c|c|c|c|}

    \hline
    \multirow{3}{0.6cm}{Con\-di\-tion} &

    \multicolumn{6}{|c|}{Metrics} \\
    \cline{2-7}

    & \multicolumn{2}{|C{3cm}|}{Length ($\mu$m)}
    & \multicolumn{2}{|C{3cm}|}{Width ($\mu$m)}
    & \multicolumn{2}{|C{3cm}|}{Area ($\mu$m\textsuperscript{2})} \\

    \cline{2-7}
    & \multicolumn{1}{|C{1.5cm}|}{Mean}
    & \multicolumn{1}{|C{1.5cm}|}{SD}
    & \multicolumn{1}{|C{1.5cm}|}{Mean}
    & \multicolumn{1}{|C{1.5cm}|}{SD}
    & \multicolumn{1}{|C{1.5cm}|}{Mean}
    & \multicolumn{1}{|C{1.5cm}|}{SD}  \\
    \hline
\end{tabular}

\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.