表のセル内の改行

表のセル内の改行

列ヘッダーの 1 つに改行が必要です。以前の投稿の提案を使用しようとしましたが、列ヘッダーが方程式であるため、使用できませんでした。次に示すように、4 番目の列の列ヘッダーに改行が必要です。

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

私が書いたコードは次のとおりです:

\begin {table}[h]

\begin{center}

\begin{tabular}{|l|l|l|l|}

\hline

$i$ & $Degree \phi_i$ & $\cos(\phi_i)$ & Product of $\cos(\phi_i)\par K=\prod K_i=\prod\cos(\phi_i)$ \\

\end{tabular}

\end{center}

\end {table}

前もって感謝します

答え1

いつでも表形式の中に表形式を配置できます。

\documentclass{article}


\begin{document}

\begin {table}[h]

    \begin{center}

    \begin{tabular}{|l|l|l|l|}

    \hline

    $i$ & Degree $\phi_i$ & $\cos(\phi_i)$ &\begin{tabular}{@{}l} Product of $\cos(\phi_i)$\\$ K=\prod K_i=\prod\cos(\phi_i)$
\end{tabular} \\

    \end{tabular}

    \end{center}

    \end {table}


\end{document} 

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

(ドル記号の異なる使い方に注意してください)。

答え2

パッケージの使用をお勧めしますmakecell。これにより、列見出しの共通の書式設定とセル内の改行が可能になります。また、縦線がないと表の見栄えが良くなります。パッケージは、さまざまな太さの水平線と、これらの線の周りに垂直方向のパディングを定義します。私は、環境 (表の周りに垂直方向のスペースを追加する) を単純なディレクティブにbooktabs置き換えました。最後に、パッケージは数値列の細かい書式設定のためにあります。center\centeringsiunitx

\documentclass[twoside]{report}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{ table-format=1.8,table-number-alignment=center}
\usepackage{makecell}
\usepackage{cellspace}
\setcellgapes[t]{3pt}
\setcellgapes[b]{1pt}

\renewcommand\theadfont{\bfseries\boldmath}
\begin{document}

\begin {table}[h]
\centering\makegapedcells
%\begin{tabular}{|l|S[table-format=2.8]|*{2}{S|}}
%\hline

\begin{tabular}{lS[table-format=2.8]*{2}{S}}
  \toprule
  {\thead{$i$}} & {\thead{Degree $\phi_i$}} & {\thead{$\cos(\phi_i)$}} & {\thead{Product of $\cos(\phi_i) $ \\ $K=\prod K_i=\prod\cos(\phi_i)$}} \\
  \midrule
  1 & 45.00000000 & 0.70710700 & 0.70710678 \\
  2 & 26.56505118 & 0.89442700 & 0.63245553 \\
  3 & 14.03624347 & 0.97014300 & 0.60883391 \\
  4 & 7.12501635 & 0.99227800 & 0.60764826 \\
  \bottomrule
\end{tabular}
\end {table}

\end{document} 

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

答え3

この場合、単純な左揃えのスタックで十分です。また、スタックの上下に垂直バッファを追加して、水平線をデフォルトよりもオフセットします。

\documentclass{article}
\usepackage{stackengine}

\begin{document}
\begin {table}[h]

\begin{center}

\begin{tabular}{|l|l|l|l|}

\hline

$i$ & Degree $\phi_i$ & $\cos(\phi_i)$ & \def\stackalignment{l}
  \addstackgap[2pt]{%
  \stackunder{Product of $\cos(\phi_i)$}{$K=\prod K_i=\prod\cos(\phi_i)$}} \\
\hline
\end{tabular}

\end{center}

\end {table}
\end{document}

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

マクロを変更するだけで\stackunder\stackanchorスタックの垂直方向の配置が中央揃えの構成に変わります。

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

関連情報