1

As titled, I want the command\mqty(\xmat*{M}{n}{m}) to generate a matrix like this:

$$
    \begin{pmatrix}
        M_{ 11 }  & M_{ 12 }  & M_{ 13 }  & \cdots & M_{ 1m }  \\
        M_{ 21 }  & M_{ 22 }  & M_{ 23 }  & \cdots & M_{ 2m }  \\
        M_{ 31 }  & M_{ 32 }  & M_{ 33 }  & \cdots & M_{ 3m }  \\
        \vdots & \vdots & \vdots & \ddots & \vdots \\
        M_{ n1 }  & M_{ n2 }  & M_{ n3 }  & \cdots & M_{ nm }  \\
    \end{pmatrix} 
$$

Also, I want this command to be applicable to finite column or row vectors (if this can also be used for matrices/vectors with infinite order would be even better) as well. (e.g. \xmat*{v}{n}{1}, \xmat*{M}{5}{n}) Do I have to create a new command for every document or is there other ways to achieve this? Thank you

  • Welcome to TeX.SE. – Mico Apr 23 at 7:35
  • Note that $$ should not be used. – user156344 Apr 23 at 11:27
  • Don't use the physics package. Its code quality is very poor. – Henri Menke Apr 24 at 2:32
0

Here's an implementation using expl3.

The arguments to \xmat are

  1. * (to denote infinite matrices)
  2. the type of matrix delimiters (optional, default p)
  3. the letter for the entries
  4. the number of rows (can be symbolic or an explicit integer)
  5. the number of explicitly shown rows (optional, default 3)
  6. the number of columns (can be symbolic or an explicit integer)
  7. the number of explicitly shown columns (optional, default 3)
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn

