8

I'm writing a latex document and am using lots of examples of matrices where every entry is \ast. I'd like to write a macro that automatically makes such a matrix for me. So, this would look something like

\newcommand{\myAsteriskMatrix}[2]{magic}

where the two inputs would be the number of rows and the number of columns.

I happen to be using the nicematrix package, so a typical matrix looks something like this:

\documentclass{standalone}

\usepackage{nicematrix}

\begin{document}

$
\begin{bNiceMatrix}[r]
  \ast & \ast & \ast \\
  \ast & \ast & \ast
\end{bNiceMatrix}
$

\end{document}

This matrix could be generated by \myAsteriskMatrix{2}{3}, but I can't figure out how to write this macro! Any ideas?

2
  • What is the maximum dimension that you need? FYI, while code snippets are useful in explanations, it is always best to compose a fully compilable MWE including the \documentclass and the appropriate packages that sets up the problem. Commented Jan 14, 2021 at 6:01
  • @PeterGrill I didn't think a MWE would be useful here because my question is about constructing something rather than scrutinizing something. Nonetheless, I've restructured my example as a MWE. I'm not sure what my maximum dimension is. Ten or maybe twenty rows and columns? Commented Jan 14, 2021 at 6:06

5 Answers 5

14

with \pAutoNiceMatrix of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

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

Output of the above code

1
  • Oh wow. Didn't realize this was built-in... Thank you! Commented Jan 14, 2021 at 6:19
8

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

4

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
4

David Kastrup's \replicate-macros at

http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf

might be of interest to you.

In situations where you don't use the nicematrix-package with its \pAutoNiceMatrix you can define a variant of \replicate:

\MyReplicate{⟨repetitions⟩}%
            {⟨tokens to deliver in all repetitions but the last repetition⟩}%
            {⟨tokens to deliver in the last repetition⟩}%
\documentclass{standalone}

\makeatletter
\newcommand\MyReplicate[3]{%
  \ifnum#1>0 \expandafter\@firstofone\else\expandafter\@gobble\fi{%
    \ifnum#1>1 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
    {#2}{#3}\expandafter\MyReplicate\expandafter{\number\numexpr#1-1}{#2}{#3}%
  }%
}%
\makeatother

\newcommand\myAsteriskMatrix[2]{%
  \left[\begin{array}{*{#2}{r}}%
  \MyReplicate{#1}{\MyReplicate{#2}{\ast&}{\ast\\}}{\MyReplicate{#2}{\ast&}{\ast}}%
  \end{array}\right]%
}%


\begin{document}

$\myAsteriskMatrix{2}{3}$

\end{document}

enter image description here

3

If you are an Emacs user, you can create an elisp function to generate the latex code for a matrix of your choice; (you can do the same with any algorithmic language, but in emacs Elisp is embedded)

(defun matrice(type symb nrows ncolumns)
  "compose a LaTeX matrice type TYPE filling symbol SYMB
number of rows NROWS number of columns NCOLUMS "
  (concat
   (format "\\begin{%s}\n" type)
   (mapconcat (lambda (j)(concat
                          (mapconcat
                           (lambda(i) (format " %s " symb))
                           (number-sequence 1 ncolumns)"&")
                          "\\\\") )
              (number-sequence 1 nrows)"\n")
   (format "\n\\end{%s}\n" type)))

To insert a matrice just type (C-x C-e on a following line)

% (insert(matrice 'pmatrix '* 3 5))

\documentclass{article}
\usepackage{amsmath}

\begin{document}

% (insert(matrice 'pmatrix '* 3 5))

\[
\begin{pmatrix}
     * & * & * & * & * \\
     * & * & * & * & * \\
     * & * & * & * & * \\
\end{pmatrix}
\]

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .