1

Using the following code, how to automate the process of drawing from a specific x-axis point up to the intersection to a line, then to the y-axis.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape]
\pgfmathsetmacro{\incrmnty}{1.}
\pgfmathsetmacro{\incrmntx}{2.}
\pgfmathsetmacro{\xlab}{2006}
\pgfmathsetmacro{\xn}{5}
\pgfmathsetmacro{\yn}{5}
\begin{scope}[xshift=0cm, yshift=0cm]
\draw [thick](0,0) -- (\incrmntx*\xn+2,0);
\node at (\incrmntx*\xn+2,\incrmnty*\yn-.4) (endx){};% to compensate for the shift of the x-axis labels, and to draw short from the end of the y-axis 
\draw [thick](0,0) -- (0,\incrmnty*\yn);
\end{scope}
\begin{scope}[xshift=\incrmntx*.4cm, yshift=0cm]% to shift the x-axis labels
\foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0) node [black, xshift=0cm, yshift=-.68cm] {\footnotesize \aff} -- +(-90:.2);}
\end{scope}
\draw[blue!40!black, line width=.4mm] (-.1*\incrmntx,1.56*\incrmnty) -- (endx.center);
\draw[blue!80!white, thick, dashed] (\incrmntx*\xn+.8,0) --+(90:4.26cm) node (aa){} -| (aa.center-|0,0) node [black, xshift=-1cm, yshift=.06cm] {2011 value};
\draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty) -- (endx.center);
\draw[red!80!white, thick, dashed] (\incrmntx*\xn-3.2,0) --+(90:3.7cm) node (bb){} -| (bb.center-|0,0) node [black, xshift=-1cm, yshift=.06cm] {2009 value};
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

2

Assuming that the plots are not always straight lines, I recommend using the intersections library.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape]
 \pgfmathsetmacro{\incrmnty}{1.}
 \pgfmathsetmacro{\incrmntx}{2.}
 \pgfmathsetmacro{\xlab}{2006}
 \pgfmathsetmacro{\xn}{5}
 \pgfmathsetmacro{\yn}{5}
 \begin{scope}[xshift=0cm, yshift=0cm]
  \draw [thick](0,0) -- (\incrmntx*\xn+2,0);
  \node at (\incrmntx*\xn+2,\incrmnty*\yn-.4) (endx){};% to compensate for the shift of the x-axis labels, and to draw short from the end of the y-axis 
  \draw [thick](0,0) -- (0,\incrmnty*\yn);
 \end{scope}
 \begin{scope}[xshift=\incrmntx*.4cm, yshift=0cm]% to shift the x-axis labels
  \foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]
  in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0) 
  node [black, xshift=0cm, yshift=-.68cm,font=\footnotesize] {\aff} -- +(-90:.2);}
  \end{scope}
  \draw[blue!40!black, line width=.4mm,name path=blue plot] (-.1*\incrmntx,1.56*\incrmnty) -- (endx.center);
  \path[overlay,name path=vert-1] (\incrmntx*\xn+.8,0) -- ++(0,10);
  \draw[blue!80!white, thick, dashed,name intersections={of=blue plot and vert-1,
  by=i1}] (\incrmntx*\xn+.8,0) |- (i1 -|0,0) node [black, left] {2011 value};
  \draw[red!40!black, line width=.4mm,name path=red plot] (-.1*\incrmntx,2.5*\incrmnty) -- (endx.center);
  \path[overlay,name path=vert-2] (\incrmntx*\xn-3.2,0) -- ++(0,10);
  \draw[red!80!white, thick, dashed,name intersections={of=red plot and vert-2,
  by=i2}] (\incrmntx*\xn-3.2,0) |- (i2 -|0,0) 
  node [black,left] {2009 value};
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

If, however, you are dealing with straight lines, you could work with the calc library instead.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape]
 \pgfmathsetmacro{\incrmnty}{1.}
 \pgfmathsetmacro{\incrmntx}{2.}
 \pgfmathsetmacro{\xlab}{2006}
 \pgfmathsetmacro{\xn}{5}
 \pgfmathsetmacro{\yn}{5}
 \begin{scope}[xshift=0cm, yshift=0cm]
  \draw [thick](0,0) -- (\incrmntx*\xn+2,0);
  \node at (\incrmntx*\xn+2,\incrmnty*\yn-.4) (endx){};% to compensate for the shift of the x-axis labels, and to draw short from the end of the y-axis 
  \draw [thick](0,0) -- (0,\incrmnty*\yn);
 \end{scope}
 \begin{scope}[xshift=\incrmntx*.4cm, yshift=0cm]% to shift the x-axis labels
  \foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]
  in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0) 
  node [black, xshift=0cm, yshift=-.68cm,font=\footnotesize] {\aff} -- +(-90:.2);}
  \end{scope}
  \draw[blue!40!black, line width=.4mm] 
  (-.1*\incrmntx,1.56*\incrmnty) coordinate(blue-start) -- (endx.center);
  \path (intersection cs:first line={(blue-start) -- (endx.center)},
  second line={(\incrmntx*\xn+.8,0)--(\incrmntx*\xn+.8,10)}) coordinate (i1);
  \draw[blue!80!white, thick, dashed] (\incrmntx*\xn+.8,0) |- (i1 -|0,0) node [black, left] {2011 value};
  \draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty) 
  coordinate(red-start)-- (endx.center);
  \path (intersection cs:first line={(red-start) -- (endx.center)},
  second line={(\incrmntx*\xn-3.2,0)--(\incrmntx*\xn-3.2,10)}) coordinate (i2);
  \draw[red!80!white, thick, dashed] (\incrmntx*\xn-3.2,0) |- (i2 -|0,0) 
  node [black,left] {2009 value};
