在 \align{} 环境中对齐两个列表

在 \align{} 环境中对齐两个列表

我想对齐以下两个列表

$$
\begin{align}
\text{list_of_primes} &= [2 3 5 7 11 13 17 19]\\
\text{list_of_powers} &= [4 2 1 1  1  1  1  1]\\
\end{align}
$$

通过以下方式:

在此处输入图片描述

我怎样才能实现这个目标?

答案1

您可以使用表格环境来实现这一目标。

另外:使用\[\]显示数学模式,如下所述在这个答案中. 其次,我们必须用反斜杠转义下划线:list\_of\_primes

\documentclass{article}
\usepackage{amsmath}
\begin{document}
  \[
    % There are 9 columns. The first one left aligned, and the rest right aligned
    % so we need one l and 8 r as the first argument to tabular:
    \begin{tabular}{lrrrrrrrr}
        \text{list\_of\_primes} & = [2 & 3 & 5 & 7 & 11 & 13 & 17 & 19] \\
        \text{list\_of\_powers} & = [4 & 2 & 1 & 1 & 1  & 1  & 1  & 1]
    \end{tabular}
  \]
\end{document}

输出是 对齐的数字列表 我相信它看起来像您提供的图像。

答案2

您可以使用array

\documentclass{article}
\usepackage{amsmath,array}

\begin{document}

\[
\begin{array}{
  @{}       % no padding
  >{$}l<{$} % first column in text mode (left aligned)
  @{}       % no padding
  >{{}}l    % for = [
  @{}       % no padding
  *{8}{r}   % the primes and the exponents
  @{}       % no padding
  l         % closing bracket
  @{}       % no padding
}
list\_of\_primes & = [ & 2 & 3 & 5 & 7 & 11 & 13 & 17 & 19 & ] \\
list\_of\_powers & = [ & 4 & 2 & 1 & 1 &  1 &  1 &  1 &  1 & ]
\end{array}
\]

\end{document}

在此处输入图片描述

抽象一下这个想法:列表以“分解形式”给出,其中素数因子用空格分隔。A*版本提供“内联”形式。

\documentclass{article}
\usepackage{amsmath,array,xparse}

\ExplSyntaxOn
\NewDocumentCommand{\showfactorization}{sm}
 {
  \IfBooleanTF{#1}
   { \bru_show_factorization_inline:n { #2 } }
   { \bru_show_factorization_display:n { #2 } }
 }

\tl_new:N \l__bru_show_factorization_top_tl
\tl_new:N \l__bru_show_factorization_bot_tl
\seq_new:N \l__bru_show_factorization_seq

\cs_new_protected:Nn \bru_show_factorization_inline:n
 {
  \seq_set_split:Nnn \l__bru_show_factorization_seq { ~ } { #1 }
  \ensuremath
   {
    \seq_use:Nn \l__bru_show_factorization_seq { \cdot }
   }
 }

\cs_new_protected:Nn \bru_show_factorization_display:n
 {
  \tl_set:Nn \l__bru_show_factorization_top_tl { list \_ of \_ primes & = [ }
  \tl_set:Nn \l__bru_show_factorization_bot_tl { list \_ of \_ powers & = [ }
  \seq_set_split:Nnn \l__bru_show_factorization_seq { ~ } { #1 }
  \seq_map_inline:Nn \l__bru_show_factorization_seq
   {
    \__bru_show_factorization_split:w ##1 \q_stop
   }
  \begin{array}
   {
    @{}       % no padding
    >{$}l<{$} % first column in text mode (left aligned)
    @{}       % no padding
    >{{}}l    % for = [
    @{}       % no padding
    *{\seq_count:N \l__bru_show_factorization_seq}{r} % the primes and the exponents
    @{}       % no padding
    l         % closing bracket
    @{}       % no padding
   }
  \tl_use:N \l__bru_show_factorization_top_tl & ] \\
  \tl_use:N \l__bru_show_factorization_bot_tl & ]
  \end{array}
 }

\cs_new_protected:Npn \__bru_show_factorization_split:w #1 ^ #2 \q_stop
 {
  \tl_put_right:Nn \l__bru_show_factorization_top_tl { & #1 }
  \tl_put_right:Nn \l__bru_show_factorization_bot_tl { & #2 }
 }
\ExplSyntaxOff

\begin{document}

\showfactorization*{2^4 3^2 5^1 7^1 11^1 13^1 17^1 19^1}
\[
\showfactorization{2^4 3^2 5^1 7^1 11^1 13^1 17^1 19^1}
\]

\end{document}

在此处输入图片描述

相关内容