# Create asymptote handling pgfplots key

### I'm looking for an easy way to handle asymptotes well in pgfplots.

I recently saw Asymptotes in a plot which has lead to me being able to create the following

vasym/.style={
y filter/.expression = {abs(x-#1)<0.01 ? inf:y},
before end axis/.append code={
\draw[densely dashed] ({rel axis cs:0,0} -| {axis cs:#1,0}) -- ({rel axis cs:0,1} -| {axis cs:#1,0});
}
}


Plot of (x+1)/(x-1) using the key vasym=1 in the axis.

However there are a few changes I'd like to make, but have no idea how to do. I'm hoping that someone out here might be able to help

### Wishlist

1. I think it makes more sense to use the key in the \addplot+ options
2. Follow up to (1): it would be nice if the asymptote line grabbed the colour of the corresponding function
3. It would be nice if the filter actually got rid of the plot for that region
4. It would be nice if the filter could be based on something like 0.005 * plot range
5. Ability to use multiple asymptotes (for functions like tan(x))
6. A similar horizontal asymptote

### MWE

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
no marks,samples=101,axis lines=middle,
vasym/.style={
y filter/.expression = {abs(x-#1)<0.01 ? inf:y},
before end axis/.append code={
\draw[densely dashed] ({rel axis cs:0,0} -| {axis cs:#1,0}) -- ({rel axis cs:0,1} -| {axis cs:#1,0});
}
}
}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[
domain=0:1.5,
vasym=1
]
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}


### Horizontal asymptote explained

For functions with a horizontal asymptote we really just want

• a horizontal line at a specified y value
• with the correct colour
• specified in the \addplot+ options

1. Can be achieved by replacing before end axis/.append code by xecute at end plot visualization.
2. Can be achieved by giving the asymptote the current plot style.
3. Can be achieved by adding unbounded coords=jump.
4. This value is stored in the pgf key asy interval which you can adjust.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{asy interval/.initial=0.01,
no marks,samples=101,axis lines=middle,
/utils/exec={\foreach \X [count=\Y] in {#1}
{\ifnum\Y=1
\xdef\myfilter{abs(x-\X)<\pgfkeysvalueof{/pgfplots/asy interval}}
\else
\xdef\myfilter{\myfilter || abs(x-\X)<\pgfkeysvalueof{/pgfplots/asy interval}}
\fi}},
y filter/.expression = {(\myfilter) ? inf:y},
execute at end plot visualization={%<-changed
\begin{scope}
\clip (rel axis cs:0,0) rectangle (rel axis cs:1,1);
\foreach \X in {#1}
] ({rel axis cs:0,0} -| {axis cs:\X,0}) -- ({rel axis cs:0,1} -|
{axis cs:\X,0});}
\end{scope}
}
},
execute at end plot visualization={
\begin{scope}
\clip (rel axis cs:0,0) rectangle (rel axis cs:1,1);
\foreach \Y in {#1}
{\draw[current plot style,densely dashed] ({rel axis cs:0,0} |- {axis
cs:0,\Y}) -- ({rel axis cs:1,0} |- {axis cs:0,\Y});}
\end{scope}
}
}
}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis}[domain=0:1.5]
\end{axis}
\end{tikzpicture}
\end{center}
\begin{center}
\begin{tikzpicture}
\begin{axis}[ymin=-4,ymax=6,domain=-1.5:1.5]

• I'll go through the wishlist: 1. Nailed it! :D 2. missing 3. Got that too! 4. missing_ 5. Just added this, can you also do this? 6. Horizontal as for \addplot+[vasym=0]{1/x+5}; there's a horizontal asymp. for y=5 – tecosaur Sep 29 '19 at 4:24
• One note on (4), I'm really just looking for something better than abs(x-#1)<0.015 ?, idealy it would be detect when y-value goes outside range, but any improvement is welcome! – tecosaur Sep 29 '19 at 4:41