在表格中添加换行符

在表格中添加换行符

我有以下乳胶代码:

%\usepackage{multirow}    
\begin{tabular}{|c|c|c|c|c|c|}
    \hline
    \multirow{2}{*}{Position}                                      & \multirow{2}{*}{CF ($t=0$)}                               & \multicolumn{4}{c|}{CF ( $t=T$ )}                                                                                                                                         \\ \cline{3-6} 
                                                                   &                                                           & $S_T \le 40$                                                       & $40<S_T \le 50$                                                    & $50<S_T \le 70$ & $S_T \ ge 70$ \\ \hline
    \begin{tabular}[c]{@{}c@{}}Long 2 Puts  (K=40)\end{tabular}  & \begin{tabular}[c]{@{}c@{}}$-2(10)  = -20$\end{tabular} & \begin{tabular}[c]{@{}c@{}}$2(40-S_T)  = 80 - 2S_T$\end{tabular} & $0$                                                                & $0$             & $0$           \\ \hline
    \begin{tabular}[c]{@{}c@{}}Long Put  (K=70)\end{tabular}     & $-30$                                                     & $70-S_T$                                                           & $70-S_T$                                                           & $70-S_T$        & $0$           \\ \hline
    \begin{tabular}[c]{@{}c@{}}Short 3 Puts  (K=50)\end{tabular} & \begin{tabular}[c]{@{}c@{}}$+3(20) =60$\end{tabular}    & \begin{tabular}[c]{@{}c@{}}$-3(50 - S_T)=3S_T-150$\end{tabular} & \begin{tabular}[c]{@{}c@{}}$-3(50 - S_T) =3S_T-150$\end{tabular} & $0$             & $0$           \\ \hline
    \textbf{Total}                                                 & $10$                                                      & $0$                                                                & $2S_T-80$                                                          & $70-S_T$        & $0$           \\ \hline
    \end{tabular}

这是我想要的表格,它可以工作,但是它太宽了,无法放入我的文档中,所以我只是想在某些单元格中简单地放入换行符 \\,例如相同的代码:

\begin{tabular}{|c|c|c|c|c|c|}
\hline
\multirow{2}{*}{Position}                                      & \multirow{2}{*}{CF ($t=0$)}                               & \multicolumn{4}{c|}{CF ( $t=T$ )}                                                                                                                                         \\ \cline{3-6} 
                                                               &                                                           & $S_T \le 40$                                                       & $40<S_T \le 50$                                                    & $50<S_T \le 70$ & $S_T \ ge 70$ \\ \hline
\begin{tabular}[c]{@{}c@{}}Long 2 Puts  (K=40)\end{tabular}  & \begin{tabular}[c]{@{}c@{}}$-2(10)  = -20$\end{tabular} & \begin{tabular}[c]{@{}c@{}}$2(40-S_T)  = 80 - 2S_T$\end{tabular} & $0$                                                                & $0$             & $0$           \\ \hline
\begin{tabular}[c]{@{}c@{}}Long Put  (K=70)\end{tabular}     & $-30$                                                     & $70-S_T$                                                           & $70-S_T$                                                           & $70-S_T$        & $0$           \\ \hline
\begin{tabular}[c]{@{}c@{}}Short 3 Puts  \\(K=50)\end{tabular} & \begin{tabular}[c]{@{}c@{}}$+3(20) =60$\end{tabular}    & \begin{tabular}[c]{@{}c@{}}$-3(50 - S_T)=3S_T-150$\end{tabular} & \begin{tabular}[c]{@{}c@{}}$-3(50 - S_T) =3S_T-150$\end{tabular} & $0$             & $0$           \\ \hline
\textbf{Total}                                                 & $10$                                                      & $0$                                                                & $2S_T-80$                                                          & $70-S_T$        & $0$           \\ \hline
\end{tabular}

除非我在 (K=50) 前面放置一个 \\,否则代码不再起作用。我该如何修复这个问题?

答案1

解决方案如下makecell

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\usepackage{mathtools}
\usepackage{amsfonts}
\usepackage{xcolor}
\usepackage{array, multirow, makecell}
\setcellgapes{4pt}

\begin{document}

\centering\makegapedcells\setlength\tabcolsep{5pt}
\begin{tabular}{|c|*{5}{>{$}c<{$}|}}
  \hline
  \multirowcell{2}[-1ex]{Position} & \multirowcell{2}[-1ex]{CF ($t=0$)} & \multicolumn{4}{c|}{CF ( $t=T$ )} \\ \cline{3-6}
  & & S_T ≤ 40 & 40<S_T ≤ 50 & 50<S_T ≤ 70 & S_T ≥ 70 \\
  \hline
  \makecell{Long 2 Puts \\ (K=40)} & -2(10) = -20 & \begin{matrix*}[r]2(40-S_T)\quad{}\\= 80 - 2S_T\end{matrix*} & 0 & 0 & 0 \\
  \hline
  \makecell{Long Put \\ (K=70)} & -30 & 70-S_T & 70-S_T & 70-S_T & 0 \\
  \hline
  \makecell{Short 3 Puts \\ (K=50)} & +3(20) =60 & \begin{matrix*}[r]-3(50 - S_T)\quad {}\\=3S_T-150\end{matrix*} & \begin{matrix*}[r]-3(50 - S_T)\quad {}\\ =3S_T-150\end{matrix*} & 0 & 0 \\
  \hline
  \textbf{Total} & 10 & 0 & 2S_T-80 & 70-S_T & 0 \\
  \hline
\end{tabular}

\end{document} 

在此处输入图片描述

答案2

我总是考虑重新格式化太宽的表格,尤其是当它们很短时。对我来说,这表明表格需要旋转 90 度:

\documentclass{article}
\usepackage{booktabs}

\usepackage{multirow}

\begin{document}

\newcommand{\st}{\ensuremath{S_T}}

\begin{table}
\centering
\begin{tabular}{cccccc} \toprule
& & \multicolumn{3}{c}{$K$} \\ \cmidrule{3-5}
Position & & 40 & 70 & 50 & Total \\ \midrule
CF ($t=0$) & & -20 & -30 & 60 & 10 \\ \addlinespace
\multirow{4}{*}{CF ($t=T$)} & $\st \le 40$ & $80-2 \st$ &  $70-\st$ & $3 \st - 150$ & 0 \\
& $40 < \st \le 50$ & 0 & $70-\st$ & $3 \st - 150$ & $2 \st - 80$ \\
& $50 < \st \le 70$ & 0 & $70-\st$ & 0 & $70-\st$\\
& $\st \ge 70$ & 0 & 0 & 0 & 0 \\ \bottomrule
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

相关内容