適切な書式を維持しながら、表のセル内で強制的に改行を行う良い方法は何ですか?

適切な書式を維持しながら、表のセル内で強制的に改行を行う良い方法は何ですか?

表のセル内で強制的に改行する方法について、次のスレッドで提供されているさまざまな解決策をいくつか調べてみました。

表のセル内に強制改行を追加する方法

それに基づいて、提案された改行方法のいくつかを含む表を作成し、それらが序文でリストした表の書式設定とどのように相互作用するかを確認しました。

\documentclass{article}

\usepackage{graphicx} % Required for inserting images

\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}

% Must have features for my tables

\usepackage{array}

\setlength{\tabcolsep}{18pt}

\setlength{\arrayrulewidth}{0.5mm}

\renewcommand{\arraystretch}{1.5}

% ________________________________

\usepackage[usestackEOL]{stackengine} 
% Package for \strutlongstacks{T} forced linebreak method

\usepackage{makecell}
% Package for \makecell forced linebreak method

\begin{document}

\begin{table}[h]

    \centering
    \strutlongstacks{T}
    \begin{tabular}{|c|c|m{3cm}|}
        \hline
        \multicolumn{3}{|c|}{Calender for the rest of the year} \\
        \hline
        \textbf{Date} & \textbf{Homework} & \textbf{Goal}  \\
        \hline
        Week 1 & Homework 1 and 2 & \begin{tabular}[c]{@{}c@{}}Goal for 1 is to... \\ Goal for 2 is to... \end{tabular} \\ % Text exceeds cell when limit is reached
        \hline
        Week 2 & Homework 3 and 4 & \vtop{\hbox{\strut Goal for 3 is to...}\hbox{\strut Goal for 4 is to...}} \\ % Force elongates cell on itself when limit is exceeded
        \hline
        Week 3 & Homework 5 and 6 & \Centerstack{Goal for 5 is to... \\ Goal for 6 is to...} \\ % Text exceeds cell when limit is reached
        \hline
        Week 4 & Homework 7 and 8 & \begin{flushleft} Goal for 7 is to... \\ Goal for 8 is to... \end{flushleft} \\ % Works for long text, too, only for column types of "|m{}|", though, not for regular "|c|"
        \hline
        Week 5 & Homework 9 and 10 & \parbox{3cm}{Goal for 9 is to... \\ Goal for 10 is to...} \\ % Works but completely ignores the \renewcommand{\arraystretch}{1.5} in the preamble
        \hline
        Week 6 & Homework 11 and 12 & \makecell{Goal for 11 is to... \\ Goal for 12 is to...} \\ % Text exceeds cell when limit is reached
        \hline
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label}
\end{table}

\end{document}

出力側でどのように見えるかは、次のようになります。 コードの視覚的表現

目標 1 と 2 の表示方法が気に入っており、前文の設定を補完しているので、m{3cm}3 列目で使用するときに、宿題 7 と 8 のように 2 列目が中央に配置され、テキストがセルに指定された制限を超えたときに自動改行が行われるように変更できれば理想的です。

これらのいずれかを最適化して適切にフォーマットする方法、またはそれを実現する新しい独創的な方法を思いついたら、ここで共有していただければ幸いです:)

答え1

私の理解では、問題は、垂直方向に中央揃えされた固定サイズの複数行の列を取得することです。

では固定サイズを使用できます\makecell。例:

\makecell[c{p{3cm}}]{Goal for 1 is to... \\ Goal for 2 is to...}

は、固定幅 で垂直中央揃えのセルを作成します3cm。この場合、幅は列指定で設定されるため、3cmを に置き換えます\hsize(列の幅が使用されます)。ただし、正しい垂直位置を取得するには、\makecellで処理し、列指定でp{3cm}の代わりにを使用する必要があります。m{3cm}

追加する

\renewcommand{\cellset}{\def\arraytretch{1.5}}%

内部にもっと広いスペースが必要な場合\makecell

完全な例:

\documentclass{article}

\usepackage{graphicx} % Required for inserting images

\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}

% Must have features for my tables

\usepackage{array}
\usepackage{makecell}

\setlength{\tabcolsep}{18pt}
\setlength{\arrayrulewidth}{0.5mm}
\renewcommand{\arraystretch}{1.5}
\renewcommand{\cellset}{\def\arraytretch{1.5}}%

\begin{document}

\begin{table}[h]

  \centering
  \begin{tabular}{|c|c|p{3cm}|}
    \hline
    \multicolumn{3}{|c|}{Calender for the rest of the year}                                 \\
    \hline
    \textbf{Date}   & \textbf{Homework} & \textbf{Goal}                                                                \\
    \hline
    \hline
    Week 1 & Homework 1 and 2
           & \makecell[c{p{\hsize}}]{Goal for 1 is to... lot of things that take many lines \\
             Goal for 2 is to... lot of things that take many lines}                        \\
    \hline
    Week 2 & Homework 3 and 4
           & \makecell[c{p{\hsize}}]{Goal for 3 is to... lot of things that take many lines \\
             Goal for 4 is to... lot of things that take many lines}                        \\
    \hline
    Week 3 & Homework 5 and 6
           & \makecell[c{p{\hsize}}]{Goal for 5 is to... lot of things that take many lines \\
             Goal for 6 is to... lot of things that take many lines}                                     \\
     \hline
  \end{tabular}
  \caption{With \textsf{makecell}}
  \label{tab:my_label}
