6

I am trying to figure out if it is possible to (locally) redefine \\ (double backslash) within amsmath's align environment. On the one hand, I'd like to be able to dynamically insert extra columns, on the other I'd like to manipulate spacing. Here is an example, which does not work:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align*}
\renewcommand{\\}{& x \\[1cm]}%
1  \\
2  \\
3
\end{align*}

\end{document}

The adapted \\ is active for the first line but seems to be reset afterwards so the output is

1x


2
3

Redefining the command once again after the first line makes it active for the second but stops again working after the second line.

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align*}
\renewcommand{\\}{& x \\[1cm]}%
1  \\ \renewcommand{\\}{& x \\[1cm]}%
2  \\
3  \\
4
\end{align*}

\end{document}

Similarly, if I first store away the original \\ and use that in my redefinition

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\let\mylb\\
\begin{align*}
\renewcommand{\\}{& x \mylb[1cm]}%
1  \\ 
2  \\
3  \\
4
\end{align*}

\end{document}

the result is not quite what I hoped for as this seems just to swallow up the linebreak:

1x2 
3
4

Is there a way to achieve what I'm trying here?

2 Answers2

10

The align environment (with or without star) is internally implemented via a table (\halign is the TeX primitive). Each cell is inside a group. Therefore local definitions are lost after the cell.

Also I would not want to mess with \\ by global definitions. Equations could also occur inside other environments (center, flushleft, ...), which also redefine \\ locally. Global redefinitions would destroy the re-establishing of the previous meaning after such an environment.

The following example solves the issue by using a different command name \NL. The global redefinitions are done in \noalign before the new row starts.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\gdef\NL{\\}
\begin{align*}
  a\NL
  b\NL
  \noalign{\gdef\NL{& x \\[1cm]}}%
  1 \NL
  2 \NL
  3 \NL
  \noalign{\gdef\NL{\\}}%
  c \NL
  d
\end{align*}

\end{document}

Result

Heiko Oberdiek
  • 271,626
  • 1
    Thanks for explaining the behavior. I was, however, rather hoping not having to work with a different macro but redefine \ for the scope of one align. I've originally placed it doing something like \let\mylb\\renewcommand{\}{& x \mylb}\begin{align}1 \ 2 \ 3\end{align} did not seem to have any effect at all. – Arno Mittelbach Mar 21 '15 at 09:39
  • On second thoughts .. the tip with halign does it. That is, when redefining halign locally I am able to get the effect that I was going for. – Arno Mittelbach Mar 21 '15 at 09:59
5

Just for completeness. Heiko's pointer to halign allows to implement the effect I was going for. Here is the short code snippet.

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begingroup
\let\myhalign\halign
\def\halign{%
\let\mylb\\
\renewcommand{\\}{& x \mylb[1cm]}
\myhalign
}
\begin{align*}
1  \\ 
2  \\
3
\end{align*}
\endgroup

\begin{align*}
1  \\ 
2  \\
3
\end{align*}

\end{document}

Here I was able to redefine \\ for the first align without changing other aligns. The output of the above is:

1x


2x


3



1
2
3
  • Could I ask some small questions? 1. With only \let\mylb\\ \renewcommand{\\}{& x \mylb[1cm]} it only works for text instead of the align* block. What causes this weird result? 2. With yours, only align* but not text works. After some search especially https://tex.stackexchange.com/a/383381/308105, I know \let will make \myhalign expansion same as \halign. Then the above means \let\myhalign\let\mylb\\...\myhalign which is nested. So how does this nested block make \\ in the align* block work? – An5Drama Nov 19 '23 at 06:37