How can I fill an arbitrarily sized matrix with asterisks?

with \pAutoNiceMatrix of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
$\pAutoNiceMatrix{7-7}{*}$
\end{document}

Output of the above code


Without nicematrix. You may need to add \usepackage{xparse} if you're running LaTeX prior to the 2020-10-01 release.

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\automatrix}{O{*}mmm}
 {% #1 = symbol (default *), #2 = delimiter, #3 = rows, #4 = columns
  \int_compare:nT { #4 > \value{MaxMatrixCols} } { \setcounter{MaxMatrixCols}{#4} }
  \begin{#2matrix}
  \prg_replicate:nn { #3 }
   {
    {#1} \prg_replicate:nn { #4 - 1 } { & #1 } \\
   }
  \end{#2matrix}
 }

\ExplSyntaxOff

\begin{document}

\[
\automatrix{p}{2}{3}
\ne
\automatrix[+]{b}{3}{2}
\]

\end{document}

enter image description here


In OpTeX, we can define:

\def\repmatrix#1x#2 #3{
   \left(\vcenter{
      \table{#2c}{\fornum 1..#1 \do {\fornum 1..#2-1 \do{#3&}#3\cr}}}
   \right)
}

$$
  \repmatrix 2x3 {*} \not= \repmatrix 3x5 {*} \not= \repmatrix 2x2 {$Z$}
$$

\bye

Tags:

Loops

Macros