1

I've started using new command lately and I think it is great. For example

\newcommand{\QQQ}{~~,\quad\text{and}\qquad }  
\newcommand{\WW}{~~,\quad\text{with}\qquad } 
\newcommand{\WWW}{~~,\quad\text{where}\qquad } 

I also want to have a shortcut for creating equation and align envornments, but newcommand won't let me do it. How can I make a shortcut for

\begin{equation}
\end{equation}?

I'm using TeXStudio if there's a non-latex way to do it in my editor.

For example, if I use

\newcommand{\ZZ }{\begin{align}} 
\newcommand{\ZZZ }{\end{align}} 

and then type

\ZZ 
\pi
\ZZZ

I get the PDF compiler error:

Paragraph ended before \align was complete.
  • 1
    Why \newcommand would not work? What did you try that did not work? Can you add an example to show what you tried? – Vincent Dec 28 '20 at 01:47
  • 4
    With equation it should work, but with align it doesn't because it scans for the \end{align} text, and with your attempt it fails to find it. I must say, though, \begin{align} tells a lot more than \ZZ to me, and with some decent tab completion you can type ba<TAB> and get \begin{align} ... \end{align} typed (even fewer keystrokes than \ZZ ... \ZZZ) – Phelype Oleinik Dec 28 '20 at 02:00
  • 4
    My advice: don't use these shortcuts. They seem to save time, but they actually cost you a lot more than they save (for starters, the time you spent asking this question) – Phelype Oleinik Dec 28 '20 at 02:02
  • 2
    The only option you have is to \long\def\ZZ#1\ZZZ{\begin{align}#1\end{align}}. This approach has been covered before. – Werner Dec 28 '20 at 03:30

2 Answers2

0

This is only possible in LuaTeX.

\documentclass{article}
\usepackage{luacode}
\usepackage{amsmath, amssymb}

\begin{luacode*} local uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" local lowercase = "abcdefghijklmnopqrstuvwxyz" local numbers = "0123456789" local char_set = uppercase .. lowercase .. numbers

function get_random_name(k) local tab = {} for i=1,k do local rand = math.random(char_set:len()) table.insert(tab, char_set:sub(rand, rand)) end return table.concat(tab,"") end

function register_verbatim(cmd_e, env_name) local name = get_random_name(15) local env_s = string.format([[\begin{%s}]], env_name) local env_e = string.format([[\end{%s}]], env_name) tex.print(env_s) local callback = function(str) if string.find(str, cmd_e) then luatexbase.remove_from_callback("process_input_buffer" , name) return env_e else return str end end luatexbase.add_to_callback("process_input_buffer" , callback, name) end \end{luacode*}

\newcommand{\al}{ \directlua{ register_verbatim([[\string\eal]], "align") } } \newcommand{\als}{ \directlua{ register_verbatim([[\string\eals]], "align*") } }

\begin{document}

\al \int_a^b \eal

\als \frac{a}{b} \eals

\end{document}

Alan Xiang
  • 5,227
0

It is not only possible in LuaTeX. It is possible in plain TeX or LaTeX too. For example:

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

\def\al#1\eal{\begin{align}#1\end{align}} \def\als#1\eals{\begin{align}#1\end{align}}

\begin{document}

\al \int_a^b \eal

\als \frac{a}{b} \eals

\end{document}

wipet
  • 74,238