3

I need a line break in one of the column headers. I tried using the suggestions from the earlier posts but since the column header is an equation, I couldn't use them. I need the line break in the column header of 4th column as given below :-

enter image description here

The code i have written is :

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

Thanks in advance

3 Answers3

2

You can always put a tabular inside a tabular:

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

enter image description here

(Please observe a different usage of dollar signs).

1

In this case, a simple left-aligned stack would do. I also add a vertical buffer above/below the stack to offset the horizontal lines more than the default.

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

enter image description here

Merely changing the macro \stackunder to \stackanchor changes the vertical alignment of the stack to a centered configuration:

enter image description here

1

I suggest using the makecell package. It allows for a common formatting in column heads and line breaks in cells. Also your table will look better without vertical lines. The booktabs package defines horizontal rules with variable thickness and some vertical padding around these rules. I replaced the center environment (which adds vertical spacing around the table) with a simple \centering directive. Last, not least, the siunitx package is there for a fine formatting of numerical columns.

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

enter image description here

Bernard
  • 271,350