表格环境中的水平移动

表格环境中的水平移动

我的问题是在表格环境中最有效地处理下图中表格的最后一行。数字水平移动半列。谢谢! 在此处输入图片描述

答案1

这是一个使用S列类型(由包提供siunitx)来确保数字数据列都等宽的解决方案。我还建议使用宏\midrule(来自booktabs包)而不是\hline,以获得间距合适的水平规则。

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs} % for \midrule macro
\usepackage{siunitx}  % for 'S' column type
\begin{document}

\begin{table}
\centering
\sisetup{table-format=-1.0}
\setlength\arraycolsep{2pt} % default value: 5pt
$\begin{array}{l @{\quad} *{19}{S} }
K & 0 && 1 && 2 && 3 && 4 && 5 && 6 && 7 && 8 && 9\\
\midrule
y_K & 2 && 3 && 5 && 8 && 9 && 9 && 8 && 7 && 7 && 6\\
\midrule
\Delta y_K && 1 && 2 && 3 && 1 && 0 && -1 && -1 && 0 && -1\\
\end{array}$
\end{table}
\end{document}

答案2

这是一个(复杂的)版本,可确保列宽相等并计算差异

\documentclass{article}

\usepackage{xparse,booktabs,array}

\ExplSyntaxOn

\NewDocumentCommand{\difftab}{mmm}
 {% #1=variable name, #2=index name, #3=values
  \christian_diff_tab:nnn { #1 } { #2 } { #3 }
 }

\int_new:N \l_christian_diff_cols_int
\tl_new:N \l_christian_diff_body_tl
\tl_new:N \l__christian_diff_tmp_tl
\seq_new:N \l_christian_diff_data_seq
\seq_new:N \l_christian_diff_diff_seq
\dim_new:N \l__christian_diff_width_dim

\cs_new_protected:Npn \christian_diff_tab:nnn #1 #2 #3
 {
  \dim_zero:N \l__christian_diff_width_dim
  \seq_set_from_clist:Nn \l_christian_diff_data_seq { #3 }
  \seq_map_inline:Nn \l_christian_diff_data_seq
   {
    \tl_set:Nn \l__christian_diff_tmp_tl { ##1 }
    \__christian_diff_measure:
   }
  \seq_clear:N \l_christian_diff_diff_seq
  \int_set:Nn \l_christian_diff_cols_int { \seq_count:N \l_christian_diff_data_seq }
  \tl_clear:N \l_christian_diff_body_tl
  \tl_put_right:Nn \l_christian_diff_body_tl { $#2$ }
  \int_step_inline:nnnn { 1 } { 1 } { \l_christian_diff_cols_int }
   {
    \int_compare:nTF { ##1 > 1 }
     {
      \tl_set:Nx \l__christian_diff_tmp_tl { \int_to_arabic:n { ##1 - 1 } }
      \__christian_diff_measure:
      \tl_put_right:Nx \l_christian_diff_body_tl
       { && \l__christian_diff_tmp_tl }
      %% compute the differences
      \tl_set:Nx \l__christian_diff_tmp_tl
       {
        \int_to_arabic:n 
         {
          \seq_item:Nn \l_christian_diff_data_seq { ##1 }
          -
          \seq_item:Nn \l_christian_diff_data_seq { ##1 - 1 }
         }
       }
      \__christian_diff_measure:
      \seq_put_right:NV \l_christian_diff_diff_seq \l__christian_diff_tmp_tl
     }
     {
      \tl_set:Nx \l__christian_diff_tmp_tl { \int_to_arabic:n { ##1 - 1 } }
      \__christian_diff_measure:
      \tl_put_right:Nx \l_christian_diff_body_tl 
       { & \l__christian_diff_tmp_tl }
     }
   }
  \tl_put_right:Nn \l_christian_diff_body_tl { \tabularnewline\midrule $#1\sb{#2}$ & }
  \tl_put_right:Nx \l_christian_diff_body_tl { \seq_use:Nn \l_christian_diff_data_seq { && } }
  \tl_put_right:Nn \l_christian_diff_body_tl { \tabularnewline\midrule $\Delta #1\sb{#2}$ && }
  \tl_put_right:Nx \l_christian_diff_body_tl { \seq_use:Nn \l_christian_diff_diff_seq { && } }
\tl_show:N \l_christian_diff_body_tl
  %%% make the table
  \use:x
   {
    \exp_not:N \begin {tabular}
     {
      @{}c
      * { \int_to_arabic:n { 1 + 2 * \l_christian_diff_cols_int } }
        { >{\exp_not:N\centering$} p{ \l__christian_diff_width_dim } <{$} @{} }
     }
   }
   \toprule
   \tl_use:N \l_christian_diff_body_tl \tabularnewline
   \bottomrule
  \end{tabular}
 }

\cs_new_protected:Npn \__christian_diff_measure:
 {
  \hbox_set:Nn \l_tmpa_box { $ \l__christian_diff_tmp_tl $ }
  \dim_compare:nT { \box_wd:N \l_tmpa_box > \l__christian_diff_width_dim }
   {
    \dim_set:Nn \l__christian_diff_width_dim { \box_wd:N \l_tmpa_box }
   }
 }
\cs_new_protected:Npn \christian_diff_cell:n #1
 {
  \makebox[\l__christian_diff_width_dim]{ $#1$ }
 }

\ExplSyntaxOff

\begin{document}
\[
\difftab{y}{k}{2, 3, 5, 8, 9, 9, 8, 7, 7, 6}
\]
\[
\difftab{a}{i}{0,1,1,2,3,5,8,13,21}
\]
\end{document}

在此处输入图片描述

主要参数用于计算差异以及所需列数。每项都添加到表中,首先测量它,以便我们可以传递tabular列宽p

相关内容