0

I want to make a conditional macro. I currently use the line

\def\[#1\]{\begin{align*}#1\end{align*}}

since I often use align, and don't want to have to type \begin{align*} ... \end{align*} every time. (It's a tough life.) However, when using \qedhere (which is from an AMS package...?), it puts the QED symbol in the wrong place: it overlaps some of the text, rather than flush to the right, eg writing

\[ a = b \qedhere \].

If I place a single & somewhere, then this does not happen, eg writing

\[ & a = b \qedhere \].

However, I quite often forget this, and it's a bit annoying to have to do. (Again, life is so hard...)

Is there a way that I can make the following definition?

if #1 includes a & sign, then define \[#1\] as {\begin{align*}#1\end{align*}}
otherwise, define \[#1\] as {\begin{equation*}#1\end{equation*}}

I'm using the following packages: amsmath, amsthm, amssymb, mathrsfs, xifthen.

6
  • 1
    Have you thought about using an editor with autocomplete features? There are a few more differences between equation* and align* than the & (namely multi-line equations and the space before if the line before is short), so maybe it should be a deliberate choice. The align* environment also has a special implementation that means that it does not always work like normal environments, you can sometimes see this in error messages and from the fact that you can't define abbreviations such as \ba and \ea for \begin{align} and \end{align} in the simple way.
    – moewe
    Aug 14, 2018 at 11:57
  • @moewe Yeah, sure. I use TeXstudio, which does have autocompletion features. For example, Ctrl+Alt+Space writes \begin{, and Alt+Enter ends the current environment (eg writing \end{align*}). Sometimes I want to change a long single line to two lines, or maybe two short lines to one, and don't want to have to change equation to align.
    – Sam OT
    Aug 14, 2018 at 14:35
  • 1
    to change a long equation to two lines, you should use multline not align unless you are adding alignment points (...&=...) so a macro that hides which multi-line display is in use will do more harm than good. Aug 14, 2018 at 14:39
  • My apologies for not being clearer. I did indeed mean that not only do I put it on two lines, but use the alignment &. For example, I may have (one) = (two) = (three), and change this to (one) &= (two) \\&= (three)
    – Sam OT
    Aug 14, 2018 at 15:09
  • It is known that \qedhere doesn't work in align* when on a line without any &. For a single equation you should use equation*, not align*. Changing \[..\] to do align* in every situation is wrong and, of course, unsupported.
    – egreg
    Aug 14, 2018 at 16:30

1 Answer 1

4

I don't think this is a good idea, you should perhaps just input your stuff correctly. But the following does what you wanted:

\documentclass[]{article}

\usepackage[]{amsmath}

\def\IfAmpersandUseAlign#1#2&#3\EndIfAmpersandUseAlign%
  {%
    \if\relax\detokenize{#3}\relax
      \begin{equation*}%
        #1%
      \end{equation*}%
    \else
      \begin{align*}%
        #1%
      \end{align*}%
    \fi
  }
\def\[#1\]%
  {%
    \IfAmpersandUseAlign{#1}#1&\EndIfAmpersandUseAlign
  }

\begin{document}
\[
  This &= is + a + bad + idea\\
       &=really + it + is
\]
\[
  This = is + a + bad + idea\\
       =really + it + is
\]
\end{document}

Output (as you can see, the second is not linebroken, as it is not inside an align*):

enter image description here

6
  • Thank you, that does indeed work with the \qedhere symbol. I've edited your answer ever-so-slightly to use the proof environment with \qedhere (and amsthm); I hope this is ok. (It's in a review queue at the moment, in case you're thinking "no he hasn't"!)
    – Sam OT
    Aug 14, 2018 at 11:49
  • May I ask, though, why you feel it is such a bad idea?
    – Sam OT
    Aug 14, 2018 at 11:51
  • 1
    @SamT see for example the second example here, it has \\ but is on one line as there is no & so it is set as an equation not an alignment. A multiline equation with no alignment should be using gather not equation or align Aug 14, 2018 at 13:12
  • @DavidCarlisle Sure, yeah, I use TeXstudio, and it has some autocompletion. I was just looking for an even more streamlined version!
    – Sam OT
    Aug 14, 2018 at 14:33
  • 1
    @SamT apart from the details of this specific definition (which seems a bad idea) redefining a core standard latex command like \[ is never a good idea, it moves your document aways from standard latex, so will confuse any syntax highlighters (as found in this site) or make it hard to combine fragments from your document with other people's documents etc. Aug 14, 2018 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .