[Tex/LaTex] How would you create syntax for variable substitutions

macrosmath-mode

I'm using the following syntax a lot for defining variable substitutions in my calculations:

\left [ \begin{array}{rcl}
    t &=& \sqrt{x+5}\\
    x &=& t^2 - 5\\
    dx &=& 2tdt\\
\end{array} \right ]

It looks (almost!) exactly the way I want, but it is a lot of garbage to write everytime I'm making a substitution. How can I create new Latex syntax so that instead of the above, I would instead write:

\begin{vars}
    t &=& \sqrt{x+5}\\
    x &=& t^2 - 5\\
    dx &=& 2tdt
\end{vars}

And it would render to the same? Even better would be if I could write something like:

\defvars{t, {\sqrt{x+5}}{x, {t^2 - 5}}{dx, {2tdt}}

Best Answer

Here's an idea using etoolbox's \docsvlist:

\documentclass{article}
\usepackage{amsmath,etoolbox}

\makeatletter
\newcommand*\dovar{}
\def\@dovar#1=#2\q@stop{#1&{}={}&#2\\}
\newrobustcmd*\vars[1]{%
  \renewcommand*\do[1]{\@dovar##1\q@stop}%
  \left[
    \begin{array}{r@{}c@{}l}
      \docsvlist{#1}
    \end{array}
  \right]
}
\makeatother

\begin{document}

\[
  \vars{ t = \sqrt{x+5} , x = t^2 - 5 , dx = 2tdt }
\]

\end{document}