如何防止表格内的数学运算出现换行?

如何防止表格内的数学运算出现换行?

我有一段 TeX 描述了一个表格

\begin{table}[h]
\begin{tabular}{|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|}
\hline
\sloppy Angle ($\theta$) deg \newline $\Delta\theta=\pm0.5$ & \sloppy Time (t) s \newline $\Delta t\nolinebreak=\pm0.11$ & \sloppy Time (t) s \newline $\Delta t\nolinebreak=\pm0.11$ & \sloppy Time (t) s \newline $\Delta t\nolinebreak=\pm0.11$ & \sloppy Time (t) s \newline $\Delta t\nolinebreak=\pm0.11$ & \sloppy Time (t) s \newline $\Delta t\nolinebreak=\pm0.11$ & \sloppy Time (t) s \newline $\Delta t=\pm0.11$ \\ \hline
0.00                         & Did not move      & Did not move      & Did not move      & Did not move      & Did not move      & Did not move      \\ \hline
15.0                         & 3.95              & 3.90              & 3.73              & 3.93              & 3.86              & 3.87              \\ \hline
30.0                         & 2.77              & 2.84              & 2.74              & 2.80              & 2.81              & 2.79              \\ \hline
45.0                         & 2.66              & 2.71              & 2.69              & 2.67              & 2.65              & 2.68              \\ \hline
60.0                         & 3.39              & 3.23              & 3.26              & 3.34              & 3.32              & 3.31              \\ \hline
75.0                         & 5.01              & 5.00              & 4.91              & 4.91              & 4.95              & 4.96              \\ \hline
90.0                         & Did not move      & Did not move      & Did not move      & Did not move      & Did not move      & Did not move      \\ \hline
\end{tabular}
\end{table}

当我在 Windows 10 上使用 pdflatex 构建它时,我得到了这个输出。

TeX 输出

我想要输出的是这样的:

期望输出

我希望时间 (t) 和 s 位于不同的行,而 \delta t 和 \pm0.11 位于表格第一行的同一行。另外,我希望“未移动”位于同一行。

答案1

我使用\mbox{}以避免换行。此外 - 这与您的问题无关 - 我认为这Time (t) s不是一种“正确”的格式。我会改用Time $t$ in s因为t是一个变量并且应该是斜体(意思是在数学模式下)。“in”也只是一个建议。

\documentclass[]{article}
\usepackage[showframe=true,margin=20mm]{geometry}
\usepackage{float}

\begin{document}

\begin{table}[h]
\begin{tabular}{|p{0.142\textwidth}|p{0.142\textwidth}|}
\hline
% Cell 1
    Angle $\theta$ \newline 
    in deg  \newline 
    \mbox{$\Delta\theta=\pm0.5$} 
& 
% Cell 2 
    Time $t$ \newline 
    in s \newline 
    \mbox{$\Delta t=\pm0.11$}
    \\ \hline
% Data
0.00 & Did not move   
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

即使你制造了细胞太小了(例如 0.042\textwidth)那么仍然没有换行符:

在此处输入图片描述

也许看看包 tabularx(错误:缺少数字,视为零。)以及@Werner在他的评论中建议。

答案2

我建议避免重复信息;此外,拼写“没有移动”会使表格过长。

\documentclass{article}
\usepackage{siunitx,booktabs}

\begin{document}
\begin{table}[htp]
\centering
\newcommand{\dnm}{{DNM}}

\begin{tabular}{
  @{}
  S[table-format=2.0]
  *{6}{S[table-format=1.2]}
  @{}
}
\toprule
{Angle (deg)} & \multicolumn{6}{c@{}}{Time (s)} \\
{$\theta$} & {$t_1$} & {$t_2$} & {$t_3$} & {$t_4$} & {$t_5$} & {$\bar{t}$}\\
\cmidrule(r){1-1} \cmidrule(l){2-7}
{$\Delta\theta=\pm0.5$} & \multicolumn{6}{c@{}}{$\Delta t=\pm0.11$} \\
\midrule
 0 & \dnm & \dnm & \dnm & \dnm & \dnm & \dnm \\
15 & 3.95 & 3.90 & 3.73 & 3.93 & 3.86 & 3.87 \\
30 & 2.77 & 2.84 & 2.74 & 2.80 & 2.81 & 2.79 \\
45 & 2.66 & 2.71 & 2.69 & 2.67 & 2.65 & 2.68 \\
60 & 3.39 & 3.23 & 3.26 & 3.34 & 3.32 & 3.31 \\
75 & 5.01 & 5.00 & 4.91 & 4.91 & 4.95 & 4.96 \\
90 & \dnm & \dnm & \dnm & \dnm & \dnm & \dnm \\
\bottomrule
\multicolumn{7}{@{}l@{}}{\footnotesize\dnm: Did not move}
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

答案3

由于不知道您的文档的页面布局,以下解决方案基于猜测,这与我们从\usepackage[showframe]{geometry}包中获得的结果接近。

使用包siunitx(用于单位和列格式化)、包tabularx(用于对具有等宽列的表格进行简单格式化)和makecell包标题,我得到了下表:

在此处输入图片描述

