I'm trying to write a package to provide some shortcut commands for common linear algebra operations (for my intro to linear algebra course at least). However, I'm getting weird errors under certain circumstances. Based on other posts I think I'm getting tripped by the fragile vs. robust command thing (which I don't completely understand), and I'm not sure how to fix it. I have tried changing my macros to \DeclareRobustCommand
and adding \protect
s in various places, but none of those solved the issue. Specifically, I'm getting the following error:
Use of \\mat doesn't match its definition.
\@ifnextchar ... \reserved@d =#1\def \reserved@a {
#2}\def \reserved@b {#3}\f...
l.136 This \mat{\sqrt{2},0,0,0}
also doesn't work
If you say, e.g., `\def\a1{...}', then you must always
put `1' after `\a', since control sequence names are
made up of letters only. The macro here has not been
followed by the required stuff, so I'm ignoring it.
And here is the MRCE for the error (las
stands for "linear algebra shortcut"):
\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{tcolorbox}
\usepackage{etoolbox}
\usepackage{pgffor}
\usepackage{pgfmath}
\usepackage{calc}
\usepackage{intcalc}
\usepackage{calculator}
\usepackage{nicematrix}
\makeatletter
\setcounter{MaxMatrixCols}{25}
\gdef\las@defaultvec{row}
\newcounter{las@matrow}
\newcounter{las@matcol}
\newcounter{las@listlen}
\newcounter{las@matsize}
\newcommand{\las@length}[2]{ % \len{list}{counter}
\setcounter{#2}{0}
\foreach\@e in {#1} {
\stepcounter{#2}
}
}
\newcommand{\las@createMatrix}[3]{ % dims, rows
\setcounter{las@matrow}{#1}
\setcounter{las@matcol}{#2}
\setcounter{las@listlen}{0}
\las@length{#3}{las@listlen} % get the length of the list after expansion
\setcounter{las@matsize}{\thelas@matrow*\thelas@matcol}
\ifnum\thelas@matsize=\thelas@listlen % if neq
\else
\PackageError{LAS}{Attempt to define a \arabic{las@matrow} by \arabic{las@matcol} matrix with \arabic{las@listlen} elemets}{}
\fi
\xdef\las@resMatrix{}
\foreach \elem[count=\@i] in {#3}{
% \xdef\las@resMatrix{\las@resMatrix \elem \\}
\ifnum\@i=\thelas@matsize
\xdef\las@resMatrix{\las@resMatrix\elem}
\else
\ifnum\intcalcMod{\@i}{\thelas@matcol}=0
\xdef\las@resMatrix{\las@resMatrix \elem \\}
\else
\xdef\las@resMatrix{\las@resMatrix \elem &}
\fi
\fi
}
}
\newcommand{\las@vec}[2]{
\las@length{#2}{las@listlen}
\ifstrequal{#1}{row}{
\las@createMatrix{1}{\thelas@listlen}{#2}
}{
\ifstrequal{#1}{col}{
\las@createMatrix{\thelas@listlen}{1}{#2}
}{
\PackageError{LAS}{Unknown option "#1". Valid options are "col" and "row"}{}
}
}
}
\newcommand{\las@mat}[2]{
\setcounter{las@matrow}{1}
\setcounter{las@matcol}{1}
\ifstrequal{#1}{0,0}{
\las@length{#2}{las@listlen}
\SQRT{\thelas@listlen}{\@dim}
\FLOOR{\@dim}{\@dim}
\ifnum\intcalcSqr\@dim=\thelas@listlen
\else
\PackageError{LAS}{Not a square matrix. Please specify the dimensions}{}
\fi
\setcounter{las@matrow}{\@dim}
\setcounter{las@matcol}{\@dim}
}{
\foreach\elem[count=\@i] in {#1} {
\ifnum\@i=1
\setcounter{las@matrow}{\elem}
\else
\ifnum\@i=2
\setcounter{las@matcol}{\elem}
\else
\PackageError{LAS}{Too many options given to mat}{}
\fi
\fi
}
}
\las@createMatrix{\thelas@matrow}{\thelas@matcol}{#2}
}
\renewcommand{\vec}[2][col]{ % overrides \vec in amsmath
\ensuremath{
\las@vec{#1}{#2}
\begin{bNiceMatrix}
\las@resMatrix
\end{bNiceMatrix}
\ifstrequal{#1}{row}{^T}{}
}
}
\newcommand{\mat}[2][0,0]{
\ensuremath{
\las@mat{#1}{#2}
\begin{bNiceMatrix}
\las@resMatrix
\end{bNiceMatrix}
}
}
\makeatother
\begin{document}
This works
\begin{equation*}
\mat{1,...,9}
\end{equation*}
This \mat{\frac{1}{3},\frac{2}{4},0,0} also works
Vectors \vec{1,...,4} are fine too
% This doesn't work
% \begin{align*}
% \mat{1,...,9}
% \end{align*}
% This \mat{\sqrt{2},0,0,0} also doesn't work
%
% \vec{\sqrt{2},0,0,0} doesn't work either
\end{document}
The above code is from a bigger package I'm making, but for the ease of reproducing the error I have merged them into the same document. The document compiles fine as it currently stands, however uncommenting anything in the document marked as not working will result in the error. I haven't figured out what exactly triggered the error, but I know from experience that using either \vec
or \mat
inside the align
environment will not work. Using \sqrt
inside any of these macro doesn't work either.
There was one related question about this package here.