答案1
这里有 3 个解决方案。第二个解决方案使用matrix*
来自 的环境mathtools
。它允许使用可选参数l,c
或l
。默认值为c
。最后一个解决方案依赖于alignat
,并可轻松控制列间距:
\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}
答案2
另外两个解决方案:
纯净的
align*
环境,以及array
自动插入=
符号的环境
请注意,array
基于的解决方案允许排版数字后符号=
采用右对齐模式,而不是左对齐模式。
\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}
答案3
这似乎有点过头了。;-)
请查看 GuITMeeting 2016(将于 10 月底举行)的会议记录,了解对该代码的评论。
\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}
当然,左边的表格也可以这样排版
\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}
但这并不好玩。;-)