\documentclass{article}
\usepackage[showframe]{geometry}% <-- this solve problem with breaking equation into two line
\usepackage{makecell,tabularx}
\newcommand\mcx[1]{\multicolumn{1}{>{\centering\arraybackslash}X|}{#1}}
\usepackage{siunitx}

\begin{document}
    \begin{table}
\begin{tabularx}{\linewidth}{|S[table-format=2.2]|
                         *{6}{S[table-format=1.2]|}
                            }
    \hline
{\thead{Angle ($\theta$)\\ $\Delta\theta=\SI{\pm0.5}{\degree}$}}
        &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                        &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                                &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                                        &  {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                                                &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}  \\
    \hline
0.00    & \mcx{Did not move}    
                & \mcx{Did not move}    
                        & \mcx{Did not move}      
                                & \mcx{Did not move}
                                        & \mcx{Did not move}
                                                & \mcx{Did not move}    \\ \hline
15.0    & 3.95  & 3.90  & 3.73  & 3.93  & 3.86  & 3.87                  \\ \hline
30.0    & 2.77  & 2.84  & 2.74  & 2.80  & 2.81  & 2.79                  \\ \hline
45.0    & 2.66  & 2.71  & 2.69  & 2.67  & 2.65  & 2.68                  \\ \hline
60.0    & 3.39  & 3.23  & 3.26  & 3.34  & 3.32  & 3.31                  \\ \hline
75.0    & 5.01  & 5.00  & 4.91  & 4.91  & 4.95  & 4.96                  \\ \hline
90.0    & \mcx{Did not move}
                & \mcx{Did not move}
                        & \mcx{Did not move}
                                & \mcx{Did not move}
                                        & \mcx{Did not move}
                                                & \mcx{Did not move}    \\ \hline
\end{tabularx}
    \end{table}
\end{document}

注意: 的默认字体大小\thead\footnotesize

编辑: 将页边距减少到 25mm,\tabcolsep将默认的 6pt 减少到 3pt,并将thead字体大小增加到小\arraystretch到 1.2,表格变为:

在此处输入图片描述

\documentclass{article}
\usepackage[showframe,
            margin=25mm]{geometry}% <-- this solve problem with breaking equation into two line
\usepackage{makecell,tabularx}
\renewcommand\theadfont{\small}
\newcommand\mcx[1]{\multicolumn{1}{>{\centering}X|}{#1}}
\usepackage{siunitx}

\begin{document}
    \begin{table}
\linespread{0.8}
\setlength\tabcolsep{3pt}
\renewcommand\arraystretch{1.2}
\begin{tabularx}{\linewidth}{|S[table-format=2.2]|
                         *{6}{S[table-format=1.2]|}
                            }
    \hline
{\thead{Angle ($\theta$)\\ $\Delta\theta=\SI{\pm0.5}{\degree}$}}
        &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                        &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                                &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                                        &  {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}
                                                &   {\thead{Time ($t$)\\ $\Delta t=\SI{\pm0.11}{s}$}}  \\
    \hline
0.00    & \mcx{Did not move}    
                & \mcx{Did not move}    
                        & \mcx{Did not move}      
                                & \mcx{Did not move}
                                        & \mcx{Did not move}
                                                & \mcx{Did not move}    \\ \hline
15.0    & 3.95  & 3.90  & 3.73  & 3.93  & 3.86  & 3.87                  \\ \hline
30.0    & 2.77  & 2.84  & 2.74  & 2.80  & 2.81  & 2.79                  \\ \hline
45.0    & 2.66  & 2.71  & 2.69  & 2.67  & 2.65  & 2.68                  \\ \hline
60.0    & 3.39  & 3.23  & 3.26  & 3.34  & 3.32  & 3.31                  \\ \hline
75.0    & 5.01  & 5.00  & 4.91  & 4.91  & 4.95  & 4.96                  \\ \hline
90.0    & \mcx{Did not move}
                & \mcx{Did not move}
                        & \mcx{Did not move}
                                & \mcx{Did not move}
                                        & \mcx{Did not move}
                                                & \mcx{Did not move}    \\ \hline
\end{tabularx}
    \end{table}
\end{document}

笔记2):当列宽足够大时,方程式就不会被打破,并防止其被打破\mbox{$<equation>$}。此修复可能会导致表格中出现新问题:如果单元格比列宽窄——方程式会溢出到右侧,如图所示Manuel Kuehner 博士答案。我的结论是:修复您的表格的方法是使列的宽度更大......

答案4

仍然不漂亮,但是

在此处输入图片描述

\documentclass{article}

\begin{document}
\begin{table}[htp]
\begin{tabular}{|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|p{0.142\linewidth}|}
 \hline
 \centering\small Angle~($\theta$)\,deg \\ $\Delta\theta=\pm0.5$ &
 \centering\small Time (t)\,s \\ $\Delta t=\pm0.11$ &
 \centering\small Time (t)\,s \\ $\Delta t=\pm0.11$ &
 \centering\small Time (t)\,s \\ $\Delta t=\pm0.11$ &
 \centering\small Time (t)\,s \\ $\Delta t=\pm0.11$ &
 \centering\small Time (t)\,s \\ $\Delta t=\pm0.11$ &
 \centering\small Time (t)\,s \\ $\Delta t=\pm0.11$ \tabularnewline 
\hline
0.00                         &
 \centering Did not move      &
 \centering Did not move      &
 \centering Did not move      &
 \centering Did not move      &
 \centering Did not move      &
 \centering Did not move      \tabularnewline
 \hline
15.0                         & 3.95              & 3.90              & 3.73              & 3.93              & 3.86              & 3.87              \\ \hline
30.0                         & 2.77              & 2.84              & 2.74              & 2.80              & 2.81              & 2.79              \\ \hline
45.0                         & 2.66              & 2.71              & 2.69              & 2.67              & 2.65              & 2.68              \\ \hline
60.0                         & 3.39              & 3.23              & 3.26              & 3.34              & 3.32              & 3.31              \\ \hline
75.0                         & 5.01              & 5.00              & 4.91              & 4.91              & 4.95              & 4.96              \\ \hline
90.0                         & \centering Did not move      & \centering Did not move      & \centering Did not move      & \centering Did not move      & \centering Did not move      & \centering Did not move      \tabularnewline
 \hline
\end{tabular}
\end{table}
\end{document}

相关内容