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.

What is the best way to draw braces between tick marks? It would be convenient to assign nodes at tick marks, is this doable?

The best method for placement that I have right now is a guess-and-check.

MWE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns,decorations.pathreplacing}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis x line = middle,
    axis y line = middle,
    xticklabels={}
    ]
\addplot [domain=-1:5] { -x * (x-4) };
\end{axis}
\draw [thick,decoration={brace,mirror},decorate] (1.2,3) -- (5.7,3) node[midway,below,yshift=-.1cm] {4};
\end{tikzpicture}

\end{document}

MWEpicture

share|improve this question
    
Update: In my actual implementation (not the MWE), my brace fell out of the graphing area and was clipped off. However, I did not want to use clip=false, since my graph would have extended out as well. To fix all this, I used after end axis/.code={\draw [decoration={brace},decorate] (A) -- (B) node {yadda}; in the argument of \begin{axis}, and \coordinate (A) at (axis cs:pi,0);, \coordinate (B) at (axis cs:5*pi/3,0); just before \end{axis}. I hope this helps anyone else who might run into the same situation! –  user2868156 May 14 at 6:51
    
(Special thanks to Jake's answer in Annotate Plots in TikZ/PGFplots.) –  user2868156 May 14 at 6:59

1 Answer 1

up vote 20 down vote accepted

Below I present three levels of improvement:

  1. You can move the \draw inside the axis environment and use the axis cs coordinate system which gives you access to the coordinates of the plot; also, you can use the raise option to shift the brace:

    \documentclass{article}
    \usepackage{pgfplots}
    \usetikzlibrary{patterns,decorations.pathreplacing}
    \pgfplotsset{compat=1.10}
    
    \begin{document}
    
    \begin{tikzpicture}
    \begin{axis}[
        axis x line = middle,
        axis y line = middle,
        xticklabels={}
        ]
    \addplot [domain=-1:5] { -x * (x-4) };
    \draw [thick,decoration={brace,mirror,raise=5pt},decorate] 
      (axis cs:0,0) --
        node[below=7pt] {4} 
      (axis cs:4,0);
    \end{axis}
    \end{tikzpicture}
    
    \end{document}
    

    output of solution 1

  2. You don't have to know beforehand the coordinates for the intersection points; you can let the intersections library do the calculations for you:

    \documentclass{article}
    \usepackage{pgfplots}
    \usetikzlibrary{patterns,decorations.pathreplacing,intersections}
    \pgfplotsset{compat=1.10}
    
    \begin{document}
    
    \begin{tikzpicture}
    \begin{axis}[
        axis x line = middle,
        axis y line = middle,
        xticklabels={},
        samples=100
        ]
    \addplot[name path=curve,domain=-1:5] { -x * (x-4) };
    \addplot[name path=line,domain=-1:5,forget plot] {0};
    \draw[
      name intersections={of=curve and line, by={a,b}},
      thick,
      decoration={brace,mirror,raise=5pt},decorate
    ] 
      (a) --
        node[below=7pt] {4} 
      (b);
    \end{axis}
    \end{tikzpicture}
    
    \end{document}
    
  3. Finally you can also use the calc library and the lengthconvert package, for example, so the length (4) is also automatically calculated:

    \documentclass{article}
    \usepackage{lengthconvert}
    \usepackage{pgfplots}
    \usetikzlibrary{patterns,decorations.pathreplacing,intersections,calc}
    \pgfplotsset{compat=1.10}
    
    \begin{document}
    
    \begin{tikzpicture}
    \begin{axis}[x=1cm,
        axis x line = middle,
        axis y line = middle,
        xticklabels={},
        samples=100
        ]
    \addplot[name path=curve,domain=-1:5] { -x * (x-4) };
    \addplot[name path=line,domain=-1:5,forget plot] {0};
    \draw[
      name intersections={of=curve and line, by={a,b}},
      decoration={brace,mirror,raise=5pt},
      decorate
    ]
      let \p1 = ($ (a) - (b) $),
          \n2 = {veclen(\x1,\y1)}
      in
      (a) --
        node[below=7pt] {\Convert[precision=1,number-only=true]{\n2}} 
      (b);
    \end{axis}
    \end{tikzpicture}
    
    \end{document}
    
share|improve this answer
    
Ah brilliant, thank you! –  user2868156 May 14 at 4:01

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.