# Using newcommand with math environment

I defined a new command as:

\newcommand{\eq}[2]{$$\begin{split}#1\end{split}\label{#2}$$}


so to make a new equation I do:

 \eq{equation stuff here}{label here}


But since I've not used the equation environment explicitly, texstudio highlights every math symbol in the equation. It is really annoying. Is there a way around this?

-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. (I moved my first comment to an answer, since I'm very confident that there is no answer. If someone proves me wrong, I'll be impressed :-). ) –  Sean Allred Jul 11 '13 at 19:37
I advise you not doing that, because you lose information. For instance, you can't look for all the labels with a simple find function for \label. Maintenance of compact code is harder. –  egreg Jul 11 '13 at 19:38
I suggest you to use the capability of TeXstudio to autocomplete the commands instead of defining new ones. –  Sigur Jul 11 '13 at 19:44
egreg, I do prefixes to my labels, e.g. fig: or eq:, so finding them wont be a problem. Vedran, That seems like a reasonable compromise, thanks a lot. P.S. Being a new user, I cannot add comments, so I must post this as an answer. –  user33542 Jul 11 '13 at 19:57

Given the nature of TeX's language, I don't think there is a good, reusable solution to this one. Syntax highlighting is something done usually using regular expressions, but TeX is (literally) a class of languages higher than regular expressions can cover. Getting the syntax highlighting right would require some serious umph behind TeXStudio's parser - paramount to including a TeX distribution in-house.

-
I was a bit pessimistic about the problem. I guess I can just turn off the highlighting in texstudio. Thanks for the reply. –  user33541 Jul 11 '13 at 19:34

If you're content with replacing the command \eq with the environment eq, this should be doable. Just define your own CWL file with the line

\begin{eq}#\math
\end{eq}#S


Then include this file in options, under Completition.

More on the CWL format can be found here and the basic info on the file itself is here.

-
Since I don't use TeXStudio anymore, I didn't know about this functionality. Neat! I would note to @Harsh though that environments can also take arguments. So, whether or not it's A Good Idea, you could incorporate the automatic label as an argument in much the same way as you would do in \newcommand. –  Sean Allred Jul 11 '13 at 20:32

You can define something like that:

\def\eq$$$#1$$${$$#1$$}

\eq$a(b+c)=ab+ac, \label{distriblaw}$


and you have both shorthand and highlighting!

(Read Knuth's TeXbook rather than LaTeX tutorials to know how to define commands properly. ;-) )

Here are some more of my own definitions:

\def\eq#1$$#2$${\begin{equation#1}#2\end{equation#1}}
\long\def\subeq#1{\begin{subequations}#1\end{subequations}}
\def\Split$$#1$${\begin{split}#1\end{split}}
\def\Align#1$$#2$${\begin{align#1}#2\end{align#1}}
\def\Aligned#1{\begin{aligned}#1\end{aligned}}
\def\Gather#1$$#2$${\begin{gather#1}#2\end{gather#1}}
\def\Gathered#1{\begin{gathered}#1\end{gathered}}
\def\Multline#1$$#2$${\begin{multline#1}#2\end{multline#1}}


Examples:

\Align*$$a&=b+c, \\ e&=fg.$$

\Align at$${2} a&=b,&c&=d, \label{eq1} \\ e&=f,&g&=h. \label{eq2}$$

-
using  in latex removes the ability to use some useful features that have been provided by the named latex environments, such as \qedhere with amsthm. although ingenious, this is not a practical solution. –  barbara beeton May 13 at 16:21