# Set the width of a column according to the first line and wrap text

I have the following code:

\documentclass[12pt]{article}
\begin{document}

\begin{tabular}{|l|}
\hline
the title \\
\hline
very very very very long \\
\hline
\end{tabular}

\end{document}


The code draws a tabular as follows (please ignore the font and background colours):

I would like to wrap the second line of the text according to the width of the first line (so the text "very very very very long" will be displayed in several lines). Does anyone know how to do it?

Like this?

\documentclass[12pt]{article}
\newlength{\mylen}
\settowidth{\mylen}{the title}
\begin{document}

\begin{tabular}{|l|}
\hline
the title \\
\hline
\multicolumn{1}{|p{\mylen}|}{very very very very long} \\
\hline
\end{tabular}

\end{document}


If you don't want those underfull boxes, load array package and use >{\raggedright\arraybackslash}p{\mylen}:

\documentclass[12pt]{article}
\usepackage{array}
\newlength{\mylen}
\settowidth{\mylen}{the title}
\begin{document}

\begin{tabular}{|l|}
\hline
the title \\
\hline
\multicolumn{1}{|>{\raggedright\arraybackslash}p{\mylen}|}{very very very very long} \\
\hline
\end{tabular}

\end{document}


Another attempt for long column case. Use of widthof{<content>} from calc package is needed.

Code

\documentclass[border=20pt]{standalone}
\usepackage{array,calc}

\begin{document}
\noindent
\begin{tabular}{|p{\widthof{the title}}|} \hline
the title \\ \hline
very very very very long  \\ \hline
very very very very very very long  \\ \hline
\end{tabular}

\end{document}