I'm trying to produce a command for an equation that may or may not be used inside an align
element, and therefore sometimes needs a &
next to the =
to align the equation. I therefore defined the command \newcommand{\optal}[1][=]{a #1 b}
so that when called as simply \optal
it outputs the equation, but when called as \optal[&=]
I can use the equation inside an align. The problem is that for some reason the command fails; it seems one cannot use &
as a parameter. The problem goes away if I make the argument mandatory (ie, \newcommand{\optal}[1]{a #1 b}
)
Here is the MWE. I'm using Miktex 2.9 and pdflatex.
\documentclass[a4paper, 12pt]{book}
\usepackage{amssymb}
\usepackage{amsmath}
\begin{document}
% new command
\newcommand{\optal}[1][=]{a #1 b}
%trying it out
$\optal$ \\
$\optal[ \neq ]$ \\
% trying it out inside align
\begin{align}
\optal[=]
\end{align}
%so far, so good
% trying it out with & as parameter
\begin{align}
\optal[&=]
\end{align}
% Fails with "Argument of \\optal has an extra } \end{align}"
% Paragraph ended before \\optal was complete \end{align}
\end{document}
Am I missing something? Can you think of a workaround?