1

I would like to align the following two lists

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

In the following way:

enter image description here

How could I achieve that?

bru1987
  • 903

2 Answers2

4

You could use the tabular environment to achieve this.

Also: use \[ and \] for display math mode, as explained in this answer. Second, we have to escape the underscores with a backslash: 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}

And the output is Aligned list of numbers Which I believe looks like your provided image.

3

You can use 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}

enter image description here

Abstracting the idea: the list is given in “factored form”, with prime factors separated by spaces. A *-version provides the “in line” form.

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

enter image description here

egreg
  • 1,121,712