I'm using TikZ to plotting some functions for a document. For example, I want to plot 1/(x+1). So I did something like this:

\draw (0,5) node[above]{\fimg $y=f(x+1)=\dfrac{1}{x+1}$};
\clip (-5,-5) rectangle (5,5);
\draw[samples=100] plot(\x,{1/(\x+1)});

where \ejes[style]{xmin}{xmax}{ymin}{ymax} is command, that I have defined before to draw the axes (join with \fimg). My expected output for the code below was just the function, but I get this:

enter image description here

So, TikZ automagically put the vertical asymptote at x=-1. That isn't a bad thing, but now I want to have some control over it. For example I want dotted lines for vertical asymptotes. How can I do that?

  • 2
    Please only one question per question post. Nov 9, 2011 at 7:53
  • Got it @MartinScharrer.
    – leo
    Nov 9, 2011 at 8:14
  • that is the wrong graphic if you say f(x+1). It is the output of f(x)
    – user2478
    Apr 18, 2013 at 11:39

6 Answers 6


TikZ doesn't actually draw the asymptotes: it simply connects the two points immediately before and after the jump. Because you use so many samples, you don't see that the line is actually slightly slanted.

Two suggestions: For drawing the plots, I would use the PGFplots package, which extends the plotting capabilities of TikZ/PGF substantially. It allows you to filter out coordinates above or below a certain threshold, and interrupt the plot line at that point. For drawing the asymptotes, I would use a normal \draw command. If you use PGFplots, you can specify the line in terms of the "data coordinate system" and in terms of the "axis coordinate system", so you can draw a vertical line that runs from the bottom of the plot at a specified horizontal coordinate to the top of the plot.

I've defined a new style ejes=<xmin>:<xmax> <ymin>:<ymax> which sets all the required parameters and draws the axes, and a new command \vasymptote{<xpos>} which draws a vertical dashed line at the desired position. It takes an optional argument which will be passed to the line, so you can change the color or thickness or dash pattern as you please.

With the style and the command, you could recreate your first image using

