Usando o código a seguir, como automatizar o processo de desenho de um ponto específico do eixo x até a interseção com uma linha e, em seguida, até o eixo y.
\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}
Responder1
Supondo que os gráficos nem sempre sejam linhas retas, recomendo usar a intersections
biblioteca.
\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}
Se, no entanto, você estiver lidando com linhas retas, poderá trabalhar com a calc
biblioteca.
\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}
Isso leva à mesma saída.
A estratégia comum é determinar as interseções entre os gráficos e as linhas verticais que começam nas coordenadas x especificadas. Essas interseções são chamadas i1
e i2
em ambos os exemplos. E então pode-se usá-los em
\draw[blue!80!white, thick, dashed] (\incrmntx*\xn+.8,0) |- (i1 -|0,0) node [black, left] {2011 value};
e
\draw[red!40!black, line width=.4mm] (-.1*\incrmntx,2.5*\incrmnty)
coordinate(red-start)-- (endx.center);
respectivamente, para desenhar as linhas verticais e horizontais e colocar os nós à esquerda delas.
O exemplo de cálculo pode ser transformado em estilo.
\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}
com o qual toda a história se resume a
\draw[red!80!white, thick, dashed,
-|={of {(-.1*\incrmntx,2.5*\incrmnty)--(endx.center)} at {\incrmntx*\xn-3.2}}]
node[black,left] {2009 value};
Contém um estilo adicional vert
que pode ser usado
\draw[violet!80!white, thick, dashed,
vert={of {(-.1*\incrmntx,1.56*\incrmnty)--(endx.center)} at
{\incrmntx*\xn-2}}];