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 recently looked up J. van Hoorn's question on How to color delimiters in Math Mode, and so I decided to test my LaTeX knowledge by trying to define a new appropriate command: \leftcolor{color}<delim symbol> and \rightcolor{color}<delim symbol>.

The MWE is as follows:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
%
\newcommand*\leftcolor[2]{%
 \color{#1}\left#2\normalcolor%
}
\newcommand*\rightcolor[2]{%
 \color{#1}\right#2\normalcolor%
}
%
\begin{document}
\begin{equation*}
%
\leftcolor{red}(\frac{a}{b}\rightcolor{red})^n \neq \left(\frac{a}{b}\right)^n
%
\end{equation*}
\end{document}

The problem can be seen in the result shown below:

enter image description here

I clearly see that the exponent positioning is wrong, but I have no clue on how LaTeX puts that symbol in this way, because I followed the only option possible for the correct coloring (even after searching on the xcolor documentation I didn't find anything related to this topic).

My guess is that the effective size of the delimiter is "faked" in such a way that the compiler finds more correct to put the exponent as one normal size delimiter rather than the extended one.

I don't know if there is some plain TeX or LaTeX string to help about this situation, because the most common solution in these cases involves the making new delimiters such as:

\customdelim{\frac{a}{b}}^n   % these commands are not present in this case

The fact is that I don't request this kind of macro because I want to be able to switch directly from one color to another as a specific argument.

share|improve this question
    
I do not have access to a computer at the moment, but does it help to add \yphantom{\left{#2}} after \normalcolor (and the corresponding command for the right delimiter)? –  malin Jul 28 '13 at 20:20
    
@malin I tried the code suggested but it gives me an Undefined Control Sequence for \yphantom, and after trying \vphantom the ! Missing delimiter (. inserted) appears, I'm sorry but LaTeX doesn't want to end this so easily :) –  Andrea L. Jul 28 '13 at 20:25
1  
Typo error :-) does \vphantom{\left#2} work? –  malin Jul 28 '13 at 20:28
    
@malin I'm sorry but unfortunately not, as the errmsg is ! Missing delimiter (. inserted). –  Andrea L. Jul 28 '13 at 20:29
1  
@malin The usage of a specific delimiter, in this case ( in the preamble, gives the same error as before. Maybe I fonud one of DEK allowance valuable bugs :-) –  Andrea L. Jul 28 '13 at 20:41

3 Answers 3

up vote 8 down vote accepted

The macros \left and \right put their content in its own group.

You will notice this in the following example if you leave out the \normalcolor in the \rightcolor macro:

\leftcolor{blue}( \frac{a}{b} \rightcolor{red})^n

The superscript n (and everything following it in the same group) turns blue!

The internal macro \set@color that is called by \color as well as \normalcolor contains a \aftergroup\reset@color which gets called after the group of \left and \right. (So the problem is the \normalcolor in \leftcolor and the \color in \rightcolor.) The following ^n doesn’t see the right math atom to get correctly aligned.

Adding another group around it reverses this affect and ^2 is aligned correctly (see the fractions 4 and 5). But the fact that \left and \right is an Inner Atom is destroyed hereby, too, as {} construct an Ordinary Atom.

TeXbook, page 155:

There's also an eighth classication, \mathinner, which is not normally used for individual symbols; fractions and \left...\right constructions are treated as “inner” subformulas, which means that they will be surrounded by additional space in certain circumstances.

The correct usage of re-colored delimiters would need instead of the second group a \mathinner to get the correct spacing.

I have implemented this in the \p macro that takes an additional optional argument if you want to use different colors for the delimiters.

The difference in spacing is shown with the a.

Although it has been said on the linked question, I want to repeat the fact that \normalcolor does not switch back to the current color before the \left but to the default color. A \color{green} a \p{red}(){b} c will still show a black b and a green c.

Code

\documentclass{article}
\usepackage{mathtools}
\usepackage{xcolor}
\makeatletter
\newcommand*{\p}[5][]{%
  \edef\@tempa{#1}%
  \ifx\@tempa\@empty
    \edef\@tempa{#2}%
  \fi
  \mathinner{\begingroup\color{\@tempa}\left#3\normalcolor#5\color{#2}\right#4\endgroup}
}
\makeatother
\newcommand*\leftcolor[2]{%
 \color{#1}\left#2\normalcolor%
}
\newcommand*\rightcolor[2]{%
 \color{#1}\right#2%
}

\begin{document}
\begin{gather*}
a \p{red}(){\frac{a}{b}}^n \\
a \p[blue]{red}(){\frac{a}{b}}^n \\ 
%
a \left(\frac{a}{b}\right)^n \\
%
%
a \mathinner{{\leftcolor{blue}( \frac{a}{b} \rightcolor{red})}}^n \\
a \leftcolor{blue}( \frac{a}{b} \rightcolor{red})^n \\ % the 'n' is blue not red!
a {{\leftcolor{blue}( \frac{a}{b} \rightcolor{red})}}^n \\ % the spacing is bad
%
a \left(\frac{a}{b}\right)^n
\end{gather*}
\end{document}

Output

enter image description here enter image description here

share|improve this answer
    
Thanks, now it works like a charm. Oh but for a last thing: should I use inside the \newcommand* \bgroup\bgroup and \egroup\egroup to obtain the same result in your code? (Maybe it's a silly question, but it will be clear this issue once and for all). –  Andrea L. Jul 28 '13 at 21:19
    
@AndreaL. What do mean? {} is the same as \bgroup\egroup. You can use both. By the way, you can define \p so that it uses two different colors if you specify two (say with an optional argument). –  Qrrbrbirlbel Jul 28 '13 at 21:29
    
it would be better to use a \begingroup\endgroup group instead of (two) {} groups, to avoid making a mathord –  David Carlisle Jul 28 '13 at 21:41
    
That definitely cleared my issue, sorry for the last comment but even if it may have seemed obivous it has resolved my problem. Thanks again –  Andrea L. Jul 28 '13 at 21:42
2  
@Qrrbrbirlbel: \left and \right implicitly put the formula including fences in a \mathinner. This causes a different spacing, if symbols are before or after the formula. Instead of the two curly braces (or \bgroup/\egroup) that create two nested \mathord formulas, I would use \mathinner{\begingroup ...\endgroup}. –  Heiko Oberdiek Jul 28 '13 at 22:08

For comparison, ConTeXt provides a \definemathfence command for defining colored delimiters. math-fen.mkiv predefines a few math fences:

\definemathfence [parenthesis] [\c!left=0x28,\c!right=0x29]
\definemathfence [bracket]     [\c!left=0x5B,\c!right=0x5D]
\definemathfence [braces]      [\c!left=0x7B,\c!right=0x7D]
\definemathfence [bar]         [\c!left=0x7C,\c!right=0x7C]
\definemathfence [doublebar]   [\c!left=0x2016,\c!right=0x2016]
\definemathfence [angle]       [\c!left=0x3C,\c!right=0x3E]

which you can use using \fenced[parenthesis]{ \frac {a}{b} }. To add color, overload one of the predefined fences (or just start from scratch), and add a color attribute as well. For example:

\definemathfence[colorbracket][parenthesis][color=red, command=yes]

The command=yes part defines a command \colorbracket that can be used in place of \fenced[colorbracket]. Complete example:

\definemathfence[colorbracket][parenthesis][color=red, command=yes]

\starttext

\startformula
  \colorbracket{\frac {a}{b}}^n
  \qquad
  \left( \frac {a}{b} \right)^n
\stopformula

\stoptext

which gives

enter image description here

Like all ConTeXt \define.... commands, you can use \setup... command to change the behavior. For example, to change the color from red to blue, you can use:

\setupmathfence[colorbracket][color=blue]

This command does not support different colors for left and right delimiters. For implementation, see math-fen.mkiv.

share|improve this answer
    
When I'll start to look for other typesetting engines like LuaTeX or ConTeX this answer will be very useful (thanks), but now I'd like to put a definitive end to this issue on pdfLaTeX. –  Andrea L. Jul 29 '13 at 10:12

After considering the useful comments above (I never imagined that people like Heiko Oberdiek would spend their time on my only second question!), I added the implementation of the case where the argument of color is expanded from \@empty to the standard black.

The final MWE is as follows:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{xcolor}
%
\makeatletter
%
\newcommand\leftcolor[2]{%
  \edef\@tempa{#1}%
  \ifx\@tempa\@empty
    \edef\@tempa{black}%
  \fi
 \mathinner\bgroup\begingroup\color{\@tempa}\left#2\normalcolor%
}
\newcommand\rightcolor[2]{%
  \edef\@tempa{#1}%
  \ifx\@tempa\@empty
    \edef\@tempa{black}%
  \fi
  \color{\@tempa}\right#2\endgroup\egroup%
}
\makeatother
%
\begin{document}
%
\begin{gather*}
a\leftcolor{red}(\frac{a}{b} \rightcolor{red})^n \left(\frac{a}{a}\right)^n \\
a\leftcolor{}(\frac{a}{b}\rightcolor{})^n \left(\frac{a}{a}\right)^n \\
a\leftcolor{red}(\frac{a}{b}\rightcolor{blue})^n \left(\frac{a}{a}\right)^n \\
\end{gather*}
%
\end{document}

And the final output is now shown correct:

enter image description here

I also noticed that the function of \mathinner\begingroup\bgroup and \endgroup\egroup in each definition makes the code working as \mathinner{{<argument>}}^n, but inside my package LyXbasic I'll keep the command that Qrrbrbirlbel suggested, because the syntax is equally fluid with:

\p{<left delim color>}{<right delim color>}{<left delim>}{<right delim>}{<argument>}
share|improve this answer

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.