12

I am trying to create a list like below, enter image description here

but I can't seem to to align the three columns of equations and when I do I get something like

enter image description here

Here is my code:

$\begin{array}{ccc}
x_8 = 93 \quad y_8 = 64 \quad z_8 = 61\\
x_7 = 186 \quad y_7 = 32 \quad z_7 = 61\\
x_6 = 231 \quad y_6 = 32 \quad z_6 = 29
\end{array}$

Added image:

enter image description here

How would I align the two tables like that?

  • align(*) supports multiple alignment points. Here is the syntax for the first line: x_8 &=93 & y_8 &= 64 & z_8=61 \\, then just repeat (of course assumes the amsmath package) – daleif Sep 02 '16 at 15:04
  • @daleif How do I manage the spacing? What if I want the spacing to look like as in the first picture I have? – user19405892 Sep 02 '16 at 15:06
  • Then lookup alignat though it has a slightly different syntax – daleif Sep 02 '16 at 17:25

3 Answers3

11

Here are 3 solutions. The second solution uses the matrix* environment from mathtools. It allows for an optional argument l,c or l. The default is c. The last solution relies on alignat, and gives an easy control on the spacing of the columns:

\documentclass{article}
\usepackage{mathtools}

\begin{document}
\[ \begin{array}{lll}%
x_8 = 93  &  y_8 = 64  &  z_8 = 61\\
x_7 = 186  &  y_7 = 32 &  z_7 = 61\\
x_6 = 231  &  y_6 = 32  &  z_6 = 29
\end{array}\]%

\[ \begin{matrix*}[l]%
x_8 = 93  &  y_8 = 64  &  z_8 = 61\\
x_7 = 186  &  y_7 = 32 &  z_7 = 61\\
x_6 = 231  &  y_6 = 32  &  z_6 = 29
\end{matrix*}\]%

\begin{alignat*}{3}
x_8  & = 93  &\qquad  y_8 &  = 64  &\qquad  z_8  & = 61\\
x_7  & = 186  &  y_7 & = 32 &  z_7  & = 61\\
x_6  & = 231  &  y_6  & = 32  &  z_6  & = 29
\end{alignat*}

\end{document} 

enter image description here

Bernard
  • 271,350
10

Two additional solutions:

  • a pure align* environment, and

  • an array environment with automatic insertion of the = symbols

Note that the array-based solution permits typesetting the numbers after the = symbols in right-aligned rather than in left-aligned mode.

enter image description here

\documentclass{article}
\usepackage{amsmath} % for 'align*' environment
\usepackage{array}   % for '\newcolumntype' directive
\newcolumntype{R}{r@{{}={}}r} % for the array-based solution
\begin{document}

\begin{align*}
x_8 &= 93  & y_8 &= 64 & z_8 &= 61\\
x_7 &= 186 & y_7 &= 32 & z_7 &= 61\\
x_6 &= 231 & y_6 &= 32 & z_6 &= 29
\end{align*}

\[
\renewcommand\arraystretch{1.33}
\begin{array}{ R @{\qquad} R @{\qquad} R }
x_8 &  93 & y_8 & 64 & z_8 & 61\\
x_7 & 186 & y_7 & 32 & z_7 & 61\\
x_6 & 231 & y_6 & 32 & z_6 & 29
\end{array}
\]
\end{document}
Mico
  • 506,678
8

This might seem overkill. ;-) Look at the (forthcoming) proceedings of the GuITMeeting 2016 (to be held at the end of October) for comments over the code.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\providecommand\fpeval{\fp_eval:n}

