TransWikia.com

Alignment at equals signs in two columns of an array environment

TeX - LaTeX Asked by user74973 on June 13, 2021

I would like to make a table of two columns of the squares of all the digits. With the following code, there is no proper spacing between the equals sign and the numbers in the first column, and the equals sign is not typeset in the second column.

documentclass{amsart}
usepackage{amsmath}

begin{document}

$$
begin{array}{r@{${}={}$}lc@{qquadqquad}cr@{${}={}$}l}
0^2&0, & 5^2&25, 
1^2&1, & 6^2&36, 
2^2&4, & 7^2&49, 
3^2&9, & 8^2&64, 
4^2&16, & 9^2&81.
end{array}
$$

end{document}

I would appreciate tips on how to do this only using the array environment.

3 Answers

You have declared a fifth column in between the two groups. In my opinion, it's unnecessary. Here is a simpler code:

documentclass{amsart}
usepackage{amsmath}
usepackage{array}

begin{document}

[
begin{array}{r@{{}²={}}l!{qquad}r@{{}²={}}l}
0&0, & 5&25, 
1&1, & 6&36, 
2&4, & 7&49, 
3&9, & 8&64, 
4&16, &9&81.
end{array}
]

end{document} 

enter image description here

Answered by Bernard on June 13, 2021

I am using a platform that apparently does not allow all the options for an array environment. It may only allow l, r, c, and @{qquad}. I have modified my code so that it gives me exactly what I want.

documentclass{amsart}
usepackage{amsmath}

begin{document}

$$begin{array}{r@{}l @{qquadqquad} r@{}l}
0^2 &{}={} 0, & 5^2 &{}={} 25, 
1^2 &{}={} 1, & 6^2 &{}={} 36, 
2^2 &{}={} 4, & 7^2 &{}={} 49, 
3^2 &{}={} 9, & 8^2 &{}={} 64, 
4^2 &{}={} 16, & 9^2 &{}={} 81.
end{array}$$

end{document}

Answered by user74973 on June 13, 2021

You can ask TeX to do the computations itself in whatever radix you like (up to 36).

documentclass{article}
usepackage{xparse,array,booktabs}

ExplSyntaxOn

NewDocumentCommand{digitsquares}{sO{c}m}
 {% #1 * option for the header, #2 = alignment, #3 = radix
  IfBooleanTF { #1 }
   {
    tl_set:Nn l_tmpa_tl { multicolumn{6}{c}{mbox{Base ~ $#3$}}  midrule }
   }
   {
    tl_clear:N l_tmpa_tl
   }
  int_step_inline:nnn { 0 } { int_div_round:nn { #3 } { 2 } - 1 }
   {
    tl_put_right:Nx l_tmpa_tl
     {
      int_to_Base:nn { ##1 } { #3 } ^2 & = & int_to_Base:nn { (##1)*(##1) } { #3 } &
      int_compare:nT { ##1 + int_div_round:nn { #3 } { 2 } < #3 }
       {
        int_to_Base:nn { ##1 + int_div_round:nn { #3 } { 2 } } { #3 }^2 & = &
        int_to_Base:nn
         {
          (##1 + int_div_round:nn { #3 } { 2 })*
          (##1 + int_div_round:nn { #3 } { 2 })
         } { #3 }
        exp_not:N 
       }
     }
   }
  begin{array}[#2]{@{} r @{} >{{}}c<{{}} @{} l @{qquad} r @{} >{{}}c<{{}} @{} l @{}}
  l_tmpa_tl
  end{array}
 }

ExplSyntaxOff

begin{document}

[
digitsquares*[t]{10} qquad digitsquares*[t]{7}
]
[
digitsquares{16}
]

end{document}

enter image description here

The * means that we want the header; the optional argument is for passing the alignment option to array. The mandatory argument is the radix.

The array preamble consists of two sets

r @{} >{{}}c<{{}} @{} l

that is: right aligned column, no space, a centered column with {} on either side of the item (it will be the = sign), left aligned column; in between a qquad separation. The leading and trailing @{} nullify the padding on either side of the array.

Answered by egreg on June 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP