2

I recently was convinced of the benefit of using align over eqnarray, but I am really not writing

\begin{align}
...
\end{align}

every time, so I just did what I always do:

\def\bea{\begin{align*}}
\def\eea{\end{align*}}

However, align do not seem to work with my defined commands. I even tried other ways of defining, with \let and \newcommand, but none seem to work. I always get the same error. To give an example, if I type:

\bea
    E = mc^2
\eea

which is very simple, I still get:

Runaway argument? E &= mc^2 \eea
! Paragraph ended before \align* was complete.

\par

However if I write the full commands, it does work. How can it be so and how do I solve it? I know LaTeX only expands \eea into \end{align*}, so there should be no way one can result in error and the error cannot.

chsk
  • 3,667
  • 2
    See https://tex.stackexchange.com/questions/100138/what-is-wrong-with-defining-bal-as-beginalign – Steven B. Segletes May 05 '21 at 14:34
  • Also https://tex.stackexchange.com/q/593636/134574 and https://tex.stackexchange.com/q/314530/134574 and https://tex.stackexchange.com/q/171027/134574 – Phelype Oleinik May 05 '21 at 14:35
  • 1
    Users attempt this every few days. Don't, it makes your code much harder to read. Often your editor will have shortcuts for it instead – daleif May 05 '21 at 14:36
  • @daleif it does not get too hard to read if you use it long enough and my fingers to thank me for it, because I write a lot in LaTex. – lucas belasque May 05 '21 at 14:46
  • Thanks @StevenB.Segletes, I had I look there and could understand the problem. – lucas belasque May 05 '21 at 14:47
  • 5
    @lucasbelasque trusty me, I've been using LaTeX for almost 25 years, it is not worth it, it is so annoying to read code from someone who writes like this. My editor gives easy access to align and easy access to change it to align* (which yours does not) or any other environment. – daleif May 05 '21 at 14:49
  • Are you able and willing to compile your documents with LuaLaTeX? – Mico May 05 '21 at 14:51
  • I really hope that you don't always use \def to create new commands. That is dangerous as you won't get any warning if you overwrite an existing command. – Ulrike Fischer May 05 '21 at 15:04
  • @UlrikeFischer nope, I do not do that always. I do for some commands that I use extensevely and my fingers get tired of writting. – lucas belasque May 05 '21 at 15:31
  • I didn't meant the commands itself, but that you use \def to define them. But beside this: I too think that it is a bad habit to invent such shorthands. I get sometimes the request to debug some problem in such a document and it is a pain to have to translate all sort of commands. Do it only if you are the only one handling the document. And if you ask a question here, translate your own commands first. – Ulrike Fischer May 05 '21 at 16:03
  • 2
    I'm a strong proponent of using snippets to take the burden off my fingers while simultaneously making sure everything is still readable. Any modern editor (i.e. Vim, VSCode, even Overleaf) will have snippets and autocomplete commands available, often as extensions. – Antoine Ego May 05 '21 at 16:07
  • @UlrikeFischer oh, sure. I just use macros in my own documents. If there is the chance that someone will colaborate, I just type everything. Also, I use sometimes \newcommand. – lucas belasque May 05 '21 at 16:11
  • 1
    a reasonable tex editor will let you enter \begin{align} \end{align} with three or four keystrokes so fewer that \bea \eaa also the editor (and forums such as this) will know align is math and give correct syntax highlighting but they will not know that \bea starts math mode. It is really much better to use the standard syntax here (and for environments in general) – David Carlisle May 05 '21 at 18:10

2 Answers2

3

In the flavor of Mico's answer, this tokcycle approach also examines all the tokens in advance, makes substitutions, and then typesets the result. So here, \bea and \eea are just quarks, discarded and replaced by the token cycle.

Best of all, it also runs in pdflatex and only takes a single compilation. Just wrap your document in \tokencyclexpress...\endtokencyclexpress.

Provisos: catcode-changing material (such as verbatim) must be excluded from the environment. EDITED to support use of #. So just end and restart the environment as needed.

Of course, the underlying problem here is that your source code still uses a syntax that will confound your coauthors and which your editors will hate.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle,amsmath}
\Characterdirective{\whennotprocessingparameter#1{\addcytoks{#1}}}
\Macrodirective{
  \ifx\bea#1\addcytoks{\begin{align}}\else
  \ifx\eea#1\addcytoks{\end{align}}\else
  \addcytoks{#1}\fi\fi}
\def\bea{\bea}
\def\eea{\eea}
\begin{document}
\tokencyclexpress
\tableofcontents

\hrulefill

\section{My equation}

\def\massmultiplier#1{m#1} \meaning\massmultiplier

Here it is. \bea E &= \massmultiplier{c^2} \ 1+1 &= 2 \eea Wasn't that great?

\endtokencyclexpress \end{document}

enter image description here

2

If you're free and able to compile your documents with LuaLaTeX, you could define a Lua function that replaces all instances of \bea and \eea with \begin{align} and \end{align}, respectively, at a very early stage of processing, before TeX starts it usual processing work.

Oberve that with this set, \bea and \eea may look like they're ordinary LaTeX macros. However, that's not the case. They're just markers that will be replaced at the preprocessor stage with real LaTeX directives.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath} % for align environment
\usepackage{unicode-math}

\usepackage{luacode} % for \luaexec macro \luaexec{ function bea_eea ( s ) s = s:gsub ( "^\bea" , "\begin{align}" ) s = s:gsub ( "^\eea" , "\end{align}" )
return s end } % Assign the Lua function to LuaTeX's "process_input_buffer" callback: \AtBeginDocument{\directlua{luatexbase.add_to_callback ( "process_input_buffer" , bea_eea , "beaeea" )}}

\begin{document} \bea E &= mc^2 \ 1+1 &= 2 \eea \end{document}

Mico
  • 506,678
  • 5
    SamCarter will be so disappointed you broke her very important tikzlings-bears package, \bear[eye=red] ... – David Carlisle May 05 '21 at 18:30
  • @DavidCarlisle - :-) \bear could still be used, just not at the very start of a line. (The gsub function kicks into high gear only if \bea or \eea occur at the start of line...) – Mico May 05 '21 at 18:43
  • 2
    @Mico you could use s:gsub ( "^\\bea(%W)" , "\\begin{align}%1" ) to make this bear-safe! – Skillmon May 05 '21 at 20:36