2

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): enter image description here

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?

2

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}

enter image description here

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}
1

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

enter image description here

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}

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.