表格儲存格內的換行符號

表格儲存格內的換行符號

我需要在其中一個列標題中換行。我嘗試使用之前帖子中的建議,但由於列標題是一個方程式,我無法使用它們。我需要在第四列的列標題中換行,如下所示:-

在此輸入影像描述

我寫的程式碼是:

\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用一個簡單的指令取代了環境(增加了桌子周圍的垂直間距)\centering。最後,該siunitx軟體包可用於對數字列進行精細格式化。

\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堆疊的垂直對齊方式變更為居中配置:

在此輸入影像描述

相關內容