[Tex/LaTex] LaTeX Loops in \newcommand

loops

I have used LaTeX for about a year now and every time I write a paper I try to improve on my knowledge so recently I've been creating my own commands to help save time. I am interested if it is possible to use "for-loops" in LaTeX to create \newcommand in the following way:

\newcommand{idmatrix}[1]

where my parameter is the size say n and then it will print an n x n identity matrix. This would be useful for equations where I want to show the matrix calculations but don't have to do the mundane work of using \bmatrix etc and creating a 3×3 or a 2×2 matrix every single time.

Best Answer

Here's an implementation with LaTeX3 features. When \idmatrix{n} is called for the first time a new token list variable is set up containing the code for producing the matrix, so that it can be reused without being built each time.

Thus, \idmatrix{2} will build the token list variable \g_julian_idmatrix_1_tl and use it; subsequent calls of \idmatrix{2} will just use the variable.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\idmatrix}{ m }
 {
  \justin_idmatrix:n { #1 }
 }

\cs_new_protected:Npn \justin_idmatrix:n #1
 {
  \tl_if_exist:cF { g_justin_idmatrix_#1_tl }
   {
    \justin_make_idmatrix:n { #1 }
   }
  \tl_use:c { g_justin_idmatrix_#1_tl }
 }

\cs_new_protected:Npn \justin_make_idmatrix:n #1
 {
  \tl_new:c { g_justin_idmatrix_#1_tl }
  \tl_gput_right:cn { g_justin_idmatrix_#1_tl }
   {
    \left[ % or the delimiter you like best
    % there's a column more for accommodating the empty value after the last 0& (or 1&)
    \begin{array}{ @{} *{#1}{c} @{} c @{} }
   }
  \int_step_inline:nnnn { 1 } { 1 } { #1 }
   {
    % At step k add k-1 zeroes
    \prg_replicate:nn { ##1 - 1 }
     {
      \tl_gput_right:cn { g_justin_idmatrix_#1_tl } { 0 & }
     }
    % Add a 1
    \tl_gput_right:cn { g_justin_idmatrix_#1_tl } { 1 & }
    % Add n - k zeroes
    \prg_replicate:nn { #1 - ##1 }
     {
      \tl_gput_right:cn { g_justin_idmatrix_#1_tl } { 0 & }
     }
    % End the line
    \tl_gput_right:cn { g_justin_idmatrix_#1_tl } { \\ }
   }
  \tl_gput_right:cn { g_justin_idmatrix_#1_tl }
   {
    \end{array}
    \right] % or the delimiter you like best
   }
 }

\ExplSyntaxOff

\begin{document}
\[
\idmatrix{1}\quad
\idmatrix{2}\quad
\idmatrix{3}\quad
\idmatrix{4}
\]
\[
\idmatrix{12}
\]
\end{document}

enter image description here

Related Question