表格不允许数学模式和对齐问题

表格不允许数学模式和对齐问题

我正在编写表格,遇到了两个问题。第一个问题是我无法在表格中输入数学表达式。我不确定如何调整代码以启用数学模式。第二个问题是我无法垂直对齐列(尽管第二列已按预期对齐)。

MWE 示例如下

\documentclass{report}
\usepackage[english]{babel}
\usepackage{amsfonts}
%%% TABLE %%%
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{siunitx}
%%% DASHED LINES IN TABLE %%%
\usepackage{array}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}

\begin{document}
\centering
\begin{tabular}{SSSSSSSS} \toprule
    \textbf{Example} & \textbf{Example} & \textbf{Example} & \textbf{Example}  \\ \midrule
    $D_{D}$  & \text{This is an example of a rather long sentence} & 2 & 3 \\ \hdashline
    1  & \text{This is an example of a rather long sentence} & 2 & 3 \\ \hdashline
    1  & \text{This is an example of a rather long sentence} & 2 & 3 \\ \hdashline
    1  & \text{This is a short sentence} & 2 & 3  \\ \bottomrule
\end{tabular}
\end{document}

MWE 的输出如下所示

在此处输入图片描述

答案1

在此处输入图片描述

(红线表示页面布局)

  • 你定义了 7 列,但只使用了 4 列
  • 列中的文本S必须用花括号括起来
  • 第二列仅包含文本,因此这里最好使用非S列类型,例如l或在文本非常长的情况下,这可能导致表格溢出文本边框,应使用p{<with>}X(实际上L从派生X)列类型自动分成两行或更多行
\documentclass{report}
\usepackage[english]{babel}
\usepackage{amssymb}%{amsfonts}
%%% TABLE %%%
\usepackage{ragged2e}    % <--- new
\usepackage{booktabs, tabularx}
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X}
\usepackage{siunitx}
%%% DASHED LINES IN TABLE %%%
%\usepackage{array}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}

\begin{document}
    \begin{table}[ht]
    \sisetup{table-format=1} % you need accommodate to used (decimal) numbers
    \centering
\begin{tabularx}{\linewidth}{@{} SL SS @{}} 
    \toprule
{\textbf{Example}}  
    &   \textbf{Example} 
                &   {\textbf{Example}}
                    &   {\textbf{Example}}              \\
    \midrule
{$D_{D}$}           &   This is an example of a rather long sentence 
                        & 2 & 3                         \\ \hdashline
    1  &  This is an example of a rather very long sentence in two lines
                        & 2 & 3                         \\ \hdashline
    1  &  This is an example of a rather long sentence
                        & 2 & 3                         \\ \hdashline
    1  &  This is a short sentence
                        & 2 & 3                         \\ 
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}

笔记:我不会使用虚线水平线。相反,您可以插入一些垂直空间,例如通过使用包\addlinespace中定义的booktabs

附录: 如果您希望所有单元格内容都显示在一行中并左对齐,那么您需要使用l列类型,但从这样的代码结果可以看出,它会非常丑陋并且溢出文本区域:

\documentclass{report}
\usepackage[english]{babel}
\usepackage{amssymb}%{amsfonts}
%%% TABLE %%%
\usepackage{booktabs, tabularx}
%%% DASHED LINES IN TABLE %%%
%\usepackage{array}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}

\begin{document}
    \begin{table}[ht]
    \centering
\begin{tabular}{ll ll} 
    \toprule
{\textbf{Example}}  
    &   \textbf{Example} 
                &   {\textbf{Example}}
                    &   {\textbf{Example}}              \\
    \midrule
{$D_{D}$}           &   This is an example of a rather long sentence 
                        & 2 & 3                         \\ \hdashline
    1  &  This is an example of a rather very long sentence in two lines
                        & 2 & 3                         \\ \hdashline
    1  &  This is an example of a rather long sentence
                        & 2 & 3                         \\ \hdashline
    1  &  This is a short sentence
                        & 2 & 3                         \\ 
    \bottomrule
\end{tabular}
    \end{table}
\end{document}

在此处输入图片描述

(红线表示页面布局)

相关内容