Suppose I have an equation like y=a+b that I want to repeat multiple times. I want to use macros to define this equation.
First, I thought that the following definition would do the tric.
\newcommand{\myEquation}{\ensuremath{y = a + b}}
But then I figured I might want to use this equation inside a align sometimes, so I need parameters. Then I tried :
\newcommand{\myEquation}[1]{
\IfEqCase{#1}{
{a}{\ensuremath{y = a + b}
{b}{\ensuremath{y &= a + b}
}[\PackageError{tree}{Undefined option to tree: #1}{}]
}
When I call this command with \myEquation{a}, I get the error message below.
Undefined control sequence. \myEquation{a}
Misplaced alignment tab character &. \myEquation{a}
Package tree Error: Undefined option to tree: a. \myEquation{a}
Can someone help me understand the problem here and what I should do ?
Thank you !
The following compiles without errors but the alignment is wrong.
\documentclass[12pt]{article}
\usepackage{amssymb, amsmath, amsthm}
\newcommand{\myFirstEquation}{y_1 = a + b}
\newcommand{\mySecondEquation}{2 y_2 = x}
\begin{document}
\begin{align}
\myFirstEquation \\
\mySecondEquation
\end{align}
\end{document}
The following example compiles with the previous error message.
\documentclass[12pt]{article}
\usepackage{amssymb, amsmath, amsthm}
\newcommand{\myEquation}[1]{
\IfEqCase{#1}{
{a}{\ensuremath{2 y_3 = x}}
{b}{\ensuremath{2 y_3 &= x }}
}[\PackageError{tree}{Undefined option to tree: #1}{}]
}
\begin{document}
\myEquation{a}
\end{document}
As suggested, I tried an example without the \IfEqCase. It works.
\documentclass[12pt]{article}
\usepackage{amssymb, amsmath, amsthm}
\newcommand{\myEquation}{\ensuremath{2 y_2 = x}}
\newcommand{\myEquationP}{\ensuremath{2 y_2 & = x}}
\newcommand{\otherEquation}{y_1 &= a + b}
\begin{document}
\begin{equation}
\myEquation
\end{equation}
\begin{align}
\myEquationP \\
\otherEquation
\end{align}
\end{document}
Would be nice if the \IfEqCase worked too though. But I guess it will do !

starred/unstarredversion of your command, rather. And your example does not compile due to\IfEqCaseis unknown – Jul 07 '15 at 19:05\usepackage{xstring}... – Jul 07 '15 at 19:13IfEqCaseis unknown, that is probably why I could not fix it. Thank you, I edited my question. I now have a solution that works. – Emilie Picard-Cantin Jul 07 '15 at 19:14xstringpackage ? It works on my computer without it... – Emilie Picard-Cantin Jul 07 '15 at 19:24xstringthen. – Jul 07 '15 at 19:39