# Horizontal shift in tabular environment

My question is the most efficient way to do in tabular environment the last row of the table in the image below. Numbers are shifted horizontally by an half column. thankyou!

• Which image? ;-) Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Feb 7 '15 at 9:53

Here's a solution that uses the S column type (provided by the siunitx package) to assure that the numeric-data columns are all equally wide. I also suggesting using the \midrule macro (from the booktabs package), instead of \hline, to get well-spaced horizontal rules.

\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}

• although this mostly looks really nice, the negative items are not centered horizontally, but look like they're shoved too much to the the left. – barbara beeton Feb 7 '15 at 14:09

Here's a (complicated) version that ensures equal width of the columns and computes the differences

\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}


The main argument is used to compute the differences and also the number of necessary columns. Each item is added to the table, first measuring it so we can pass to tabular the width of the p columns.

• in this last answer, is it possible to have 2 improvement (for my pourpuses): – Christian Feb 10 '15 at 9:57
• @Christian What improvements? – egreg Feb 10 '15 at 10:01
• in this last answer, is it possible to have 2 improvement (for my aims): 1) first line in italic form 2) is it possible to have a second version of the table with the second and third differences? – Christian Feb 10 '15 at 10:04
• @Christian Yes, it would be possible; just use the same idea and from the first difference sequence compute the second difference and so on. About numbers in italics (in the first row): it's possible, but it would be wrong. If I find the time, I'll try it. – egreg Feb 10 '15 at 10:11
• do you have some news for me? – Christian Apr 8 '15 at 11:44