5

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 !

  • Can you provide a complete minimal working example? Now, it is hard to tell where the error lies exactly. – Ruben Jul 7 '15 at 18:17
  • Sometimes it's better not to wrap anything in a command, but in this case, I suggest a starred/unstarred version of your command, rather. And your example does not compile due to \IfEqCase is unknown – user31729 Jul 7 '15 at 19:05
  • @Corathy: Your example is still missing \usepackage{xstring} ... – user31729 Jul 7 '15 at 19:13
  • I did not get an error message saying that IfEqCaseis 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 7 '15 at 19:14
  • Why the xstring package ? It works on my computer without it... – Emilie Picard-Cantin Jul 7 '15 at 19:24
5

Here is an idea: pass the alignment operator & into the macro when you need it and {} otherwise. Keeps it simple.

\documentclass{article}
\usepackage{amsmath}

\newcommand\myeq[1]{2y_3 #1= x}

\begin{document}

\noindent Lorem ipsum dolor sit amet, $\myeq{}$, consetetur sadipscing
elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
aliquyam erat:
% don't leave a blank line here - see comment below by barbara beeton
\[ \myeq{} \]

Stet clita kasd gubergren, no sea takimata sanctus est.

\begin{align}
\myeq{&} \\
y_1 &= a + b
\end{align}


At vero eos et accusam et justo duo dolores et ea rebum. Stet clita 
kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\begin{align*}
    \myeq{&}
\end{align*}

\end{document}

To make it more symmetrical you could also use

\newcommand\myeq[1]{2y_3 #1 x}

and call either \myeq{=} or \myeq{&=}.

enter image description here

  • 4
    not really relevant to the answer (ingenious!), but a recommended practice -- don't leave a blank line before any display environment. the spacing will always be wrong. if you want to leave a visual clue in the input, put a % at the beginning of the otherwise blank line, and it will be treated as a comment (and ignored). – barbara beeton Jul 7 '15 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.