Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to patch the newline command \\ in an align environment provided by amsmath. The patched version should execute some custom code and finally call the original definition, like in the following example.

\documentclass{minimal}
\usepackage{amsmath}
\begin{document}
    \begin{align}
        \let\old\\%
        \def\\{1\old}%
        %
        a\\
        b\\
        c
\end{align}
\end{document}

This results in: enter image description here

So it works only for the first call (the "b" is missing a "1"). Why is this and how can I make it work for all newlines?

share|improve this question
2  
align uses the same mechanism as tabular and all cells are implicit groups, so that a local definition doesn't survive the cell in which it's given. Can you please give more motivation for what you're trying to do? –  egreg Jun 1 '12 at 22:15
    
@egreg: In the end, I want to prepend a \notag command to the newline if the line has not been referenced. The idea is to extend the autonum package to more advanced environments (as align). –  Patrick Häcker Jun 1 '12 at 22:23
    
Adding \global before the \let and the \def does not solve the problem. Then an error about the exceeded memory capacity of TeX is thrown. I was just guessing anyways ... –  Patrick Häcker Jun 1 '12 at 22:27
add comment

2 Answers

up vote 1 down vote accepted

Although the ideas in Mico's comment probably do work, they would be very specific to one environment and subject to break if anything related changes. I was heading for a more general solution.

There are two problems:

  1. As egreg points out, every line is an implicit group, so that all local definitions get deleted before the next line.
  2. Every amsmath environment is executed twice, first for measuring its size and then for printing.

Because of 1., \global has to be used. Because of 2., every direct \let followed by a \def of the same command will lead to an infinite recursion in the second pass, because the \let then points to itself, so the \gdef reaches towards an infity.

With this knowledge, a fix is simple. The recursion has to be avoided by only executing the commands in the first pass. Thus, this is solution:

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{etoolbox}

\begin{document}
    \begin{align}
        \ifundef{\old}{%
            \global\let\old\\%
            \gdef\\{1\old}%
        }{}
        %
        a\\
        b\\
        c
    \end{align}
\end{document}

result

share|improve this answer
add comment

You mention in a comment that your goal is to automatically affix a \notag instruction to all equations that aren't cross-referenced in the document. To achieve this objective, you could load the mathtools package and issue the command

\mathtoolsset{showonlyrefs}

in the preamble.

share|improve this answer
    
Thanks a lot for the answer. What I did not state in my question was that I exactly want to overcome some deficiencies of the mathtools package in that regard. So I am really interested in some information or tip about how to patch the newline in an align environment. –  Patrick Häcker Jun 2 '12 at 8:07
    
@MMM -- Take a look at the file amsmath.sty (if you haven't already done so). There are lots and lots of definitions of the form \math@cr, \math@cr@, \math@cr@@, \math@cr@@@, etc., and the various amsmath equation-related environments make use of these macros rather than of \` directly. (A reason for this, I surmise, is that amsmath` measures the width of any equation line first before typesetting it, so as to decide whether the associated equation number should be set next to or below the equation.) You'll probably need to change these macros to achieve your goal. –  Mico Jun 2 '12 at 11:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.