% #1 = star
% #2 = letter for the matrix delimiters (p,b,B,v,V), default = p
% #3 = letter for the entries
% #4 = number of rows
% #5 = number of explicit rows, default = 3
% #6 = number of columns
% #7 = number of explicit columns, default = 3
\NewDocumentCommand{\xmat}{sO{p}mmO{3}mO{3}}
 {
  \IfBooleanTF { #1 }
   {% with \xmat* we want an infinite matrix
    \bool_set_true:N \l__wang_xmat_infinite_bool
   }
   {
    \bool_set_false:N \l__wang_xmat_infinite_bool
   }
  \wang_xmat:nnnnnn { #2 } { #3 } { #4 } { #5 } { #6 } { #7 }
  %                    t      l      r     mr      c     mc
 }

\bool_new:N \l__wang_xmat_infinite_bool
\bool_new:N \l__wang_xmat_dotrow_bool
\bool_new:N \l__wang_xmat_dotcol_bool
\tl_new:N \l__wang_xmat_body_tl
\int_new:N \l__wang_xmat_exrows_int
\int_new:N \l__wang_xmat_excols_int

\cs_new_protected:Nn \wang_xmat:nnnnnn
 {
  % clear the variable containing the body of the matrix
  \tl_clear:N \l__wang_xmat_body_tl
  % set the tentative number of explicit rows
  \int_set:Nn \l__wang_xmat_exrows_int { #4 }
  \regex_match:nnTF { \A [[:digit:]]* \Z } { #3 }
   {% number of rows is an integer
    \int_compare:nTF { #3 <= #4 }
     {% if #3 <= #4 we don't want a row of dots
      \bool_set_false:N \l__wang_xmat_dotrow_bool
      \int_set:Nn \l__wang_xmat_exrows_int { #3 }
     }
     {% we want a row of dots
      \bool_set_true:N \l__wang_xmat_dotrow_bool
     }
   }
   {% number of rows is symbolic, we want a row of dots
    \bool_set_true:N \l__wang_xmat_dotrow_bool
   }
  % set the tentative number of explicit columns
  \int_set:Nn \l__wang_xmat_excols_int { #6 }
  \regex_match:nnTF { \A [[:digit:]]* \Z } { #5 }
   {% number of cols is an integer
    \int_compare:nTF { #5 <= #6 }
     {% if #5 <= #6 we don't want a column of dots
      \bool_set_false:N \l__wang_xmat_dotcol_bool
      \int_set:Nn \l__wang_xmat_excols_int { #5 }
     }
     {% we want a column of dots
      \bool_set_true:N \l__wang_xmat_dotcol_bool
     }
   }
   {% number of columns is symbolic, we want a column of dots
    \bool_set_true:N \l__wang_xmat_dotcol_bool
   }
  % loop through the rows
  \int_step_inline:nn { \l__wang_xmat_exrows_int }
   {
    % add the first entry in the row
    \tl_put_right:Nn \l__wang_xmat_body_tl { #2\sb{##1 1} }
    % add the further entries in the explicit columns
    \int_step_inline:nnn { 2 } { \l__wang_xmat_excols_int }
     {
      \tl_put_right:Nn \l__wang_xmat_body_tl { & #2\sb{##1 ####1} }
     }
    % if we have a column of dots, add \cdots and the last entry
    \bool_if:NT \l__wang_xmat_dotcol_bool
     {
      \tl_put_right:Nn \l__wang_xmat_body_tl { & \cdots & #2\sb{##1 #5} }
     }
    % infinite matrix, add \cdots
    \bool_if:NT \l__wang_xmat_infinite_bool
     {
      \tl_put_right:Nn \l__wang_xmat_body_tl { & \cdots }
     }
    % finish up the row
    \tl_put_right:Nn \l__wang_xmat_body_tl { \\ }
   }
   % finish up the rows
   \bool_if:NT \l__wang_xmat_dotrow_bool
    {
     % if we have a row of dots, fill it in
     \tl_put_right:Nn \l__wang_xmat_body_tl { \vdots }
     \prg_replicate:nn { \l__wang_xmat_excols_int - 1 }
      {
       \tl_put_right:Nn \l__wang_xmat_body_tl { & \vdots }
      }
     \bool_if:NT \l__wang_xmat_dotcol_bool
      {
       \tl_put_right:Nn \l__wang_xmat_body_tl { & \ddots & \vdots }
      }
     \tl_put_right:Nn \l__wang_xmat_body_tl { \\ }
     % fill the last row
     \tl_put_right:Nn \l__wang_xmat_body_tl { #2\sb{#3 1} }
     \int_step_inline:nnn { 2 } { \l__wang_xmat_excols_int }
      {
       \tl_put_right:Nn \l__wang_xmat_body_tl { & #2\sb{#3 ##1} }
      }
     \bool_if:NT \l__wang_xmat_dotcol_bool
      {
       \tl_put_right:Nn \l__wang_xmat_body_tl { & \cdots & #2\sb{#3 #5} }
      }
     % if the matrix is infinite, add a further column with \cdots
     \bool_if:NT \l__wang_xmat_infinite_bool
      {
       \tl_put_right:Nn \l__wang_xmat_body_tl { & \cdots }
      }
     % finish up the row
     \tl_put_right:Nn \l__wang_xmat_body_tl { \\ }
    }
  % if the matrix is infinite, add a final row
  \bool_if:NT \l__wang_xmat_infinite_bool
   {
    \tl_put_right:Nn \l__wang_xmat_body_tl { \vdots }
    \prg_replicate:nn { \l__wang_xmat_excols_int - 1 }
     {
      \tl_put_right:Nn \l__wang_xmat_body_tl { & \vdots }
     }
    \bool_if:NT \l__wang_xmat_dotcol_bool
     {
      \tl_put_right:Nn \l__wang_xmat_body_tl { &  & \vdots }
     }
    \tl_put_right:Nn \l__wang_xmat_body_tl { & \ddots }
   }
  % typeset the matrix
  \begin{#1matrix}\l__wang_xmat_body_tl\end{#1matrix}
 }

\ExplSyntaxOff

\begin{document}

\[
\xmat{M}{m}{n} \xmat*[b]{M}{m}{n}
\]
\[
\xmat{M}{m}[2]{n}[4] \xmat*[b]{M}{m}[4]{n}[2]
\]
\[
\xmat{v}{1}{n} \xmat{v}{m}{1} \xmat{v}{m}{2} \xmat{a}{2}{2} \xmat*[v]{a}{2}{2}
\]
\[
\xmat[]{M}{m}{n}
\]

\end{document}

enter image description here

If you want an undelimited matrix, call \xmat[]{M}{m}{n}.

I don't think there is a way to produce this using just tools in the physics package (which I find weird and awkward to use, so I never recommend it).

  • Thank you! I did a little edit by changing \xmat into ximat to be used with physics package. (I used it quite a lot) – Frank Wang Apr 25 at 11:33

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.