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.