LaTeX 表格中的自动对齐括号

LaTeX 表格中的自动对齐括号

我正在尝试创建一个包含坐标信息的表格,但是输出很混乱,因为有些坐标有分数和负号。有没有办法格式化它以获得更好的外观?

在此处输入图片描述

\documentclass{article}

\begin{document}

\centering
\begin{tabular}{cccc}
\hline
Test Sample & Output 1 & Output 2 & Classification \\ \hline
$(-\frac{14}{3},-\frac{8}{3})$ & +1              & -1              & 1              \\
$(-\frac{11}{3},\frac{1}{3})$ & +1              & -1              & 1              \\
$(-\frac{7}{3},-\frac{11}{3})$ & +1              & -1              & 1              \\
$(-\frac{4}{3},\frac{8}{3})$ & +1              & +1              & 3              \\
$(-\frac{2}{3},\frac{1}{3})$ & +1              & -1              & 1              \\
$(\frac{2}{3},-2)$ & +1              & -1              & 1              \\
$(\frac{4}{3},3)$ & +1              & +1              & 3              \\
$(\frac{7}{3},0)$ & +1              & -1              & 1              \\
$(4,\frac{13}{3})$ & +1              & +1              & 3              \\
$(5,1)$ & -1              & -               & 2              \\ \hline
\end{tabular}

\end{document}

答案1

