1

Is there anyway to define a newcommand for an environment shortcut ? This is what I have so far, but is not working as I would like to.

\newcommand{\beq}{\begin{equation}
    \label{eq:}

and

\newcommand{\eeq}{\end{equation}}

I would like to define a command such that typing

\eq{1} 
x + y = z 
\eeq

is the same as typing something similar to

 \begin{equation} 
\label{eq:1} 
x + y = z
\end{equation}. 

Would this be possible ? I have read the posts in Define a new command with parameters inside newcommand and What exactly do \csname and \endcsname do? but did not fully understand so it would be great if someone could help me out as I am relatively new to latex. The compiler I am using is texstudios and would greatly appreciate any help.

Thank you

  • 4
    No No No No No, you don't do that! Get a decent editor that lets you be lazy, but don't use LaTeX macros to shorten math. – Johannes_B Jun 27 '16 at 6:47
  • 3
    as previously noted, best not to use numeric labels. In general you can do that but you can not do that with AMS math environments, as documented. Even where it works it is usually a bad idea as environments are the major syntactic construct introduced by latex and it is much easier for your editor to help navigate and display the code properly if you do not hide the structure, – David Carlisle Jun 27 '16 at 6:47
  • @DavidCarlisle I understand your point but I write all my equations inside environments using theorems so I could still keep track of the structure. I will also make sure to skip a line for each shortcut so that I still can display the code properly. Thank you for the advice though ! – PutsandCalls Jun 27 '16 at 6:55
  • 4
    No! never put a blank line before a display math environment that produces entirely the wrong spacing if you do. – David Carlisle Jun 27 '16 at 7:00
  • 3
    I strongly advice against the proposed solutions, as they are not solutions. Math environments should never be hidden. Please don't do that. You probably will get into problems later. – Johannes_B Jun 27 '16 at 7:26
2

Or even shorter:

\documentclass{article}
\newcommand{\eq}[2]{\begin{equation} \label{eq:#1} #2 \end{equation}}

\begin{document}

    \eq{1}{x + y = z}

    \eq{2}{a + b = c}

\end{document} 

After you type in your doc, you can "translate" back your source tex file to regular environments with sed or alike. I do this very often despite what others have said (which I normally agree - but not when you have 10000 equations nested as heck in all sorts of environments in a book with 3 nested translations in the same file). TeX brags about letting you concentrate on the contents doesn't it?

4

Use the below:

\documentclass{article}

\makeatletter
\newcommand{\beq}[1]{\begin{equation}\label{eq:#1}}
\newcommand{\eeq}{\end{equation}}
\makeatother

\begin{document}

\beq{1} 
x + y = z 
\eeq

\beq{2} 
x + y = z 
\eeq

\end{document}

Output:

enter image description here

  • 2
    No, no and no. The fact that the question asks for something ridiculous, conceptually wrong and actually useless does not mean we have to answer it. Moreover, placing two equations in a row is bad for their spacing; and have you tried doing the same thing with align or gather rather than with equation? – yo' Jun 27 '16 at 7:47

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.