
He usado LaTeX durante aproximadamente un año y cada vez que escribo un artículo trato de mejorar mis conocimientos, por lo que recientemente he estado creando mis propios comandos para ayudar a ahorrar tiempo. Me interesa saber si es posible utilizar "bucles for" en LaTeX para crear \newcommand
de la siguiente manera:
\newcommand{idmatrix}[1]
donde mi parámetro es el tamaño, digamos, n
y luego imprimirá una matriz de identidad n
x . n
Esto sería útil para ecuaciones en las que quiero mostrar los cálculos matriciales pero no tengo que hacer el trabajo mundano de usar \bmatrix
, etc. y crear una matriz de 3x3 o 2x2 cada vez.
Respuesta1
Aquí hay una implementación con características de LaTeX3. Cuando\idmatrix{
norte}
Se llama por primera vez a una nueva variable de lista de tokens que contiene el código para producir la matriz, de modo que pueda reutilizarse sin tener que compilarse cada vez.
Por lo tanto, \idmatrix{2}
construirá la variable de lista de tokens \g_julian_idmatrix_1_tl
y la usará; Las llamadas posteriores de \idmatrix{2}
solo usarán la 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}
Respuesta2
Para responder a tu pregunta todo lo que necesitas es sólo una ligera modificación de lo que se dice.aquí:
\documentclass{article}
\usepackage{amsmath,amssymb,mathtools}
\usepackage{ifthen}
\newcommand{\forLoop}[5][1]
{%
\setcounter{#4}{#2}%
\ifthenelse{ \value{#4} < #3 }%
{%
#5%
\addtocounter{#4}{#1}%
\forLoop[#1]{\value{#4}}{#3}{#4}{#5}%
}%
% Else
{%
\ifthenelse{\value{#4} = #3}%
{%
#5%
}%
% Else
{}%
}%
}
\newcounter{identRow}
\newcounter{identCol}
\newcommand{\idmatrixn}[1]
{
\begin{bmatrix}
\forLoop{1}{#1}{identRow}
{
\forLoop{1}{#1}{identCol}
{
\ifthenelse{\equal{\value{identRow}}{\value{identCol}}}{1}{0}
\ifthenelse{\equal{\value{identCol}}{#1}}{}{&}
}
\\
}
\end{bmatrix}
}
\begin{document}
\idmatrixn{10}
\end{document}
Tenga cuidado ya que noté que el
bmatrix
entorno no admite matrices mayores que
10x m
o m x 10
donde 0<m<11
. Si necesita matrices más grandes, puede utilizarlas array
de la siguiente manera:
\newcounter{identRow}
\newcounter{identCol}
\newcommand{\idmatrixn}[1]
{
\left[\begin{array}{*{#1}c}
\forLoop{1}{#1}{identRow}
{
\forLoop{1}{#1}{identCol}
{
\ifthenelse{\equal{\value{identRow}}{\value{identCol}}}{1}{0}
\ifthenelse{\equal{\value{identCol}}{#1}}{}{&}
}
\\
}
\end{array}\right]
}