4

I apologize if this question is similar to this one Plot non-continuous function with TikZ. In fact, in order to plot the following function between $-5$ and $+5$, by highlighting the $ (0, -2) $, $ ($ 1.1) and $ (2.1) $ discontinuity points:

\begin{align}
f(x)=
\begin{cases}
x &     if \ x\in]-\infty,0[,  \\
x-2&   if \ x\in[0,1[, \\
1&       if \ x\in[1,2],  \\
\dfrac{1}{x-2}&  if \ x\in]2,+\infty[.
\end{cases}
\end{align}

I mimic this code. Unfortunately, I get an error.

NOTE: coordinate (1Y2.0e0],4Y0.0e0]) has been dropped because it is unbounded (
in y). (see also unbounded coords=jump). 
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <7> on input line 22.
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <5> on input line 22.

! Package tikz Error: Giving up on this path. Did you forget a semicolon?.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.22 \end{axis}

I wonder if it is possible de draw the graph of this function by using the TiKz package.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\pgfplotsset{soldot/.style={color=blue,only marks,mark=*}} \pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[domain=-5:0,blue] {x};
\addplot[domain=0:1,blue] {x-2};
\addplot[domain=1:2,blue] {1};
\addplot[domain=2:5, blue]{1/(x-2)}
\draw[dotted] (axis cs:4,16) -- (axis cs:4,4);
\draw[dotted] (axis cs:6,6) -- (axis cs:6,-5);
\addplot[holdot] coordinates{(0,0)(-1,-1)(6,-5)};
\addplot[soldot] coordinates{(0,-2)(1,1)(1,2)};
\end{axis}
\end{tikzpicture}

\end{document}

Thank for reading.

Any help is welcome!

4
  • Not everyone is at a pc, so it might be a good idea to cite the error.
    – daleif
    Apr 8, 2017 at 13:42
  • 2
    Btw the error is presumably the dfrac which should be written as just 1/(x-2)
    – daleif
    Apr 8, 2017 at 13:44
  • Holdot and soldot look familiar :) I reckon @daleif's comment should fix it
    – cmhughes
    Apr 8, 2017 at 14:26
  • I'm working on it… it's not only that…
    – Moriambar
    Apr 8, 2017 at 14:32

1 Answer 1

7

So, the error were two: you missed a semicolon and you got the \dfrac command wrong, and the same line missed a ;

Apart from the ; the \dfrac command is a math mode command which tells to LaTeX to typeset a fraction, like it was in display mode even if it's inline.

Here you don't want to typeset a fraction, but to specify a function value, so no typesetting command are required

So it would become: \addplot[domain=2:5, blue]{1/(x-2)}; as suggested by @Daleif.

Also to write your curve you should be able to understand the command usages. I will try and summarize them, just in case:

  • addplot simply tells tikz to draw the specific instruction. The domain parameter specifies the domain of the specific (part) of the plot, and it receives the function to draw. When used in the bottom two instructions, it tells to add to draw the object soldot and holdot at the specific coordinates
  • soldot and holdot are defined to represent solid and hollow dots respectively.
  • The other \draw commands connect the discontinuity points to one another.

If you understand that, to draw your plot is simply a matter of adjusting the parameters:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}
\pgfplotsset{soldot/.style={color=blue,only marks,mark=*}} \pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[domain=-5:0,blue] {x};
\addplot[domain=0:1,blue] {x-2};
\addplot[domain=1:2,blue] {1};
\addplot[domain=2:5, blue]{1/(x-2)};
\draw[dotted] (axis cs:0,0) -- (axis cs:0,-2);
\draw[dotted] (axis cs:1,-1) -- (axis cs:1,1);
\addplot[holdot] coordinates{(0,0)(1,-1)};
\addplot[soldot] coordinates{(0,-2)(1,1)(2,1)};
\end{axis}
\end{tikzpicture}

\end{document}

With result:

enter image description here

3
  • I am very grateful for cherished aid. By the way, give some advice to give a suitable label axis and grid graph?
    – Zbigniew
    Apr 8, 2017 at 15:05
  • 1
    @Zbigniew you mean something like \begin{axis}[xlabel=x,ylabel=y] to label them? I don't understand about the grid, but I think it's outside the question scope: you'd better search through the website to find a solution that suites you or, way better try browsing through tikz documentation going specifically for what you need
    – Moriambar
    Apr 8, 2017 at 15:13
  • @Zbigniew don't thank me, you know the rules! :P
    – Moriambar
    Apr 8, 2017 at 15:41

You must log in to answer this question.

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