テーブルの行が広すぎます

Aug 20 2020

見栄えのするテーブルを作成しました。ただし、テキストが長い一部の行では、ラテックステーブルに正しく表示されません。

残念ながら、彼らはテキストブロックの幅から抜け出しています。長さに基づいて自動的に折り返す方法はありますか?

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

ここで、「教育年数、教育年数、教育年数、教育年数、教育年数」というテキストを含む行のテキストをどのように折り返すことができますか?

回答

2 Mico Aug 20 2020 at 14:00

@leandriisの以前のコメントを反映して、自動改行を可能にするために、tabularからtabularx環境に切り替えXて、2番目の列に列タイプを使用することをお勧めします。\textwidth;以外の全体の幅を自由に設定する必要があります。以下の例では、を使用しています0.8\textwidth

さらに、(a)両方の列を中央揃えにするのではなく、左揃えを使用すること、(b)4つのダミー変数をグループに配置してサブヘッダーを付けることにより、テーブルの構造を増やすことをお勧めします。 、および(c)読みやすさを向上させるために、2番目の列にぶら下げインデントを使用します。

オプションで、(d)のすべての垂直のルールを省略すると、ユーザマクロの一部に使用することをお勧めしますbooktabs-パッケージを\toprule\midrule\bottomrule、と\addlinespaceテーブルに、よりオープンで魅力的な「表情」を与えることを- 。

これとは別に、などの変数名を使用するのは少し雑に見えると思います\textit{ln(wage)}。IMNSHOは、$\ln(\textit{wage})$「ln」に直立した文字と直立した括弧を使用するため、推奨されます。

\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\\
    \addlinespace
    \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}

OPのフォローアップクエリに対処するための補遺:指示

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

tabularx全幅がで0.8\textwidth、2列を含む環境を開始します。最初の列はl、基本的なLaTeX列タイプであるタイプであり、その内容は改行せずに左揃えにする必要があり、2番目の列はタイプLです。L列タイプは経由して、以前の回答で定義されている\newcolumntypeディレクティブ。Lカラムタイプが由来するX順番に、で定義され、カラム型tabularxより塩基の誘導体としてパッケージp列型。私たちの目的では、X列タイプの2つの主な特徴は、(i)必要に応じて(l列タイプとは異なり)自動的に改行を許可することと、(ii)その幅がLaTeXによって残差として動的に計算されることです。、一方ではtabularx環境の全体またはターゲットの幅(ここ0.8\textwidthでは:)と、存在する可能性のある他のすべての列の幅(ここでは、最も広いセルが単語によって設定される1つの列のみ\vn{married})と任意の一方、列間の空白。(L下地から列タイプが異なるX2つの方法でカラムタイプ:それは、その内容が右不規則ではなく、完全に正当化され、それは細胞の第二行から始まる、「ぶら下げインデント」を実装植字。)最後に、2個の@{}粒子がサーブ最初の列の左側と最後の列の右側に挿入される空白のパディングを抑制します。