\NewDocumentCommand{\nforeach}{ m +m }
 {
  \tl_clear:N \l__manual_nforeach_type_tl
  \keys_set:nn { manual/nforeach }
   {
    type=integers,start = 1, step = 1, end = 0,
   }
  \keys_set:nn { manual/nforeach } { #1 }
  \__manual_nforeach_exec:n { #2 }
 }

\int_new:N \g__manual_foreach_map_int
\int_new:N \g__manual_fp_map_int
\tl_new:N \l__manual_nforeach_type_tl

\keys_define:nn { manual/nforeach }
 {
  type .choice:,
  type .value_required:n = true,
  type/integers .code:n = \tl_set:Nn \l__manual_nforeach_type_tl { integers },
  type/fp       .code:n = \tl_set:Nn \l__manual_nforeach_type_tl { fp },
  type/alph     .code:n = \tl_set:Nn \l__manual_nforeach_type_tl { alph },
  type/Alph     .code:n = \tl_set:Nn \l__manual_nforeach_type_tl { Alph },
  start .tl_set:N = \l__manual_nforeach_start_tl,
  step  .tl_set:N = \l__manual_nforeach_step_tl,
  end   .tl_set:N = \l__manual_nforeach_end_tl,
 }

\cs_new_protected:Nn \__manual_nforeach_exec:n
 {
  \int_gincr:N \g__manual_foreach_map_int
  \str_case:Vn \l__manual_nforeach_type_tl
   {
    {integers}{\__manual_nforeach_exec_integers:n { #1 }}
    {fp}      {\__manual_nforeach_exec_fp:n { #1 }}
    {alph}    {\__manual_nforeach_exec_alph:Nn \int_to_alph:n { #1 }}
    {Alph}    {\__manual_nforeach_exec_alph:Nn \int_to_Alph:n { #1 }}
   }
  \int_gdecr:N \g__manual_foreach_map_int
 }
\cs_generate_variant:Nn \str_case:nn { V }

\cs_new_protected:Nn \__manual_nforeach_exec_integers:n
 {
  \int_step_inline:nnnn
   { \l__manual_nforeach_start_tl }
   { \l__manual_nforeach_step_tl }
   { \l__manual_nforeach_end_tl }
   { #1 }
 }
\cs_new_protected:Nn \__manual_nforeach_exec_alph:Nn
 {
  \cs_set:cn { __manual_nforeach_alph_ \int_use:N \g__manual_foreach_map_int :n } { #2 }
  \cs_generate_variant:cn
   { __manual_nforeach_alph_ \int_use:N \g__manual_foreach_map_int :n }
   { f }
  \int_step_inline:nnnn
   { \int_from_alph:f { \l__manual_nforeach_start_tl } }
   { \l__manual_nforeach_step_tl }
   { \int_from_alph:f { \l__manual_nforeach_end_tl } }
   {
    \use:c { __manual_nforeach_alph_ \int_use:N \g__manual_foreach_map_int :f }
     { #1 { ##1 } }
   }
 }
\cs_generate_variant:Nn \cs_generate_variant:Nn { c }
\cs_generate_variant:Nn \int_from_alph:n { f }

\cs_new_protected:Nn \__manual_nforeach_exec_fp:n
 {
  \manual_fp_step_inline:nnnn
   { \l__manual_nforeach_start_tl }
   { \l__manual_nforeach_step_tl }
   { \l__manual_nforeach_end_tl }
   { #1 }
 }

% a replacement for \fp_step_inline:nnnn
\seq_new:N \l__manual_fp_step_seq
\fp_new:N \l__manual_fp_step_start_fp

\cs_new_protected:Nn \manual_fp_step_inline:nnnn
 {
  \int_gincr:N \g__manual_fp_map_int
  \seq_clear_new:c { l__manual_fp_step_ \int_use:N \g__manual_fp_map_int _seq }
  \fp_compare:nTF { #2 < \c_zero_fp }
   {
    \__manual_fp_step_make_neg:nnn { #1 } { #2 } { #3 }
   }
   {
    \__manual_fp_step_make_pos:nnn { #1 } { #2 } { #3 }
   }
  \seq_map_inline:cn { l__manual_fp_step_ \int_use:N \g__manual_fp_map_int _seq } { #4 }
  \int_gdecr:N \g__manual_fp_map_int
 }
\cs_new_protected:Nn \__manual_fp_step_make_neg:nnn
 {
  \fp_set:Nn \l__manual_fp_step_start_fp { #1 }
  \fp_do_while:nn { \l__manual_fp_step_start_fp >= #3 }
   {
    \seq_put_right:cx { l__manual_fp_step_ \int_use:N \g__manual_fp_map_int _seq }
     { \fp_eval:n { \l__manual_fp_step_start_fp } }
    \fp_add:Nn \l__manual_fp_step_start_fp { #2 }
   }
 }
\cs_new_protected:Nn \__manual_fp_step_make_pos:nnn
 {
  \fp_set:Nn \l__manual_fp_step_start_fp { #1 }
  \fp_do_while:nn { \l__manual_fp_step_start_fp <= #3 }
   {
    \seq_put_right:cx { l__manual_fp_step_ \int_use:N \g__manual_fp_map_int _seq }
     { \fp_eval:n { \l__manual_fp_step_start_fp } }
    \fp_add:Nn \l__manual_fp_step_start_fp { #2 }
   }
 }

\NewDocumentCommand{\lforeach}{ s O{} m +m }
 {
  \IfBooleanTF{#1}
   {
    \manual_lforeach:non { #2 } { #3 } { #4 }
   }
   {
    \manual_lforeach:nnn { #2 } { #3 } { #4 }
   }
 }

\cs_new_protected:Nn \manual_lforeach:nnn
 {
  \keys_set:nn { manual/lforeach } { single }
  \keys_set:nn { manual/lforeach } { #1 }
  \clist_set:Nn \l__manual_lforeach_list_clist { #2 }
  \int_gincr:N \g__manual_foreach_map_int
  \__manual_lforeach_define:n { #3 }
  \clist_map_inline:Nn \l__manual_lforeach_list_clist
   {
    \use:c { __manual_lforeach_ \int_use:N \g__manual_foreach_map_int _action:w } ##1 \q_stop
   }
  \int_gdecr:N \g__manual_foreach_map_int
 }
\cs_generate_variant:Nn \manual_lforeach:nnn { no }

\cs_new_protected:Nn \__manual_lforeach_define:n
 {
  \exp_last_unbraced:NcV
   \cs_set:Npn
   { __manual_lforeach_ \int_use:N \g__manual_foreach_map_int _action:w }
   \l__manual_lforeach_format_tl
   \q_stop
   {#1}
 }

\keys_define:nn { manual/lforeach }
 {
  format .tl_set:N = \l__manual_lforeach_format_tl,
  single .code:n = \tl_set:Nn \l__manual_lforeach_format_tl { ##1 },
  double .code:n = \tl_set:Nn \l__manual_lforeach_format_tl { ##1/##2 },
  triple .code:n = \tl_set:Nn \l__manual_lforeach_format_tl { ##1/##2/##3 },
 }

%%% for this application
\NewDocumentCommand{\newlist}{m}
 {
  \seq_clear_new:c { l_manual_list_#1_seq }
 }
\NewDocumentCommand{\addtolist}{mm}
 {
  \seq_put_right:cn { l_manual_list_#1_seq } { #2 }
 }
\NewDocumentCommand{\uselist}{mm}
 {
  \seq_use:cn { l_manual_list_#1_seq } { #2 }
 }
\NewDocumentCommand{\showlist}{m}
 {
  \seq_show:c { l_manual_list_#1_seq }
 }
\ExplSyntaxOff

\newlist{listA}\newlist{listB}

\begin{document}

\[
% first table
\lforeach[format=#1/#2/#3/#4,]{
  8/93/64/61,
  7/186/32/61,
  6/231/32/29,
  5/462/16/29,
  4/483/16/13,
  3/966/8/13,
  2/975/8/5,
  1/1950/4/5,
  0/1953/4/1,
}{\addtolist{listA}{x_{#1}=#2 & y_{#1}=#3 & z_{#1}=#4 \\}}
% second table
\nforeach{start=5,step=-1,end=0}{%
  \addtolist{listB}{
    x_{#1}=\fpeval{65*2^(5-#1)} &
    y_{#1}=\fpeval{2^(#1+2)}  &
    z_{#1}=1 \\
  }%
}
\begin{array}[t]{lll}
\uselist{listA}{}
\end{array}
\qquad
\begin{array}[t]{lll}
\uselist{listB}{}
\end{array}
\]

\end{document}

enter image description here


Of course, the left-hand table can also be typeset by

\documentclass{article}

\begin{document}

\[
\def\row#1/#2/#3/#4,{%
  x_{#1}=#2 & y_{#1}=#3 & z_{#1}=#4 \\
}
\begin{array}{lll}
\row 8/93/64/61,
\row 7/186/32/61,
\row 6/231/32/29,
\row 5/462/16/29,
\row 4/483/16/13,
\row 3/966/8/13,
\row 2/975/8/5,
\row 1/1950/4/5,
\row 0/1953/4/1,
\end{array}
\]

\end{document}

but it's no fun. ;-)

egreg
  • 1,121,712