11

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}
20

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}
  • 1
    Another option is to use reverse the endpoints manually (and use a positive amplitude). – Charles Staats Mar 23 '13 at 0:00
  • @CharlesStaats: Hmmm.. Good point, but what's the fun in making things so simple... :-) – Peter Grill Mar 23 '13 at 0:19
  • @AlanH: The brace is drawn between the given coordinates. Try tweaking the coordinates -- perhaps the brace is too big, or too small in your test. If you add a MWE to your question than it would be easier to address why it is not working for you. Otherwise it is difficult to help you. – Peter Grill Apr 6 '13 at 3:25
8

With PSTricks.

enter image description here

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{pst-node,pst-plot}

\psset{nodesep=5pt,arm=7pt,linearc=5pt}

\def\V#1#2{\rput(#1,0){\psline(-0.5,1)(0,0)(0.5,1)\pnode(0,0){#2}}}

\def\Brace#1#2#3{%
    \pnode[0,-24pt](!\psGetNodeCenter{#1}\psGetNodeCenter{#2} #1.x #2.x add 2 div 0){center}
    \ncdiag[angleA=-80,angleB=90]{#1}{center}
    \ncdiag[angleA=90,angleB=-100]{center}{#2}
    \rput[t](center|0,-24pt){#3}
}

\begin{document}
\begin{pspicture}[showgrid=false](-0.5,-2)(10,2)
    \psaxes[labels=none]{->}(0,0)(-0.25,-1.5)(9.25,1.75)[time,0][Energy,90]
    \V{1}{A}\psline(A)(A|0,1)
    \V{3}{B}
    \Brace{A}{B}{$t$}
    \V{5}{C}
    \pnode(6,0){D}
    \Brace{C}{D}{$\frac{q}{r_s-r_d}$}
    \V{8}{E}
    \Brace{D}{E}{$\frac{q}{r_s}$}
\end{pspicture}
\end{document}

Animated version

enter image description here

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-node,pst-plot}

\def\V#1#2{\rput(#1,0){\psline(-0.5,1)(0,0)(0.5,1)\pnode(0,0){#2}}}

\def\Brace#1#2#3{%
    \pnode[0,-24pt](!\psGetNodeCenter{#1}\psGetNodeCenter{#2} #1.x #2.x add 2 div 0){center}
    \ncdiag[angleA=-80,angleB=90,nodesep=5pt,arm=7pt,linearc=5pt]{#1}{center}
    \ncdiag[angleA=90,angleB=-100,nodesep=5pt,arm=7pt,linearc=5pt]{center}{#2}
    \rput[t](center|0,-24pt){#3}
}
\usepackage{multido}

\def\Picture#1{%
\begin{pspicture}[showgrid=false](-0.5,-2)(10,2)
    \psaxes[labels=none]{->}(0,0)(-0.25,-1.5)(9.25,1.75)[time,0][Energy,90]
    \V{1}{A}\psline(A)(A|0,1)
    \V{3}{B}
    \Brace{A}{B}{$t$}
    \V{5}{C}
    \pnode(#1,0){D}
    \Brace{C}{D}{$\frac{q}{r_s-r_d}$}
    \V{8}{E}
    \Brace{D}{E}{$\frac{q}{r_s}$}
\end{pspicture}}

\begin{document}
\multido{\n=5.75+0.15}{10}{\Picture{\n}}
\multido{\n=6.95+-0.15}{8}{\Picture{\n}}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.