编辑:
考虑的是@Mico 评论。从中建议使用appto={@{},@{}}哪个 unfirtunatela 无法按预期工作,由 `appto={,;} 模拟。Mico,非常感谢你!

使用带有库的tabularray包:booktabssiunitx

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

\begin{document}
    \begin{table}[ht]
    \sisetup{retain-explicit-plus}
\begin{tblr}{colspec = {     Q[r, mode=math] @{} Q[l, mode=math] %
                        *{2}{Q[c, si={table-format=-1.0}]}
                             Q[c, si={table-format= 1.0}]},
             cell{2-Z}{1} = {preto={\bigl(}, appto={,\;}},
             cell{2-Z}{2} = {appto={\bigr)}},
             row{1}  = {guard, mode=text},
             rowsep  = 3pt,
             }
    \toprule
\SetCell[c=2]{c}    Test Sample
                &               & Output 1  & Output 2  & Classification    \\ 
    \midrule
-\frac{14}{3}   & -\frac{8}{3}  & +1        & -1        & 1                 \\
-\frac{11}{3}   &  \frac{1}{3}  & +1        & -1        & 1                 \\
-\frac{7}{3}    & -\frac{11}{3} & +1        & -1        & 1                 \\
-\frac{4}{3}    &  \frac{8}{3}  & +1        & +1        & 3                 \\
-\frac{2}{3}    &  \frac{1}{3}  & +1        & -1        & 1                 \\
\frac{2}{3}     & -2            & +1        & -1        & 1                 \\
\frac{4}{3}     &  3            & +1        & +1        & 3                 \\
\frac{7}{3}     &  0            & +1        & -1        & 1                 \\
4               & \frac{13}{3}  & +1        & +1        & 3                 \\
5               &  1            & -1        & {{{--}}}  & 2                 \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

附录:
使用nccmath包中的中等大小分数和构造\left(,right.\left.,\right)来表示括号。由于它们,逗号周围的间距现在比以前更正确:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\usepackage{nccmath}

\begin{document}
    \begin{table}[ht]
    \sisetup{retain-explicit-plus}
\begin{tblr}{colspec = {     Q[r, mode=math] @{} Q[l, mode=math] %
                        *{2}{Q[c, si={table-format=-1.0}]}
                             Q[c, si={table-format= 1.0}]},
             cell{2-Z}{1} = {preto={\left(}, appto={\,,\right.}},
             cell{2-Z}{2} = {preto={\left.}, appto={\right)}},
             row{1}  = {guard, mode=text},
             }
    \toprule
\SetCell[c=2]{c}    Test Sample
                &               & Output 1  & Output 2  & Classification    \\ 
    \midrule
-\mfrac{14}{3}  & -\mfrac{8}{3} & +1        & -1        & 1                 \\
-\mfrac{11}{3}  &  \mfrac{1}{3} & +1        & -1        & 1                 \\
-\mfrac{7}{3}   & -\mfrac{11}{3}& +1        & -1        & 1                 \\
-\mfrac{4}{3}   &  \mfrac{8}{3} & +1        & +1        & 3                 \\
-\mfrac{2}{3}   &  \mfrac{1}{3} & +1        & -1        & 1                 \\
\mfrac{2}{3}    & -2\vphantom{\mfrac{1}{3}}            
                                & +1        & -1        & 1                 \\
\mfrac{4}{3}    &  3\vphantom{\mfrac{1}{3}}            
                                & +1        & +1        & 3                 \\
\mfrac{7}{3}    &  0\vphantom{\mfrac{1}{3}}            
                                & +1        & -1        & 1                 \\
4\vphantom{\mfrac{1}{3}}               
                & \mfrac{13}{3} & +1        & +1        & 3                 \\
5               &  1            & -1        & {{{--}}}  & 2                 \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

答案2

如果放大寄存器,输出就不会混乱\baselineskip。例如:

\def\crl{\crcr\noalign{\medskip\hrule\medskip}}
\vbox{\baselineskip=17pt \halign{&\ \hfil$#$\hfil\ \cr\crl
  \rm Test\ Sample         & \rm Output 1 & \rm Output 2 & \rm Classification \crl
  (-{14\over3},-{8\over3}) &    +1        &    -1        &    1  \cr
  (-{11\over3},{1\over3})  &    +1        &    -1        &    1  \cr
  (-{7\over3},-{11\over3}) &    +1        &    -1        &    1  \cr
  (-{4\over3}, {8\over3})  &    +1        &    +1        &    3  \cr
  (-{2\over3}, {1\over3})  &    +1        &    -1        &    1  \cr
  ( {2\over3},-2)          &    +1        &    -1        &    1  \cr
  ( {4\over3},3)           &    +1        &    +1        &    3  \cr
  ({7\over3},0)            &    +1        &    -1        &    1  \cr
  (4, {13\over3})          &    +1        &    +1        &    3  \cr
  (5,1)                    &    -1        &    -         &    2  \crl 
}}

诺梅西

答案3

由于表格的所有内容(标题行除外)都是数学内容,我建议您使用array环境而不是tabular环境。然后,将array环境括在\[\]指令中,以便在(显示)数学模式下水平居中材料。

我进一步建议您 (a) 使用内联分数符号而不是\frac符号和 (b) 使用包的宏booktabs而不是\hline

在此处输入图片描述

\documentclass{article}
\usepackage{array,booktabs}
\begin{document}

\[
\begin{array}{@{} r @{{},{}} l ccc @{}} % '@{{},{}}' forces ',' to have type 'mathpunct'
\toprule
\multicolumn{2}{@{}c}{\mbox{Test Sample}} & \mbox{Output 1} & \mbox{Output 2} & \mbox{Classification} \\ 
\midrule
(-14/3 & -8/3)           & +1 & -1 & 1  \\
(-11/3 & \phantom{+}1/3) & +1 & -1 & 1  \\
(-7/3  &-11/3)           & +1 & -1 & 1  \\
(-4/3  & \phantom{+}8/3) & +1 & +1 & 3  \\
(-2/3  & \phantom{+}1/3) & +1 & -1 & 1  \\
\addlinespace
(2/3   &   -2)           & +1 & -1 & 1  \\
(4/3   & \phantom{+}3)   & +1 & +1 & 3  \\
(7/3   & \phantom{+}0)   & +1 & -1 & 1  \\
(4     & 13/3)           & +1 & +1 & 3  \\
(5     & \phantom{+}1)   & -1 & -\phantom{1} & 2  \\ 
\bottomrule
\end{array}
\]

\end{document}

答案4

我会使用左对齐和内联分数。

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

\begin{document}

\begin{table}[htp]
\centering

\begin{tabular}{
  @{}
  >{\vphantom{$\Big|$}}l
  *{2}{S[table-format=-1.0,retain-explicit-plus]}
  S[table-format=1.0]
  @{}
}
\toprule
\multicolumn{1}{@{}l}{Test Sample}
  & {Output 1}
  & {Output 2}
  & {Classification} \\
\midrule
$(-14/3,-8/3)$ & +1 & -1 & 1 \\
$(-11/3,1/3)$  & +1 & -1 & 1 \\
$(-7/3,-11/3)$ & +1 & -1 & 1 \\
$(-4/3,8/3)$   & +1 & +1 & 3 \\
$(-2/3,1/3)$   & +1 & -1 & 1 \\
$(2/3,-2)$     & +1 & -1 & 1 \\
$(4/3,3)$      & +1 & +1 & 3 \\
$(7/3,0)$      & +1 & -1 & 1 \\
$(4,13/3)$     & +1 & +1 & 3 \\
$(5,1)$        & -1 & -1 & 2 \\
\bottomrule
\end{tabular}

\caption{Some descriptive caption}
\end{table}

\end{document}

在此处输入图片描述

相关内容