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 would like to have a macro that allows for alignment points inside it. As a specific example, I would like to be able to say:

\begin{align*}
         a &= b\\
  \bracr{c &= d} 
\end{align*}

where \bracr places a round brackets around the argument.

This command would parse through the tokens until an alignment character is encountered. At this point it would insert a \right., add the alignment point, and then start a \left.

I realize that this example seems kind of trivial to do manually, but for now if I could just have a command such as the above brace that converts:

\bracr{c &= d}

to:

\left( c \right. & \left. = d\right)

Ultimately, I would like to add a \vphantom{c = d} to both sides of the equation to get the correct size brackets. I want to do a lot more with this, but need some help to get started as I don't know how to parse through the characters in #1.

Below is a code sample. Basically, I would like to uncomment out the lines below and have it be equivalent to the lines above (with approriate changes to the macro).

Another example, is where I want to color the particular formula as below.

\documentclass{article}
\usepackage{amsmath}
\usepackage{color}

\newcommand{\bracr}[1]{\left(  #1 \right)}    % not quite what is needed
\newcommand{\makeRed}[1]{\textcolor{red}{#1}} % also not quite as versatile as desired

\begin{document}
\begin{align*}
         a         &=        b \\
  \left( c \right. &= \left. d \right)\\
%\bracr{ c         &=        d }\\  % would prefer something like this instead
 \makeRed{c}&\makeRed{= d}
%\makeRed{c &= d} % would prefer something like this instead
\end{align*}

\end{document}

Clarification: I would like this to be able to work independent of how many align points are provided.

share|improve this question
    
Here's a related previous question: Highlight an equation within an align environment. –  Hendrik Vogt Apr 3 '11 at 9:11
add comment

2 Answers

up vote 4 down vote accepted

Why not use the simpler

\def\BRACKETS#1#2%
    {\left(\vphantom{#1#2}\right.#1%
     &%
     #2\left.\vphantom{#1#2}\right)}

with

\begin{align*}
  a &= b\\
  \BRACKETS{c}{= d} 
\end{align*}

Notice that I changed the definition (from what you had suggested) to get correct spacing.

share|improve this answer
    
While this works great for this particular problem, its not quite general enough of a solution for me. I don't like to have to adjust from \BRACKETS{c = d} to the above just because I added an align point. And if the equation(s) are more complicated and I have more than one align point things get even more complicated. –  Peter Grill Apr 3 '11 at 21:57
    
@Peter Grill: I agree that the syntax is a matter of personal style. However, I would argue that the above approach is more flexible, especially if you have more than one align point. It is easy to create a command that takes multiple optional arguments in braces (\dosinglegroupempty etc in ConTeXt; I don't know the equivalent in LaTeX) or in brackets (\dosingleempty etc in ConTeXt; \newcommand and variants in LaTeX). To parse multiple &, you need to write a recursive macro; if you want to use all the terms together, then you will need to store each of them in a helper macro. –  Aditya Apr 4 '11 at 17:31
add comment

I think that this does what you want and it should get the spacing right. It's very similar to Aditya's answer but the macro usage is what you suggested and it uses \mathopen and \mathclose to produce proper spacing around the parentheses.

\documentclass{article}
\usepackage{amsmath}

\newcommand*\bracr[1]{\bracrhelper#1\bracrhelper}
\def\bracrhelper#1&#2\bracrhelper{%
        \mathopen{\left(\vphantom{#1#2}\right.}#1%
        &#2\mathclose{\left.\vphantom{#1#2}\right)}%
}

\begin{document}
\begin{align*}
        a &= b\\
 \bracr{\frac1c &= d}
\end{align*}
\end{document}

To explain what's going on here, the \bracr macro reads its argument and then "passes" it to the \bracrhelper macro which uses delimited arguments. The first argument is everything up to the &. The second argument is everything up to the \bracrhelper token. Thus, #1 is the left hand side and #2 is the right hand side. Then the two \vphantoms construct parentheses of the appropriate heights.

share|improve this answer
    
This looks interesting (works great for this particular problem), but I don't quite understand \mathopen and \mathclose (difficult to find documentation on these - Even the TeXbook only briefly mentions these). So, how would I adjust the above for more than one align point? –  Peter Grill Apr 3 '11 at 22:04
    
@Peter: More than one align point would be putting parentheses around separate equations. I'm not really sure what you're doing here because I've never seen parentheses around an equation the way you're doing it, let alone around multiple. The most simple way would be to define multiple similar macros for the various cases since you're unlikely to need more than \foo{...&...&...&...&...&...} and there's going to be an odd number of &, so that leaves just two more. You could do it by splitting up the input into pieces by &, by that'd be a lot more work. –  TH. Apr 3 '11 at 22:11
    
@Peter: As for \mathopen and \mathclose, these are described in The TeXbook, but that's in my office right now. They're also described in TeX by Topic which is available for free. –  TH. Apr 3 '11 at 22:15
    
Yes, I realize that brackets may not be the best use, there are several things I want to be able to do and have added another example of where I would like to be able to color a particular equation. I guess I would prefer a solution that parses the #1 and splits is appropriately. –  Peter Grill Apr 3 '11 at 23:14
    
@Peter: Then Hendrik's link should be useful. –  TH. Apr 4 '11 at 0:07
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.