1

I have created a simple 3-column table, in which the first row has 3 columns, and in the second row the columns are merged into one. The text in the second row is very long.

\documentclass{article}
\usepackage{tabularx}
    
\begin{document}
  \begin{center}
    \begin{tabularx}{\linewidth}{|r|X|r|}
        \hline
        \cellcolor{black!25}Techniques & \cellcolor{black!5}Artifacts & \cellcolor{black!5}Budget \\
        \hline
        \multicolumn{3}{|c|}{Island Museum analyzes historical artifacts using one or more techniques described below – all but one of which is performed by an outside laboratory – to obtain specific information about an object’s creation. For each type of material listed, the museum uses only the technique described:}
        \hline
    \end{tabularx}
  \end{center}
\end{document}

Obviously, as LaTeX does not automatically wrap text inside a table, the second row text extends past the table edge (and even page edge).

enter image description here

The usually suggested solution to text wrapping inside a table is to set column width (fixed or relative) when beginning the table. I tried that, e.g. \begin{tabularx}{\linewidth}{|p{0.25cm}|X|p{0.25cm}|} but that doesn't help.

3
  • 2
    \multicolumn{3}{|p{\dimexpr\linewidth-2\tabcolsep}|}{... }
    – Fran
    Commented Jun 26, 2021 at 18:28
  • 2
    you specified the merged cell as c which is a single line centred cell which is why the text stayed on one line. Commented Jun 26, 2021 at 18:35
  • Apart from the mentioned issue, your code currently is not compilable. \usepackage{colortbl} or even better \usepackage[table]{xcolor} is missing. Also, a \\ after the \multicolumn and before the \hline is missing.
    – leandriis
    Commented Jun 27, 2021 at 8:19

2 Answers 2

2

A c type column is as wide as its widest contents. A \multicolumn{...}[c}{...} containing a lenghty text will therefore let its contents protrude into the margins and beyond the page, irrespective of the other column types used in the table.

To overcome this, we can easily calculate the required width. Since your table is supposed to be exactly as wide \textwidth, so is your \multicolumn cell. Since there are vertical lines to either side of your \multicolumn cell, let's substract their width (\arrayrulewidth) from \textwidth. There is also a small horizontal white space between the vertical line and the start/end of the text in the cell, so let's also substract that (\tabcolesp). With thouse changes you end up with the following output:

enter image description here

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
    
\begin{document}

\noindent
    \begin{tabularx}{\linewidth}{|r|X|r|}
        \hline
        \cellcolor{black!25}Techniques & \cellcolor{black!5}Artifacts & \cellcolor{black!5}Budget \\
        \hline
        \multicolumn{3}{|p{\dimexpr\linewidth-2\arrayrulewidth-2\tabcolsep}|}{Island Museum analyzes historical artifacts using one or more techniques described below – all but one of which is performed by an outside laboratory – to obtain specific information about an object’s creation. For each type of material listed, the museum uses only the technique described:} \\
        \hline
    \end{tabularx}

\end{document}
0

An answer has been added but I was not satisfied with the color scheme and the right alignment so a modification to the table used here -- \hhline not working with \multirow and \cellcolor

enter image description here

\documentclass{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage[svgnames,table]{xcolor}
\usepackage{makecell, multirow, tabularx}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}
    \renewcommand\theadfont{\normalsize\bfseries\color{white}}
\usepackage{hhline}

\definecolor{headers}{RGB}{0,137,182}
\definecolor{M1}{RGB}{163,195,217}
\definecolor{M2}{RGB}{155,214,220}


\begin{document}
    \setlength\arrayrulewidth{2pt}
    \arrayrulecolor{white}
    \def\clinecolor{\hhline{|>{\arrayrulecolor{M2}}->{\arrayrulecolor{white}}|-|-|}}
    \begin{tabularx}{\textwidth}%
    {|>{\hsize=0.5\hsize\columncolor{M2}}L|
      >{\columncolor{M2}}L|
      >{\hsize=0.5\hsize\columncolor{M2}}L|
    }
    \rowcolor{headers}
    \thead{Techniques }          & \thead{Artifacts}                & \thead{Budget}         \\
    \hhline{|-|-|-|}
    \rowcolor{M1}
            \multicolumn{3}{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{Island Museum analyzes historical artifacts using one or more techniques described below – all but one of which is performed by an outside laboratory – to obtain specific information about an object’s creation. For each type of material listed, the museum uses only the technique described:}                            \\
    \end{tabularx}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .