# Labeling non axis parallel braces in tikz

Given two points, the following code draws a brace connecting them and labels it.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (4,0);
\draw[decorate,decoration={brace,amplitude=10pt,raise=1pt,mirror},yshift=0pt] (a) -- (b) node [midway,yshift=-15pt]{$x$};
\fill (a) circle (2pt);
\fill (b) circle (2pt);
\end{tikzpicture}
\end{document}


This yields: However, if the line connecting a and b is not axis-parallel, we can have something like the following:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (4,2);
\draw[decorate,decoration={brace,amplitude=10pt,raise=1pt,mirror},yshift=0pt] (a) -- (b) node [midway,yshift=-15pt]{$x$};
\fill (a) circle (2pt);
\fill (b) circle (2pt);
\end{tikzpicture}
\end{document}


which yields: How can the x label be automatically located in this case in the "right" place? If there's no automatic way, I guess one has to use a combination of xshift and yshift and take into account the raise and amplitude of the brace. When I tried to tweak these I failed... What's the smart way out here?

For this, the auto placement option is a good approach: It takes the slope of the line into account when placing the node, making sure it doesn't overlap the straight line connecting the start and end points. If it's on the wrong side of the line, you can add the swap option. In the case of the brace decoration, you'll also want to move the node away from the line by setting a larger value for the outer sep:

\draw [
decorate, decoration={brace,amplitude=10pt,raise=1pt,mirror}
] (a) -- (b) node [
midway, auto, swap, outer sep=10pt
]{$x$}; However, as you can see, the placement isn't ideal yet. That's because the auto placement option only positions the node in 45° steps: east, south east, south, etc. We can "hack" a little internal macro to make the placement work at 1° steps, though. The anchor that is used for a node placed using the auto option is determined by a macro called \tikz@auto@anchor (\tikz@auto@anchor when the swap option is used), and at the time when the script is called, the \pgf@x and \pgf@y hold the x and y coordinates of a vector pointing in the direction of the line. Putting the following (taken from one of my earlier answers) into the preamble:

\makeatletter
\def\tikz@auto@anchor{%
\pgfmathtruncatemacro\angle{atan2(\pgf@y,\pgf@x)-90}
\edef\tikz@anchor{\angle}%
}

\def\tikz@auto@anchor@prime{%
\pgfmathtruncatemacro\angle{atan2(\pgf@y,\pgf@x)+90}
\edef\tikz@anchor{\angle}%
}
\makeatother


we get Full code:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.pathreplacing}
\makeatletter
\def\tikz@auto@anchor{%
\pgfmathtruncatemacro\angle{atan2(\pgf@y,\pgf@x)-90}
\edef\tikz@anchor{\angle}%
}

\def\tikz@auto@anchor@prime{%
\pgfmathtruncatemacro\angle{atan2(\pgf@y,\pgf@x)+90}
\edef\tikz@anchor{\angle}%
}
\makeatother

\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (4,2);
\draw [
decorate, decoration={brace,amplitude=10pt,raise=1pt,mirror}
] (a) -- (b) node [
midway, auto, swap, outer sep=10pt
]{$x$};
\fill (a) circle (2pt);
\fill (b) circle (2pt);
\end{tikzpicture}
\end{document}

• Amazing! Thanks! Can you add a short explanation what's the difference between the @anchor and the @anchor@prime? When which is used? – Dror Jan 31 '13 at 21:28
• @Dror: Glad it helps! @anchor@prime is used when the swap option is active. – Jake Jan 31 '13 at 21:31