3

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}}
5
  • 1
    firs step: \left[ \begin{aligned} t & = \sqrt{x+5}\\ x & = t^2 - 5\\ dx & = 2tdt \end{aligned}\; \right], second step: \newcommand\defvars[6]{\left[ \begin{aligned} #1 & = #2 \\ #3 & = #4 \\ #5 & = #6 \end{aligned}\; \right]} and than use as \defvars{t}{\sqrt{x+5}}{x}{t^2-5}{dx}{2tdt}. Is this what looking for?
    – Zarko
    Mar 14, 2017 at 20:28
  • @Zarko: I think you should turn your comment into an answer. Edit: If I may suggest it, I prefer using \newcommand* in these situations.
    – GuM
    Mar 14, 2017 at 20:32
  • @GustavoMezzetti, I was not sure, id I correctly understand OP question ... I can't manage showed desired syntax of command. And, yes, beter is use \newcommand*. Thank you for encouragement. I will convert my comment to answer.
    – Zarko
    Mar 14, 2017 at 20:36
  • @Zarko: Thanks, but that is hardcoded to three variables. I want the syntax to work for an arbitrary number. Mar 14, 2017 at 20:39
  • @BjörnLindqvist, this you not mentioned in your question ... since I didn't understood it well, I first wrote comment, which I later convert into answer. Meanwhile you receive solution which better fit your expectations.
    – Zarko
    Mar 14, 2017 at 20:49

3 Answers 3

3

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}
7
  • +1, but I really think that using aligned, as @Zarko suggests, is much better! :-)
    – GuM
    Mar 14, 2017 at 20:44
  • Perfect thanks! I'll absolutely research the difference between aligned and array Mar 14, 2017 at 20:46
  • @GustavoMezzetti nothing against aligned but why do you think that it is much better?
    – cgnieder
    Mar 14, 2017 at 20:59
  • @BjörnLindqvist I improved the answer a bit with regard to spacing
    – cgnieder
    Mar 14, 2017 at 20:59
  • @clemens: Well, it is well known that amsmath’s alignments provide the exact spacing that is required around the “=” sign—that is, exactly the same amount that is used in all math formulas… :-)
    – GuM
    Mar 14, 2017 at 22:32
2

Let me convert my comment into answer:

I assumed that you can add package amsmath or mathtool . With them you can simplify typing of your equation array to:

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

this can use in in definition of new command:

\newcommand*\defvars[6]{\left[ \begin{aligned}
    #1 & = #2   \\
    #3 & = #4   \\
    #5 & = #6
\end{aligned}\; \right]}

and then use for example as follows:

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

In both case (direct use of code or it use as command) the result is the same:

enter image description here

2

You can use xparse:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\newcommand{\diff}{\mathop{}\!d}

\ExplSyntaxOn
\NewDocumentCommand{\vars}{m}
 {
  \left[\, % this opens a group
  \tl_clear:N \l_tmpa_tl
  \seq_set_split:Nnn \l_tmpa_seq { \\ } { #1 }
  \seq_map_inline:Nn \l_tmpa_seq
   {
    \bjorn_vars_line:n { ##1 }
   }
  \begin{aligned}
  \tl_use:N \l_tmpa_tl
  \end{aligned}
  \,\right]
 }
\cs_new_protected:Nn \bjorn_vars_line:n
 {
  \seq_set_split:Nnn \l_tmpb_seq { = } { #1 }
  \tl_put_right:Nx \l_tmpa_tl
   {
    \seq_item:Nn \l_tmpb_seq {1}
    &=
    \seq_item:Nn \l_tmpb_seq {2}
    \exp_not:N \\
   }
 }
\ExplSyntaxOff

\begin{document}

$\vars{
  t = \sqrt{x+5} \\
  x = t^2 - 5 \\
  \diff x = 2t\diff t
}$

\end{document}

enter image description here

2
  • Any advantage to that over clemens method? Mar 15, 2017 at 14:26
  • @BjörnLindqvist Simplicity? ;-)
    – egreg
    Mar 15, 2017 at 17:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .