1

I use the following code to draw several tikzpictures, sharing the same node definition specifications.

How can I make these node definition specifications global instead of repeating them for each tikzpicture (with possible changing the position of begin point at (.1,.2))

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=.9, transform shape]
\draw [thick,-latex](0,0) -- (8,0);
\draw [thick,-latex](0,0) -- (0,3.);
\node [red] at (.1,.2) (begin){}
([shift={(-90.:1cm)}]begin.center) node (beginclip){}
([shift={(0.:.2cm)}]begin.center) node (beginr){}
([shift={(30:4.cm)}]beginr.center) node (midtop){}
([shift={(-30:4.cm)}]midtop.center) node (end){}
([shift={(0.:.2cm)}]end.center) node (endr){}
([shift={(-90.:1cm)}]endr.center) node (endrclip){}
([shift={(0:.5cm)}]beginr.center) node (arrowl){}
([shift={(180:.5cm)}]end.center) node (arrowr){}
;
\draw[blue, very thick] (begin.center) to [out=0,in=180, looseness=1.] (beginr.center) to [out=0,in=180, looseness=.68] (midtop.center) to [out=0,in=180, looseness=.68] (end.center) to [out=0,in=180, looseness=1.] (endr.center);
\path[clip] (beginclip.center) -- (begin.center) to [out=0,in=180, looseness=1.] (beginr.center) to [out=0,in=180, looseness=.68] (midtop.center) to [out=0,in=180, looseness=.68] (end.center) to [out=0,in=180, looseness=1.] (endr.center) -- (endrclip.center) -- cycle;
\pgfmathsetmacro{\DeltaX}{1.6}
\draw[blue, very thick] (3.76-\DeltaX,1.8) -- (3.76-\DeltaX,0) (3.76+\DeltaX,1.8) -- (3.76+\DeltaX,0);
\draw [blue, very thick] (3.76-\DeltaX,0.1) -- (3.76-\DeltaX,-0.1) node [blue, xshift=0cm, yshift=-.24cm] {-3.182}
 (3.76+\DeltaX,0.1) -- (3.76+\DeltaX,-0.1) node [blue, xshift=0cm, yshift=-.24cm] {3.182};
\draw [ultra thick, blue] (midtop.center) node [blue, fill=blue!8, xshift=0cm, yshift=-1.2cm] {accept Ho} (midtop.center|-0,0) node [blue, xshift=0cm, yshift=0cm] {X};
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

1 Answer 1

3

You could save these things in a pic, which also accepts parameters. Of course, you may choose more, less and/or different parameters.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\tikzset{pics/.cd,
Hany/.style n args={3}{code={\draw [thick,-latex](0,0) -- (0,3.);
\node [red] at (.1,.2) (begin){}
([shift={(-90.:1cm)}]begin.center) node (beginclip){}
([shift={(0.:.2cm)}]begin.center) node (beginr){}
([shift={(30:4.cm)}]beginr.center) node (midtop){}
([shift={(-30:4.cm)}]midtop.center) node (end){}
([shift={(0.:.2cm)}]end.center) node (endr){}
([shift={(-90.:1cm)}]endr.center) node (endrclip){}
([shift={(0:.5cm)}]beginr.center) node (arrowl){}
([shift={(180:.5cm)}]end.center) node (arrowr){}
;
\draw[blue, very thick] (begin.center) to [out=0,in=180, looseness=1.] (beginr.center) to [out=0,in=180, looseness=.68] (midtop.center) to [out=0,in=180, looseness=.68] (end.center) to [out=0,in=180, looseness=1.] (endr.center);
\path[clip] (beginclip.center) -- (begin.center) to [out=0,in=180, looseness=1.] (beginr.center) to [out=0,in=180, looseness=.68] (midtop.center) to [out=0,in=180, looseness=.68] (end.center) to [out=0,in=180, looseness=1.] (endr.center) -- (endrclip.center) -- cycle;
\draw[blue, very thick] (3.76-#1,1.8) -- (3.76-#1,0) (3.76+#1,1.8) -- (3.76+#1,0);
\draw [blue, very thick] (3.76-#1,0.1) -- (3.76-#1,-0.1) node [blue, xshift=0cm, yshift=-.24cm] {-3.182}
 (3.76+#1,0.1) -- (3.76+#1,-0.1) node [blue, xshift=0cm, yshift=-.24cm] {3.182};
\draw [ultra thick, blue] (midtop.center) node [blue, fill=blue!8, xshift=0cm,
yshift=-1.2cm] {#2} (midtop.center|-0,0) node [blue, xshift=0cm, yshift=0cm] {#3};
}}}

\begin{document}
\begin{frame}[t]
\frametitle{Parameters \texttt{{1.6},{accept Ho},{X}}}
\begin{tikzpicture}[scale=.9, transform shape]
\pic at (-3,0) {Hany={1.6}{accept Ho}{X}};
\end{tikzpicture}
\end{frame}

\begin{frame}[t]
\frametitle{Parameters \texttt{{1.4},{accept He},{Y}}}
\begin{tikzpicture}[scale=.9, transform shape]
\pic at (-3,0) {Hany={1.4}{accept He}{Y}};
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

4
  • Thank you very much. Can the pic definition include only the node specifications without including the curves; because there are many variable curves drawn with many other lines and text.
    – Hany
    Nov 2, 2018 at 5:47
  • 1
    @Hany Of course you can make virtually anything a pic (as long as it is compilable). Just add or remove whatever you find appropriate.
    – user121799
    Nov 2, 2018 at 5:49
  • @ marmot Thank you very much for your time.
    – Hany
    Nov 2, 2018 at 8:49
  • @ marmot Thank you for your comment, I figured out how to do that.
    – Hany
    Nov 2, 2018 at 9:03

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.