I've defined a command that uses square brackets, and when I use it within a matrix environment, the command is not parsed. I stumbled upon a solution to my problem here:
"[" Square bracket Curiosities in an array environment
which is to place {} around my command. But I'm not understanding why \\[] is the issue since I am not even using it. Note however, that if I load amsmath then the fix no longer works.
- What is the explanation for all this? And why does
amsmathbreak the fix? - Why am I able to use my command without issue within an
alignenvironment? - What other environments, besides
matrix, will be affected? - Can I adjust my definition (while keeping the square brackets) so that it is more robust?
About the coding style: This is actually a much simplied version of another macro I have written. For that macro, valid inputs are: \func \func[A] \func[A][B] \func[A][] \func[][B]. So I have multiple optional arguments. The actual macro also makes use of parenthesis as a way of specifying additional information. All of this is quite involved and it is why I turned to http://www.ctan.org/tex-archive/support/newcommand to help me generate the code. So the style of this macro follows generated output from newcommand.py, and that is why I'd like a fix for the code I presented rather than a completely new solution.
Demonstration:
\documentclass{minimal}
\usepackage{amsmath} % loading this actual causes the solution to fail
\makeatletter
\def\democmdr{%
\@ifnextchar[{\csname democmdr@i\endcsname}
{\csname democmdr@i\endcsname[]}
}%
\expandafter\def\csname democmdr@i\endcsname[#1]{%
0#10
}%
\makeatother
\begin{document}
\setlength{\parindent}{0pt}
$\democmdr[5] \qquad \begin{matrix}\democmdr[5]\end{matrix}$ \\ % does not work
$\democmdr[5] \qquad \begin{matrix}{\democmdr[5]}\end{matrix}$ % works
\begin{align*}
\democmdr[5] % works even though \\ can be used in this environment
\end{align*}
\end{document}
Quick update: It turns out that loading amsmath breaks the solution I mentioned.
\democmdrwithout[]supposed to do? I think, you get into trouble, because\demcmdrtries to read [...] or should do nothing? – Oct 17 '14 at 04:22[]such thatTeXis not confused – Oct 17 '14 at 04:53\func \func[A] \func[A][B] \func[A][] \func[][B]. It sounds like I might have to rethink how I've defined the macro and shift to something closer to what you defined, but it looks like your way would be clunkier since I'd repeat myself a bit more. – Tom Oct 17 '14 at 05:00[]optional arguments, the whole affair is more difficult. In such a case, I recommend to use more appropiate packages, such asxparse, which can with definitions of such macros (i.e. allows them) – Oct 17 '14 at 05:02newcommand.pyto the original post above, not in comments – Oct 17 '14 at 05:45