0

I want to type this table in LaTeX:

  • Cell A should have an automatic width, according to its content;

  • Cell B has a fixed width (e.g. 10 cm), and the text inside will wrap;

  • Cell C has a width of the sum of A and B, and of course allow text to wrap.

enter image description here

1

tabularx is most suitable for such cases. To fulfill the requirements you provided, cell A should be specified l and cell B should fill the remaining space (but not a fixed width as you said in the question) by specifying X column type for it. Finally, cell C is a little more tricky. It should occupy a length equal to the sum of cell A + cell B, but still you have to subtract two \tabcolseps plus two \arrayrulewidths. This is achieved by using p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth} for cell C.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\begin{document}

\begin{tabularx}{\linewidth}{|l|X|}
\hline
cell A & Cell B has a fixed width (e.g. 10 cm), and the text inside will wrap; \\ \hline
\multicolumn{2}{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{Cell C has a width of the sum of A and B, and of course allow text to wrap.} \\ \hline
\end{tabularx}

\end{document}

enter image description here

  • Thank you for your help! But the answer is still not perfect: the width of text in cell C is just the same as cell B, but not A+B. – stone-zeng Nov 5 '17 at 16:34
  • 1
    Yes, please see my edit for the exact cell size for cell C. – AboAmmar Nov 5 '17 at 16:54
0

Based on your description, your table width is variable, depending only on what is contained in Cell A. To that end, you can measure the width of Cell A using zref's savepos module; specifically, \zsaveposx{<mark>} to save the x-coordinate (in scaled points or sps) on the page as <mark>. This can then be used in dimension expressions to calculate the width of the second row:

enter image description here

\documentclass{article}

\usepackage{zref-savepos}

\begin{document}

\begin{tabular}{ | l | p{10cm} | }
  \hline
  \zsaveposx{CellAl-1}Cell \texttt{A}\zsaveposx{CellAr-1} & 
    Cell \texttt{B} has a fixed width (\textit{e.g.}, \texttt{10cm}), and the text inside will wrap to the following line; \\
  \hline
  \multicolumn{2}{| p{\dimexpr10cm+\zposx{CellAr-1}sp-\zposx{CellAl-1}sp+2\tabcolsep+\arrayrulewidth} |}{%
    Cell \texttt{C} has a width of the sum of \texttt{A} and \texttt{B}, and of course allow text to wrap to subsequent
    lines as needed, regardless of what is presented in \texttt{A}.%
  } \\
  \hline
\end{tabular}

\begin{tabular}{ | l | p{10cm} | }
  \hline
  \zsaveposx{CellAl-2}Cell \texttt{A} plus stuff\zsaveposx{CellAr-2} & 
    Cell \texttt{B} has a fixed width (\textit{e.g.}, \texttt{10cm}), and the text inside will wrap to the following line; \\
  \hline
  \multicolumn{2}{| p{\dimexpr10cm+\zposx{CellAr-2}sp-\zposx{CellAl-2}sp+2\tabcolsep+\arrayrulewidth} |}{%
    Cell \texttt{C} has a width of the sum of \texttt{A} and \texttt{B}, and of course allow text to wrap to subsequent
    lines as needed, regardless of what is presented in \texttt{A}.%
  } \\
  \hline
\end{tabular}

\end{document}

Cell A is set inside a left-aligned column, which expands naturally to fit the content. Cell B is fixed at 10cm. The width of Cell C accommodates for the width of Cell A (\zposx{<right>}sp-\zposx{<left>}), 2 regular \tabcolseps inserted around the vertical rule between Cell A and B, one rule width, and 10cm.

Since this uses the a \label-\ref-like system, you need to compile at least twice if anything within Cell A changes.

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.