Table rows too wide

I have created a table which looks nice. However, for some rows having long text is not represented properly in the Latex table.

Unfortunately, they are getting out of the width of the textblock. Is there a way to wrap automatically based on the length?

\begin{table}[htbp]
\caption{Variable Descriptions}
\label{tab:2}
\begin{center}
\begin{tabular}{|c|c|}\hline
Variables & Descriptions\\\hline
\textit{ln(wage)} & log of wage\\
\textit{educ} & years of education, years of education, years of education, years of education, years of education\\
\textit{black} & 1 if black and 0 if not\\
\textit{hisp} & 1 if hispanic and 0 if not\\
\textit{exper} & years of experience\\
\textit{exper$^{2}$} & years of experience squared\\
\textit{married} & 1 if married and 0 if not\\
\textit{union} & 1 if belongs to a union and 0 if not\\\hline
\end{tabular}
\end{center}
\end{table}


Here how can I wrap the text for the row having the text "years of education, years of education, years of education, years of education, years of education"?

• You can use tabularx and its X type column. With the column type the whole table automatically adapts to an overall width of your choice, e. g. textwidth. Aug 20, 2020 at 6:11
• Aug 20, 2020 at 6:12
• For a less busy look of the table, you might consider using a left aligned column for the second column. Aug 20, 2020 at 6:16
• I am trying to use tabularx \begin{tabularx}{\textwidth}{XX} but is there a way to say the length percentage? Or is there a command latex automatically handle it? Aug 20, 2020 at 6:21
• To answer your question: yes, there are ways to change the relative width of multiple X columns in one table, but in your example table it definitely is not worth it. Use a regular c column for the first column instead. Aug 20, 2020 at 6:22

Echoing @leandriis' earlier comment, I'd like to recommend that you switch from a tabular to a tabularx environment and employ the X column type for the second column in order to allow for automatic line breaking. You should feel free to set an overall width other than \textwidth; in the example below, I use 0.8\textwidth.

Additionally, I'd like to suggest that you (a) use left-alignment rather than centering for both columns, (b) provide for more structure in the table by placing the four dummy variables in a group and give them a sub-header, and (c) use hanging indentation in the second column to improve legibility.

Optionally, you may want to (d) omit all vertical rules and use some of the user macros of the booktabs package -- \toprule, \midrule, \bottomrule, and \addlinespace -- to give the table a more open and inviting "look".

Separately, I think it looks a bit sloppy to use a variable name such as \textit{ln(wage)}. IMNSHO, $\ln(\textit{wage})$ is to be preferred as it uses upright letters for "ln" as well as upright parentheses.

\documentclass{article}
\usepackage{tabularx} % for 'tabularx' env. and 'X' col. type
\usepackage{ragged2e} % for \RaggedRight macro
\usepackage{booktabs} % for \toprule, \midrule etc macros
%% create a derivative column type called 'L':
\newcolumntype{L}{>{\RaggedRight\hangafter=1\hangindent=1.5em}X}
% How to typeset variable names:
\newcommand\vn[1]{\textit{#1}}

\begin{document}
\begin{table}[htbp]
\centering
\caption{Variable Names and Descriptions\strut}
\label{tab:2}
\begin{tabularx}{0.8\textwidth}{@{} l L @{}}
\toprule
Name          & Description\\
\midrule
$\ln(\vn{wage})$ & logarithm of wage\\
\vn{educ}     & years of education, years of education, years of education, years of education, years of education\\
\vn{exper}    & years of experience\\
\vn{exper$^{\,2}$}& years of experience squared\\
\multicolumn{2}{@{}l}{Dummy variables:}\\
\vn{black}    & 1 if black, 0 if not\\
\vn{hisp}     & 1 if hispanic, 0 if not\\
\vn{married}  & 1 if married, 0 if not\\
\vn{union}    & 1 if belongs to a union, 0 if not\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}


Addendum to address the OP's follow-up query: The instruction

\begin{tabularx}{0.8\textwidth}{@{} l L @{}}


initiates a tabularx environment whose overall width is 0.8\textwidth and which contains 2 columns. The first column is of type l, which is a basic LaTeX column type, for columns whose contents should be left-aligned without linebreaking, and the second is of type L. The L column type is defined earlier in the answer via a \newcolumntype directive. The L column type is derived from the X column type which, in turn, is defined in the tabularx package as a derivative of the more basic p column type. For our purposes, the two main characteristics of the X column type are (i) it automatically allows line-breaking as needed (unlike the l column type) and (ii) its width is calculated dynamically by LaTeX as a residual, viz., as the difference between the overall or target width of the tabularx environment (here: 0.8\textwidth) on the one hand and the widths of all other columns that may be present (here: just 1 column, whose widest cell is set by the word \vn{married}) plus any intercolumn whitespace on the other hand. (The L column type differs from the underlying X column type in two ways: It typesets its contents ragged-right rather than fully justified, and it implements "hanging indentation", starting with the second row of the cell.) Finally, the two @{} particles serve to suppress the whitespace padding that would otherwise be inserted to the left of the first column and to the right of the final column.

• tryig to understand, what this command is doing? \begin{tabularx}{0.8\textwidth}{@{} l L @{}} Aug 21, 2020 at 4:43
• @Exploring - Please see the addendum I just posted. It turns out that explaining fully what \begin{tabularx}{0.8\textwidth}{@{} l L @{}}  does takes more than just two or three sentences...
– Mico
Aug 21, 2020 at 5:06