[Tex/LaTex] Underbrace in Tikz

bracestikz-pgftikzlibrary

I'm trying to add an underbrace to a graph in tikz but when i compile run PDFLaTeX and View PDF, the underbrace doesn't look correct. Instead of curling down and inwards at the ends, it goes outwards and then a horizontal line inwards? I've tried to use other methods suggested on here but the only other one I found used snakes and didn't work.

\documentclass[a4paper,fleqn]{article}
\usepackage{amssymb,amsthm,tikz,fancyhdr,cancel,enumerate,array,booktabs,setspace,pgf,tikz,fancyhdr,graphicx,color}
\usepackage[T1]{fontenc}
\usetikzlibrary{patterns,arrows,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=0.6091370558375635cm,y=0.9589041095890412cm]
\draw[->,color=black] (-1,0) -- (18.7,0);
\foreach \x in {-1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
\draw[shift={(\x,0)},color=black] (0pt,-2pt);
\draw[color=black] (18.05,0.05) node [anchor=south west] { Time};
\draw[->,color=black] (0,-1) -- (0,6.3);
\foreach \y in {-1,1,2,3,4,5,6}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
\draw[color=black] (0.09,6.04) node [anchor=west] { Stock Level};
\clip(-1,-1) rectangle (18.7,6.3);
\draw (0,3)-- (3,0);
\draw (4.5,3)-- (3,0);
\draw (4.5,3)-- (7.5,0);
\draw (7.5,0)-- (9,3);
\draw (9,3)-- (12,0);
\draw (13.5,3)-- (12,0);
\draw (0,3)-- (5,3);
\draw (6.4,4.78) node[anchor=north west] {$gradient=r_s-r_d$};
\draw (10.22,4.76) node[anchor=north west] {$gradient=-r_d$};
\draw [->] (7.43,4.29) -- (8.66,2.32);
\draw [->] (11.01,4.26) -- (9.76,2.33);
\draw (-0.45,4.79) node[anchor=north west] {$Q$};
\draw (-0.5,3.36) node[anchor=north west] {$q$};
\draw (2.5,5)-- (7.5,0);
\draw (3,0)-- (3,5);
\draw (0,4.5)-- (3.5,4.5);
\draw (13.5,3)-- (16.5,0);
\draw (16.5,0)-- (18,3);
\draw (18,3)-- (21,0);
\draw [decorate,decoration={brace,amplitude=-10pt},xshift=0.4pt,yshift=-0.4pt](3,0) -- (7.5,0) node[black,midway,yshift=-0.6cm] {\footnotesize $t$};
\draw [decorate,decoration={brace,amplitude=-10pt},xshift=0.4pt,yshift=-0.4pt](12,0) -- (13.5,0) node[black,midway,yshift=-0.6cm] {\footnotesize $\frac{q}{r_s-r_d}$};
\draw [decorate,decoration={brace,amplitude=-10pt},xshift=0.4pt,yshift=-0.4pt](13.5,0) -- (16.5,0) node[black,midway,yshift=-0.6cm] {\footnotesize $\frac{q}{r_s}$};
\end{tikzpicture}
\end{document}

Best Answer

If you use a positive amplitude and the mirror option then you get the correct brace (color added):

enter image description here

Notes:

  • As pointed out by Charles Staats, another option would be to use positive lengths, and reverse the order in which the coordinates are specified -- then you don't need the mirror option.

Code:

\documentclass[a4paper,fleqn]{article}
\usepackage{amssymb,amsthm,tikz,fancyhdr,cancel,enumerate,array,booktabs,setspace,pgf,tikz,fancyhdr,graphicx,color}
\usepackage[T1]{fontenc}
\usetikzlibrary{patterns,arrows,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=0.6091370558375635cm,y=0.9589041095890412cm]
\draw[->,color=black] (-1,0) -- (18.7,0);
\foreach \x in {-1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
\draw[shift={(\x,0)},color=black] (0pt,-2pt);
\draw[color=black] (18.05,0.05) node [anchor=south west] { Time};
\draw[->,color=black] (0,-1) -- (0,6.3);
\foreach \y in {-1,1,2,3,4,5,6}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
\draw[color=black] (0.09,6.04) node [anchor=west] { Stock Level};
\clip(-1,-1) rectangle (18.7,6.3);
\draw (0,3)-- (3,0);
\draw (4.5,3)-- (3,0);
\draw (4.5,3)-- (7.5,0);
\draw (7.5,0)-- (9,3);
\draw (9,3)-- (12,0);
\draw (13.5,3)-- (12,0);
\draw (0,3)-- (5,3);
\draw (6.4,4.78) node[anchor=north west] {$gradient=r_s-r_d$};
\draw (10.22,4.76) node[anchor=north west] {$gradient=-r_d$};
\draw [->] (7.43,4.29) -- (8.66,2.32);
\draw [->] (11.01,4.26) -- (9.76,2.33);
\draw (-0.45,4.79) node[anchor=north west] {$Q$};
\draw (-0.5,3.36) node[anchor=north west] {$q$};
\draw (2.5,5)-- (7.5,0);
\draw (3,0)-- (3,5);
\draw (0,4.5)-- (3.5,4.5);
\draw (13.5,3)-- (16.5,0);
\draw (16.5,0)-- (18,3);
\draw (18,3)-- (21,0);
\draw [thick, red,decorate,decoration={brace,amplitude=10pt,mirror},xshift=0.4pt,yshift=-0.4pt](3,0) -- (7.5,0) node[black,midway,yshift=-0.6cm] {\footnotesize $t$};
\draw [thick, blue,decorate,decoration={brace,amplitude=5pt,mirror},xshift=0.4pt,yshift=-0.4pt](12,0) -- (13.5,0) node[black,midway,yshift=-0.6cm] {\footnotesize $\frac{q}{r_s-r_d}$};
\draw [thick, brown, decorate,decoration={brace,amplitude=10pt,mirror},xshift=0.4pt,yshift=-0.4pt](13.5,0) -- (16.5,0) node[black,midway,yshift=-0.6cm] {\footnotesize $\frac{q}{r_s}$};
\end{tikzpicture}
\end{document}
Related Question