\end{table}

\end{document}

メイクセル付き

とにかく、今は表形式配列パッケージは、最新のキー値インターフェイスを通じてテーブルをカスタマイズする多くの機能を提供し、従来のテーブル パッケージの機能を再現します。 を使用するとtabularray、垂直方向の中央揃えの列の幅は(で使用される列指定子)3cmで設定され、複数行のセルは中括弧で簡単に取得できます。Q[3cm,m]Qtabularray

Week 1 & Homework 1 and 2  & {Goal for 1 is to...\\  Goal for 2 is to...} \\

例 (書式とコンテンツが分離されているため、より明確になっていることがわかります):

\documentclass{article}

\usepackage{graphicx} % Required for inserting images

\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}

% Must have features for my tables

\usepackage{tabularray}

\begin{document}

\begin{table}[h]
  \centering
  \begin{tblr}
    {
      colspec={ | Q[c,m] | Q[c,m] | Q[3cm,m] |},
      hline{1-Z} = {1}{-}{0.5mm, solid},
      hline{3} = {2}{-}{0.5mm, solid},
      vlines={0.5mm},
      row{2} = {font=\bfseries},
      colsep=18pt,
      stretch=1.5,
    }
    \SetCell[c=3]{c} Calender for the rest of the year
    \\
    Date   & Homework & Goal
    \\
    Week 1 & Homework 1 and 2
    & {Goal for 1 is to... lot of things that take many lines \\[6pt]
      Goal for 2 is to... lot of things that take many lines}
    \\
    Week 2 & Homework 3 and 4
    & {Goal for 3 is to... lot of things that take many lines \\[6pt]
      Goal for 4 is to... lot of things that take many lines}
    \\
    Week 3 & Homework 5 and 6
    & {Goal for 5 is to... lot of things that take many lines   \\[6pt]
      Goal for 6 is to... lot of things that take many lines}
  \end{tblr}
  \caption{With \textsf{tabularray}}
  \label{tab:my_label}
\end{table}

\end{document}

tabularray 付き

答え2

特別なことは必要ないと思います…最後の列の\tabularnewlineのため、行を終了するためにを除いて。\\

\documentclass{article}

\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{array}

\begin{document}

\begin{table}[htp]
\centering
\renewcommand{\arraystretch}{1.5}

\begin{tabular}{|c|c|>{\raggedright}m{5cm}|}
\hline
\multicolumn{3}{|c|}{Calendar for the rest of the year} \tabularnewline
\hline
\textbf{Date} & \textbf{Homework} & \multicolumn{1}{c|}{\textbf{Goal}}  \tabularnewline
\hline
Week 1 & Homework 1 and 2 &
  Goal for 1 is to... lot of things that take many lines \\[6pt]
  Goal for 2 is to... lot of things that take many lines \tabularnewline
\hline
Week 2 & Homework 3 and 4 &
  Goal for 3 is to... lot of things that take many lines \\[6pt]
  Goal for 4 is to... lot of things that take many lines \tabularnewline
\hline
Week 3 & Homework 5 and 6 &
  Goal for 5 is to... lot of things that take many lines \\[6pt]
  Goal for 6 is to... lot of things that take many lines
  Goal for 1 is to... \tabularnewline
\hline
\end{tabular}

\caption{Caption}
\label{tab:my_label}

\end{table}

\end{document}

ここに画像の説明を入力してください

代替レイアウト:

\documentclass{article}

\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{array,booktabs}

\begin{document}

\begin{table}[htp]
\centering

\begin{tabular}{@{}cc>{\raggedright}p{5cm}@{}}
\toprule
\multicolumn{3}{c}{Calendar for the rest of the year} \tabularnewline
\midrule
\textbf{Date} & \textbf{Homework} & \multicolumn{1}{c}{\textbf{Goal}}  \tabularnewline
\midrule
Week 1 & Homework 1 and 2 &
  Goal for 1 is to... lot of things that take many lines \\[6pt]
  Goal for 2 is to... lot of things that take many lines \tabularnewline
\addlinespace
Week 2 & Homework 3 and 4 &
  Goal for 3 is to... lot of things that take many lines \\[6pt]
  Goal for 4 is to... lot of things that take many lines \tabularnewline
\addlinespace
Week 3 & Homework 5 and 6 &
  Goal for 5 is to... lot of things that take many lines \\[6pt]
  Goal for 6 is to... lot of things that take many lines
  Goal for 1 is to... \tabularnewline
\bottomrule
\end{tabular}

\caption{Caption}
\label{tab:my_label}

\end{table}

\end{document}

ここに画像の説明を入力してください

関連情報