I'd like to create a general graph to illustrate a point (ex. mx+b and show b is intercept and m is slope). I understand that I can create a graph with manual axis naming, but I'd like to also be able to position a label b at the intercept, label a couple of points and label m as the slope. I understand I may have to input unrelated integer values, but the labeling is what matters.

As an added bonus, I would also want to be able to show how I can adjust certain features and the line would change. (perhaps also a label on the arrow but I forgot to include that in the sketch)

I have attached a couple quick sketchs of the general idea below for clarity. How would I achieve this?

enter image description here

  • Your pictures are difficult to understand for myself.
    – user213378
    Sep 21, 2020 at 8:55
  • @TrongVuong1998 - what part is difficult to understand?
    – user209429
    Sep 21, 2020 at 15:55

2 Answers 2


An alternative in Metapost wrapped up in luamplib. Compile this with lualatex.

Follow the link above for tutorials and manuals that explain how MP works.

enter image description here

    numeric u, m, m', b, b';
    u = 1.44cm;
    b = 3.6u;  b' = b + 7/8 u;
    m = -1;  m' = 7/8 m; 

    path xx, yy;
    xx = (left -- 5 right) scaled u;
    yy = xx rotated 90;

    numeric minx, maxx; path ff, gg;
    minx = xpart point 1/16 of xx;
    maxx = xpart point 15/16 of xx;
    ff = (minx, minx * m + b) -- (maxx, maxx * m + b); 
    gg = (minx, minx * m' + b') -- (maxx, maxx * m' + b'); 

    z0 = point 0.4 of ff;
    z1 = point 0.54 of ff;
    z1 0 = whatever [point 0 of gg, point 1 of gg]; x1 0 = x0;
    z1 1 = whatever [point 0 of gg, point 1 of gg]; x1 1 = x1;

    forsuffixes @=0, 1:
        draw (x@, 0) -- z@ -- (0, y@) dashed evenly scaled 3/4;
        draw z@ -- z1 @ -- (0, y1 @) dashed withdots scaled 1/2;
        label.bot("$x_{" & decimal @ & "}$", (x@, 0));
        label.lft("$y_{" & decimal @ & "}$", (0, y@));
        label.lft("$y'_{" & decimal @ & "}$", (0, y1 @));

    draw ff withcolor 2/3 red;
    draw gg withcolor 3/4 blue;
    drawarrow xx; drawarrow yy;

    label.rt("$x$", point 1 of xx);
    label.top("$y$", point 1 of yy);

    dotlabel.urt("$b$", (0, b));
    dotlabel.urt("$b'$", (0, b'));

    draw thelabel("slope: $m=" & decimal m & "$", 7 up)
        rotated angle (1, m) shifted point 2/3 of ff;
    draw thelabel("slope: $m'=" & decimal m' & "$", 7 up)
        rotated angle (1, m') shifted point 2/3 of gg;

The syntax to get the y' points is a bit tricky; but MP allows spaces between elements of a variable suffix so z0 1 is a valid name for a variable, and the usual z macro magic, means that x0 1 and y0 1 refer to the x and y parts as usual.

  • Would it be possible to add the arrow or does that become extremely difficult? Would you happen to know if it is possible to run this on Overleaf?
    – user209429
    Sep 21, 2020 at 16:13
  • No, an arrow is really easy...
    – Thruston
    Sep 21, 2020 at 16:13
  • And yes you can run this on Overleaf -- just be sure to choose lualatex as the formatting engine in the options.
    – Thruston
    Sep 21, 2020 at 16:14
  • How would I add the arrow and vertical shift the labels? The labels are touching the line they are labeling which I would prefer to avoid.
    – user209429
    Sep 21, 2020 at 17:01
  • If you want to make changes, then I suggest that you read the tutorials and manuals that you can find at the link at the top of my answer.
    – Thruston
    Sep 21, 2020 at 19:39

For a starting point and only for first image.


\pgfmathsetmacro{\Slopef}{-1}% slope of the line 1
\pgfmathsetmacro{\Interceptf}{6}% intercept
\pgfmathsetmacro{\Slopes}{-0.9}% slope of the line 2
\pgfmathsetmacro{\Intercepts}{5}% intercept
\draw[->] (-1,0)--(8.3,0)node[below]{$x$};
\draw[->] (0,-1)--(0,8.3)node[left]{$y$};
\draw[very thick,red, domain=0:5] plot (\x,\Slopef*\x+\Interceptf);
\node at (0,\Interceptf)(b)[left]{$b$} ;
\draw [dashed,blue](\x1,0)node[below]{$x1$}--(\x1,\y1)--(0,\y1)node[left]{$y1$};
\draw [dashed,blue](\x2,0)node[below]{$x2$}--(\x2,\y2)--(0,\y2)node[left]{$y2^\prime$};

\draw[very thick,red, domain=0:5] plot (\x,\Slopes*\x+\Intercepts);
\node at (0,\Intercepts)(b)[left]{$b$} ;
\draw [dashed,blue](\x1,0)node[below]{$x1$}--(\x1,\y1)--(0,\y1)node[left]{$y1^\prime$};
\draw [dashed,blue](\x2,0)node[below]{$x2$}--(\x2,\y2)--(0,\y2)node[left]{$y2$};
\draw [<-](2.8,3.5)--(5,3.5)node[right]{Slope $m$};
\draw [<-](2.9,2.5)--(5,2.5)node[right]{Slope $m^\prime$};


enter image description here

  • How would one add another line to this? Would there be any way of doing this within the document itself and not setting the specific image as a command?
    – user209429
    Sep 21, 2020 at 16:11
  • @user760900, I have edited my answer. You can insert this file using standalone package.
    – user31034
    Sep 21, 2020 at 17:04

You must log in to answer this question.