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.

This is slightly modified code from Frédéric's answer to How can I add clips above matrix?

\documentclass{mwrep}   
\usepackage{amsmath}    
\usepackage{tikz}
\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}
\usetikzlibrary{decorations.pathreplacing}  
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi.
\begin{equation}
\bar{P}=
\bordermatrix{ \mathrm{state} &   \tikzmark{left}{1} &   \tikzmark{right}{2}  \cr
            1 & 0.3 & 0.1  \cr
            2 & 0.3 & 0.2}
\end{equation}
\tikz[overlay,remember picture]{\draw[decoration=brace,decorate,blue] (left.north west) -- (right.north east) node[midway,above=15pt] {$S_1$};}
\end{document}

As you can see, the blue part is drawn on the text. This happens because LaTeX don't count it into size of equation. How can I do this?
I set above=15pt on purpose so the effect is more visible.

share|improve this question
    
Dirty trick will be to add some space after the sentence say like .....mi.\\[5pt] . Change the number in [5pt] suitably. –  Harish Kumar Mar 16 '12 at 8:07
    
@HarishKumar: Or alternatively, just change the above=15pt to above=0pt (at least in this case). –  Peter Grill Mar 16 '12 at 15:26
add comment

2 Answers

up vote 7 down vote accepted

(I feel as though I ought to behave like the host in one of those house makeover shows where they go round looking at all the shoddy work that's been done before and tutting loudly saying things like "Who told you to put the kitchen sink in the bedroom?")

The problem is that you want conflicting things here. When the brace and its label are placed then you want TeX to ignore their size. But when the equation is placed, then you magically want TeX to notice them again ("Oh! Was that there all along? Silly me for not noticing it before."). It may be possible, but it would take someone more knowledgeable than me in the Ways of TeX to figure it out.

What I would do in this circumstance is actually to typeset the whole thing using a single TikZ picture and thus avoid this problem altogether. However, as there may be situations where that isn't possible, I'll present a solution to this issue here. It's a hack, most definitely. What it does is to add a new element to the equation of zero width and the right height. We do this after the matrix and all its gubbins have been constructed so it doesn't affect that (I did try doing it inside the matrix but that messed that up). The new element is a TikZ path. It needs to be constructed from TikZ as we want to refer to the label on the brace. As we want TeX to notice the size of this element, we can't use the overlay option, but as we want to refer to stuff from other diagrams, we have to use the remember picture option. Using remember picture without the overlay option is Extremely Dangerous as it can seriously mess with bounding boxes (it's possible to get into the situation where the bounding box grows and grows and grows with each new run of tex). To avoid this, we only use coordinates from existing pictures.

The magic command is:

\tikz[remember picture,inner sep=0pt,baseline=0pt] \path (label.north);

The key remember picture means that TikZ makes a note of the position of the current tikzpicture. This means that it can compute the relative displacement of coordinates in other remembered pictures and so the coordinate specification (label.north) really does point to the place on the page specified by the top of the label (I added the name to the $S_1$ node). However, it is relative to the current picture. That is, it is relative to the origin in the current picture. That would be fine, except for the fact that when TikZ hands the picture over to TeX for placing on the page, TeX doesn't know about the internal coordinates but only about the bounding box. Since we only refer to the coordinate (label.north), TeX thinks that this picture consists of one tiny dot and places it as such (you can see this by replacing \path (label.north); by something more complicated such as \draw (label.south) -- (label.north); the line is of the right height, but is displaced to the current position). To get round that, we add the baseline=0pt option which says that when TikZ hands the box to TeX, it also says "The box should be placed so that the internal coordinate (0,0) lies on the baseline of the current line". For us, that has the effect of shifting the diagram up so that (label.north) is now vertically where it should be. It is not, in matter of fact, vertically where it should be. Again, if you replace the \path command by a suitable \draw then you'll see that everything is displaced to the current horizontal position. But that's okay as it was only vertical displacement that was the issue here.

Here's the full code.

\documentclass{mwrep}   
\usepackage{amsmath}    
\usepackage{tikz}

\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}
\usetikzlibrary{decorations.pathreplacing}  
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi.
\begin{equation}
\bar{P}=
\bordermatrix{ \mathrm{state} &   \tikzmark{left}{1} &   \tikzmark{right}{2}  \cr
            1 & 0.3 & 0.1  \cr
            2 & 0.3 & 0.2}
\tikz[overlay,remember picture]{\draw[decoration=brace,decorate,blue] (left.north west) -- (right.north east) node[midway,above=15pt] (label) {$S_1$};}
\tikz[remember picture,baseline=0pt] \path (label.north);
\end{equation}
\end{document}

As I warned, messing with remember picture without overlay can cause problems with bounding boxes. If stuff starts getting placed at strange places on the page, delete the .aux file. That resets everything.

Here's the result:

Readjusting sizes of TikZmarked points

My experiments show that this solution does not add any horizontal space to the equation.

share|improve this answer
    
Will this help if I set submatrice at the very bottom of the matrix as in tex.stackexchange.com/questions/47765/… ? –  Ichibann Mar 16 '12 at 12:45
add comment

I think for simple braces David's solution is great and TikZ is an overkill. But if you plan to use more complicated stuff with TikZ, maybe you can spend a little extra typing cost to get a more precise control of your mathematical expression. In this case, I have tried to replicate the border matrix with a quick attempt.

\documentclass{mwrep}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{matrix,decorations.pathreplacing}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, 
suscipit a, scelerisque sed, lacinia in, mi.
\begin{equation}
\bar{P}= 
\begin{tikzpicture}[baseline = (M.center),% center with respect to the matrix center
        every left delimiter/.style={xshift=1ex},%tighter delimiter spacing
        every right delimiter/.style={xshift=-1ex}]
\matrix (M) [matrix of math nodes,left delimiter={[},right delimiter={]} 
        ]{ 
                              0.3 & 0.1 & 0.6 & 0.0 & 0.0 & 0.0 & 0.0 \\
                              0.2 & 0.1 & 0.7 & 0.0 & 0.0 & 0.0 & 0.0 \\
                              0.3 & 0.4 & 0.3 & 0.0 & 0.0 & 0.0 & 0.0 \\
                              0.0 & 0.0 & 0.0 & 0.2 & 0.8 & 0.0 & 0.0 \\
                              0.0 & 0.0 & 0.0 & 0.3 & 0.7 & 0.0 & 0.0 \\
                              0.0 & 0.4 & 0.1 & 0.0 & 0.0 & 0.2 & 0.3 \\
                              0.0 & 0.0 & 0.1 & 0.0 & 0.3 & 0.1 & 0.5 \\
};
\node[anchor=south east] (cornernode) at (M-1-1.north west) {state}; %Position this more 
                                                                     %precisely if desired
\foreach[count=\xi] \x in {1,2,4,3,6,5,7}{ %\xi is the counter \x is the value
\node (M-\xi-0) at (cornernode |- M-\xi-1) {\x}; %Gets the left most column
\node (M-0-\xi) at (cornernode -| M-1-\xi) {\x}; % Gets the top row 
}
\draw[decoration=brace,decorate,blue] (M-0-1.north west) -- (M-0-3.north east)%
 node[midway,above] {$S_1$};
\draw[decoration=brace,decorate,blue] (M-0-4.north west) -- (M-0-5.north east)%
 node[midway,above] {$S_2$};
\end{tikzpicture}
\end{equation}
\end{document}

enter image description here

share|improve this answer
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.