\end{tikzpicture}
\end{frame}
\end{document}

This leads to the same output.

The common strategy is to determine the intersections between the plots and vertical lines that start at the specified x coordinates. These intersections are called i1 and i2 in both examples. And then one can use them in

 \draw[blue!80!white, thick, dashed] (\incrmntx*\xn+.8,0) |- (i1 -|0,0) node [black, left] {2011 value};

and

  \draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty) 
  coordinate(red-start)-- (endx.center);

respectively, to draw the vertical and then horizontal lines and place the nodes left of them.

The calc example can be made a style.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{}
\begin{tikzpicture}[scale=.8, transform shape,-|/.style args={of #1 at #2}{%
insert path={(intersection cs:first line={#1},second line={(#2,0)--(#2,10)}) 
coordinate (aux) (#2,0) |- (0,0|-aux) }},vert/.style args={of #1 at #2}{%
insert path={(#2,0) 
-- (intersection cs:first line={#1},second line={(#2,0)--(#2,10)}) }}]
 \pgfmathsetmacro{\incrmnty}{1.}
 \pgfmathsetmacro{\incrmntx}{2.}
 \pgfmathsetmacro{\xlab}{2006}
 \pgfmathsetmacro{\xn}{5}
 \pgfmathsetmacro{\yn}{5}
 \begin{scope}[xshift=0cm, yshift=0cm]
  \draw [thick](0,0) -- (\incrmntx*\xn+2,0);
  \node at (\incrmntx*\xn+2,\incrmnty*\yn-.4) (endx){};% to compensate for the shift of the x-axis labels, and to draw short from the end of the y-axis 
  \draw [thick](0,0) -- (0,\incrmnty*\yn);
 \end{scope}
 \begin{scope}[xshift=\incrmntx*.4cm, yshift=0cm]% to shift the x-axis labels
  \foreach \x [evaluate=\x as \aff using int(\xlab+(1*\x))]
  in {0,...,\xn}{\draw [black!20!white] (\incrmntx*\x,0) 
  node [black, xshift=0cm, yshift=-.68cm,font=\footnotesize] {\aff} -- +(-90:.2);}
  \end{scope}
  \draw[blue!40!black, line width=.4mm] 
  (-.1*\incrmntx,1.56*\incrmnty) -- (endx.center);
  \draw[blue!80!white, thick, dashed,
  -|={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at {\incrmntx*\xn+.8}}]
  node [black, left] {2011 value};
  \draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty) -- (endx.center);  
  \draw[red!80!white, thick, dashed,
  -|={of {(-.1*\incrmntx,2.5*\incrmnty)--(endx.center)} at {\incrmntx*\xn-3.2}}]  
  node[black,left] {2009 value};
  \draw[violet!80!white, thick, dashed,
  vert={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at 
  {\incrmntx*\xn-2}}]; 
\end{tikzpicture}
\end{frame}
\end{document}

with which the whole story boils down to

\draw[red!80!white, thick, dashed,
  -|={of {(-.1*\incrmntx,2.5*\incrmnty)--(endx.center)} at {\incrmntx*\xn-3.2}}]  
  node[black,left] {2009 value};

This contains an additional style vert that can be used

\draw[violet!80!white, thick, dashed,
  vert={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at 
  {\incrmntx*\xn-2}}]; 

enter image description here

  • @marmotThank you very very much for your time for this very nice answer and the provided explanation. – Hany Feb 22 at 5:01
  • @Hany You're welcome. (I made the calc example a style which may simplify matters if you have many of those.) – marmot Feb 22 at 5:03
  • 1
    @marmotJust one comment in your edited answer, for the first line you mentioned coordinate(blue-start); but for the second line you did not say coordinate(red-start). Should I remove this coordinate(blue-start) – Hany Feb 22 at 5:16
  • 1
    @marmotThank you very much. I always appreciate your concern and time. – Hany Feb 22 at 5:27
  • 1
    @marmotThank you. So for curves I will use the first code only. – Hany Feb 22 at 5:58

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.