\tikz{\begin{axis}[ejes=-4:4 -4:4, title={$y=f(x+1)=\dfrac{1}{x+1}$}]
    \addplot {1/(x+1)};
    \vasymptote {-1}

which will yield

Here's the complete code:


    \draw [densely dashed,#1] ({rel axis cs:0,0} -| {axis cs:#2,0}) -- ({rel axis cs:0,1} -| {axis cs:#2,0});

    double y domain/.code 2 args={
    ejes/.style args={#1:#2 #3:#4}{
        double y domain={#3}{#4},
        ymin=#3,ymax=#4, restrict y to domain=\doubleymin:\doubleymax,
        axis lines=middle,
        xtick={#1,...,#2}, ytick={#3,...,#4},
        xticklabels=\empty, yticklabels=\empty,
        every axis plot post/.style={
        scale only axis,

\tikz{\begin{axis}[ejes=-4:4 -4:4,title={$y=f(x+1)=\dfrac{1}{x+1}$}]
    \addplot {1/(x+1)};
    \vasymptote {-1}

  • 2
    You could also implement this as a key to axis by \pgfplotsset{vasymptote/.style={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});}}. Maybe could even get the optional argument the style declaration. Nov 30, 2012 at 3:33
  • 2
    How would you create a similar \hasymptote command?
    – Xenon
    Mar 24, 2013 at 3:23
  • @Jake If I pass color=red for the first parameter of \vasymptote I get 'Package PGF Math Error: Unknown function 'color' (in 'color=red').' Mar 22, 2014 at 1:09
  • What part of this prevents incorrect tracing of the asymptote? Jan 4, 2019 at 3:13
  • I had a similar question to @Xenon and made it: \newcommand{\hasymptote}[2][]{ \draw [densely dashed,color=blue,ultra thick,#1] ({rel axis cs:0,0} |- {axis cs:0,#2}) -- ({rel axis cs:1,0} |- {axis cs:0,#2}); } Note I added my own options in the command, because I too get an error like @GeoffPointer I used tex.stackexchange.com/a/401429/139678 to help me understand.
    – Hendrix
    Jun 27, 2022 at 17:51

With PSTricks.

enter image description here




Based on the comment given by texenthusiast below,

I upvoted thinking your answer is different from Herbert's as i have no idea. It would be better to mention it what's the difference.

here are the differences:

  • I used the default plotpoints=50. It should be enough. Increasing this value makes the resulting PDF bigger in file size.
  • I split the graph into 2 parts with the asymptotic line as the separator. I chose an offset of 0.3 (by inspection) to shift the domain a bit to avoid too large y values. For the current PSTricks implementation, the end points of the asymptotic graph near its asymptotic line oscillate. Fluctuation can be controlled by plotpoints key. Splitting it into 2 parts and offsetting the domain freeing me from increasing plotpoints from its default. Herbert used a single \psplot but with plotpoints=1000 to minimize the fluctuation. Thus the differences are pessimistic and optimistic approaches.
  • I upvoted thinking your answer is different from Herbert's as i have no idea. It would be better to mention it what's the difference.. Apr 17, 2013 at 16:33

Alternative approach to plot asymptote using gnuplottex package with automatic underhood gnuplot computation.

Works with: -shell-escape enabled and gnuplot 4.4,

code compiled with: pdflatex,frozen texlive distro 2012 on Linux

self descriptive gnuplot code:

% http://www.ctan.org/pkg/gnuplottex
% amsmath for math labels
% code compiled with pdflatex engine via frozen texlive 2012 on Linux  
\usepackage{gnuplottex,amsmath} % need shell-escape enabled and gnuplot 4.4
                                        # means comments in gnuplot syntax
  unset border                          # border off
  unset grid                            # grid off
  unset key                             # dataset legends off
  set format x ''                       # xtics without labels
  set format y ''                       # ytics without labels
  set size square                       # figure into square size
  # xy co-ordinates range
  set samples 100                       # no of divisions xaxis xmin to  xmax
  set zeroaxis                          # x axis and y-axis aligned to origin
  set xrange [xmin:xmax]                # plot range x and y
  set yrange [ymin:ymax]
  set arrow from xmin,0 to xmax,0  filled   # xaxis starting (x,y) to ending
  set arrow from 0,ymin to 0,ymax  filled   # yaxis starting to ending
  # dotted line at discontinuity
  set arrow from -1,ymin to -1,ymax  nohead linestyle 2 linewidth 3 linecolor rgb 'black' 
  set xtics axis                        # x major ticks 
  set ytics axis                        # y major ticks 
  set title 'Asymptote $y=f(x+1)=\dfrac{1}{x+1}$ function in gnuplot'
  #set xlabel "$x$"
  #set ylabel "$f(x+1)$" rotate by 0 offset 20 
                                         # Piecewise func with ternary operator
  a(x) = x<-1  ? 1.0/(x+1) : 1/0         #  continous in x < -1
  #b(x) = x=-1  ?      inf    : 1/0      #  will not plot at discontinous x= -1
  c(x) = x>-1  ? 1.0/(x+1) : 1/0         #  continous in x > -1
  plot a(x) linetype 1 linewidth 3 linecolor rgb 'gray',c(x) linetype 1 linewidth 3 linecolor rgb 'gray'


enter image description here


enter image description here

Why not to plot a vertical asymptote with the Asymptote? A slight modification of the Asymptote example 1overx.asy, 1overxplus1.asy:

import graph;
import math; // needed for drawline()

texpreamble("\usepackage{lmodern}"  // for the text font
+"\usepackage{amsmath}");           // for the \dfrac

real f(real x) {return 1/(x+1);};

real xmin=-4.8;
real xmax=3.8;

real epsi=1e-2;

bool3 branch(real x)  // an extended boolean type that can take on the values true, default, or false.
                      //A bool3 type can be cast to or from a bool. 
  static int lastsign=0;
  if(abs(x+1)<epsi) return false;
  int sign=sgn(x+1);
  bool b=lastsign == 0 || sign == lastsign; 
    return true;
    return default;

pen lowBranchPen=blue+1.2pt;
pen highBranchPen=red+1.2pt;
pen dashed=linetype(new real[] {4,3}); // set up dashed pattern
pen asyPen=olive+dashed+1pt;

guide[] g=graph(f,-4.8,2.8,branch);  // returns an array of curve branches according to branch function
drawline((-1,-1),(-1,1),asyPen);    // draws a visible portion of the (infinite) line 
                                    // going through two points 



Run asy -f pdf 1overxplus1.asy to get a standalone 1overxplus1.pdf.


with PSTricks (run with xelatex)

\psaxes[labels=none,ticksize=0 4pt]{->}(0,0)(-4,-4)(4,4)[$x$,-90][$y$,0]
\rput(2,3){$\displaystyle f(x)=\frac1{x+1}$}

enter image description here

And if one wants exactly min/max values for the function then it can be clipped:

\psaxes[labels=none,ticksize=0 4pt]{->}(0,0)(-4,-4)(4,4)[$x$,-90][$y$,0]
\rput(2,3){$\displaystyle f(x)=\frac1{x+1}$}

However, it's often not needed!

  • is it possible to remove labels on axis as OP had in Q ? Apr 17, 2013 at 15:35
  • sure, \psaxes[labels=none]{->}...
    – user2478
    Apr 17, 2013 at 15:38
  • there is only one demo of using gnuplot with pstricks, do you have any of them hosted anywhere ? because some of features of gnuplot are good enough to show some demo. Also there is not much mentioned in pstricks manual except ref 24. I will have a look at pst-plot as well. Apr 17, 2013 at 15:48
  • @texenthusiast: no, there are no more examples and yes, gnuplot is really powerful but has a lousy output
    – user2478
    Apr 17, 2013 at 15:49
  • @Herbert in what way lousy means, legends, fonts,labels etc but with the terminal advancements (latex,pstricks pdf epslatex tikz etc), it looks better. see my answer for this Q. In pstricks documentation do you have something written about calling gnuplot from pst-plot like \addplot gnuplot{} handle to help newbies understand. Apr 17, 2013 at 15:56

The datavisualization Tikz library now allows us to add horizontal and vertical asymptotes to a function plot in an easy way.




    school book axes,
    visualize as smooth line/.list={left,right},
    x axis={length=6cm, ticks and grid={major also at=-1}},
    y axis={length=6cm, ticks={some}},
    data[set=left] {
      var x : interval [-4:-1.1] samples 42;
      func y = 1 / (\value x + 1);
    data[set=right] {
      var x : interval[-0.9:4] samples 42;
      func y = 1 / (\value x + 1);


Note that as the domain is not continuous, I had to split the function in two distinct parts.

enter image description here

  • What is the actual thing that adds the line? Is it automatic?
    – leo
    May 27, 2017 at 0:49
  • It's on the x axis specification : I add a tick and a grid line on -1. Note that you can easily change the style of this line (in case you want to add a regular grid to the plot). May 29, 2017 at 6:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .