1

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@{\qquad\qquad}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.

1

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

4
  • I see that you have typeset a $2$ in the exponent position within the @{} alignment marker. Shouldn't you have "^2"? – user74973 Jul 22 '20 at 17:25
  • Why is there an exclamation mark? Why can't I get more space between the columns by adding another \qquad or \qquad\quad? – user74973 Jul 22 '20 at 17:25
  • @user74973 please read the manual for the array package, all you ask for is explained there. – daleif Jul 22 '20 at 17:41
  • 2
    @user74973: !{\quad} adds 1 em to the normal intercolumn space which makes a totlal of 1em+10pt, whereas @{\quad} replaces the intercolumn space with a space of 1 em. – Bernard Jul 22 '20 at 18:25
1

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.

0

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 @{\qquad\qquad} 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}
8
  • @Bernard I have posted an answer in order to show a modification of my code. I didn't want to edit the original post because it would help others using a typical LaTeX editor. I am using an online editor which has limited features. I would appreciate your help on editing my code in "my answer." – user74973 Jul 22 '20 at 17:54
  • Can't you load the array package? It is very basic. – Bernard Jul 22 '20 at 18:15
  • No, the online platform does not allow the user to upload any packages. – user74973 Jul 22 '20 at 18:28
  • 1
    But maybe it is installed on the platform? After all, you can load amsmath. Why not array? – Bernard Jul 22 '20 at 19:25
  • I only included the preamble so that the code can be compiled by someone trying to help me. On this online platform, I only encode what I want to have printed. – user74973 Jul 23 '20 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.