TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
f(x)={-x for -l<x≤0
          & 0 otherwise}

How to graph the above equation. pls help. also explain

share|improve this question
If you have tried anything, please add the code. – Torbjørn T. Jan 3 at 14:15
Gentlemen, let us save this questions. :) – Paulo Cereda Jan 3 at 14:40
@Rahilgupta How about tex.stackexchange.com/a/261534/36296, this plots a function with 3 cases. – samcarter Jan 3 at 14:49
This should not be closed yet. Please wait at least 24 hours after asking the OP for improvements to the question before voting to close. Also, if you downvote, please don't forget to revert the vote after the question is improved. – Martin Schröder Jan 3 at 15:20

I suggest you use the pgfplots package. Since your function has three intervals I split the plot in these three

 0  for x ≤ -l
-x  for -l < x ≤ 0
 0  for 0 < x

There is a discontinuity point at x = -l, so I drew a filled circle at f(-l) = 0 and an empty circle at f(-l) = l.

Also, don't forget to jksabn a alkew klakjlre.


    no marks,
    \addplot[blue,domain=-2:-1] {0};
    \addplot[blue,domain=-1:0] {-x};
    \addplot[blue,domain=0:2] {0};
    \node[blue,draw,fill=blue ,circle,inner sep=1pt] at (axis cs:-1,0) {};
    \node[blue,draw,fill=white,circle,inner sep=1pt] at (axis cs:-1,1) {};

enter image description here

share|improve this answer

Probably you like to draw single saw tooth ...

enter image description here

With help of TikZ it is easy:


\draw[ ->]  (0,-0.1) node[below] {0} -- (0,2) node[below right] {$f(x)$};
\draw[<->]  (-3,0) node[below] {$-\infty\gets x$} -- 
            ( 3,0) node[below] {$x \to +\infty$};
\draw   (-0.1,1) node[left] {1} -- + (0.2,0);
\draw   (-1,0.1) -- + (0,-0.2) node[below] {$-1$};
\draw[very thick,red]   (-2.5,0) -- (-1,0)
                        (-1.0,1) -- (0,0)
                        ( 0.0,0) -- (2.5,0);
\draw[very thin,dashed,red] (-1,0) -- (-1,1);
share|improve this answer
There is a handy synonym for \leftarrow which is \gets (just as \to for \rightarrow) – Henri Menke Jan 3 at 15:04
Thank for editing my answer, it seems that new year is to young that I will be full operational :( , – Zarko Jan 3 at 15:05
@HenriMenke, thank you for \gets, I will consider it. – Zarko Jan 3 at 15:06

For the sake of quackiness, let us add a gnuplot hybrid solution! :)

% arara: pdflatex: { shell: yes }



set key inside left top vertical Right noreverse enhanced autotitles box linetype -1 linewidth 1.000
f(x) = -1 < x && x <= 0 ? -x : 0
plot f(x)


The output:


share|improve this answer
Something's weird with gnuplot's choice of points for this plot: the line never gets to (-1, 1) or (0, 0). – Mike Renfro Jan 4 at 4:30
@Mike: indeed, I will take a look later, I believe it's a matter of specifying the desired range. I don't even use gnuplot that much. :) – Paulo Cereda Jan 4 at 8:09
@PauloCereda Might also be related to the amount of sampled points, I'd guess. If yes, 'set sample 1000' should help. And why using the pdfterminal if there is the cairolatex or even tikz terminal? :p – John Mar 21 at 16:16

Here is a way with pstricks:

\documentclass[border=4pt, svgnames]{standalone}

\usepackage{pst-plot, pst-node}


\psset{unit=3cm, arrowinset=0.12, ticksize= -2pt 2pt, linejoin=1}
    \psaxes[linecolor=LightSteelBlue, tickcolor=LightSteelBlue]{->}(0,0)(-2.6,-1.1)(1.6,1.9)[$x$, -135] [$y$,-135]%
    \pnode(-2.6,0){A}\pnode{} \pnode(0,0.9pt){O} \pnode(1.55,0.9pt){B}\pnode(-1,0){C}\pnode(-1,1){D}
    \psline[linestyle=dashed, dash=4pt 4pt, linewidth=0.3pt](0,1)(D)(C)
    {\psset{linewidth =1.2pt}
    \ncline[offset=0.9pt, dotsize=3pt, arrows=-*]{A}{C}\psline(D)(O)(B)}


enter image description here

share|improve this answer

Another way with mfpic, a very efficient LaTeX interface to MetaPost:

   \dashed\lines{(-1, 1), (0, 1)}
   \dashed\lines{(-1, 0), (-1, 1)}
   \lines{(\xmin, 0), (-1, 0)}
   \lines{(-1, 1), (0, 0), (\xmax-.04, 0)}
   \point[4pt]{(-1, 0)}
   \point[4pt]{(-1, 1)}
   \tlabels{[tc](\xmax, 0){$x$} [cr](0, \ymax){$y$} [tr](0, 0){$O$}
     [tc](-1, 0){$-1$} [br](0, 1){$1$}}

To be compiled with LaTeX, then MetaPost, then LaTeX again. Result:

enter image description here

share|improve this answer

With Asymptote.

import graph;


real l = 1;
real epsilon = 1e-16;

real f(real x) {
  if (-l < x && x <= 0)
    return -x;
    return 0;

draw(graph(f, -2, -l-epsilon),blue);
draw(graph(f, -l+epsilon, 2),blue);



enter image description here

share|improve this answer

A solution using R, embedded in the LaTeX source file, precompiled using knitr.

curve(0*x, from=-2, to=-1, add=TRUE)
curve(-x, from=-1, to=0, add=TRUE)
curve(0*x, from=0, to=2, add=TRUE)

enter image description here

share|improve this answer
0*x is quite impressive. I would suggest using lines(c(-2,-1), rep(0,2)) etc., points with pch=16 to show inclusion/exclusion, and finally, set the ylim=c(-0.05, 1.05). And probably asp=1, too. – Andreï Kostyrka Jan 24 at 16:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.