0

Is it possible to define a macro for the align* environment, or allow multi-line support for the equation display mode $$ $$?


I use the align* environment very frequently, and a typical usage goes as follows:

\begin{align*}
A &= B \\
&= C
\end{align*}

Typing the entirety of the command every time becomes a hassle, so I attempted to define a macro for them:

\documentclass[10pt]{article}
\usepackage{geometry}% larger text width
\usepackage{amsmath}

\newcommand{\begin{align}}{\bal} \newcommand{\end{align}}{\eal}

\begin{document} text here \end{document}

But this throws errors, seemingly because \begin etc. can't fit into defining a \newcommand:

! LaTeX Error: Command \begin already defined. (Line 5)
! Undefined control sequence. (Line 5)
! LaTeX Error: Command \end already defined. (Line 6)
! Undefined control sequence. (Line 6)

So is it possible to do one of the following?

  1. Define a shortcut for the align* environment.
  2. Get a multi-line alignment support for the display mode $$ $$.
  3. Find another command that creates multi-line aligned equations with optional numbering.
Uzu Lim
  • 111
  • 3
    Welcome to TeX.SX! You are yet one more user to fall in this trap. See https://tex.stackexchange.com/q/100138/134574. Basically it won't work properly, so it's less time-consuming to type the environment than to pursue a fully working shortcut – Phelype Oleinik Apr 19 '21 at 17:50

2 Answers2

3

All of the aspects you request are possible, however, I would suggest you employ option #3. I'll briefly address each of these, in turn.

  1. Define a shortcut for the align* environment.

You cannot do this via \newcommand, as you have attempted (nor via \def, which suffices for some simpler environments). I'll leave the details to other posts (H/T Phelype Oleinik & David Carlisle). While you could accomplish this with more complex code, doing so is generally not considered a good idea (H/T David Carlisle).

  1. Get a multi-line alignment support for the display mode $$ $$.

There is no great way of doing this directly, but there are various options. More generally, it is best to consider improved ways of aligning equations.

  1. Find another command that creates multi-line aligned equations with optional numbering.

While many prefer otherwise, I would recommend breqn. There are a number of useful discussions and alternatives.

Coby Viner
  • 1,939
2

Here's a LuaLaTeX-based approach to meeting your first objective. (By meeting the first objective, it's no longer necessary to deal with the two fallback objectives, right?) This approach lets you employ markers called \bal and \eal which, if placed at the start of a line, will be replaced with \begin{align*} and \end{align*} at a very early stage of processing, before TeX does its usual work.

Observe that it's still possible to define "real" LaTeX macros called \bal and \eal, respectively. Just make sure that they're never used at the start of a line.

enter image description here

That said, I don't think it's really all that burdensome or cumbersome to just write \begin{align*} and \end{align*} by hand.

\documentclass{article}
\usepackage{amsmath} % for 'align*' env.

\usepackage{luacode} % for '\luaexec' macro \luaexec{ function bal_eal ( s ) s = s:gsub ( "^\bal" , "\begin{align}" ) s = s:gsub ( "^\eal" , "\end{align}" ) return s end }

% Assign the Lua function to LuaTeX's 'process_input_buffer' callback: \AtBeginDocument{\luaexec{luatexbase.add_to_callback( "process_input_buffer", bal_eal, "baleal" )}} % Define a separate macro called '\bal' \newcommand\bal{Hello World}

\begin{document} \noindent An instance of \verb+\bal+ not at the start of a line: \bal. \bal % <-- '\bal' occurs at start of line A &= B \ &= C \eal % <-- '\eal' occurs at start of line More text. \end{document}

Mico
  • 506,678