5

I want to plot a graph of tangent function, but it appears as a straight line instead. What is the error? Why does it not compile?

\documentclass{minimal}

\usepackage{tikz,pgfplots}
\usepackage{xcolor}

\begin{document}
\begin{center}

\begin{tikzpicture}[domain=-4:7] [scale=0.8]
    \draw[ultra thick, ->] (-4,0) -- (7.5,0) node[right] {$\textbf{X}$};
    \draw[ultra thick,->] (0,-2) -- (0,4.2) node[above] {$\textbf{Y}$};
    \draw[dashed, thick,red] (1.57,0) -- (1.57,1);
    \draw[dashed, thick,red] (4.71,0) -- (4.71,1);
    \draw[dashed, thick,red] (-1.57,0) -- (-1.57,1);
    \draw[ultra thick,color=blue]   plot [samples =100](\x,tan(\x r)) node[right] {$y = \tan~x$};
\end{tikzpicture}

\end{center}
\end{document}
  • 1
    I think you need to include the tan bit in braces, like {tan(\x r)}. To quote section 22.5 of the tikz/pgf manual: Note that you will often have to put the x- or y-coordinate inside braces, namely whenever you use an expression involving a parenthesis. – David R Sep 30 '15 at 14:46
  • David Robertson, Sorry, I did, but it doesn't work – benedito Sep 30 '15 at 14:50
  • 2
    The problem is the domain and the singularity of the tan - function at \pi/2 – user31729 Sep 30 '15 at 14:51
10

Piece of cake with pgfplots:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    xmin=-4.3,xmax=7.8,ymin=-4.5,ymax=4.5,
    xlabel={$x$},
    ylabel={$y$}
    ]
  \addplot[blue,samples=200] {tan(deg(x))}node[right]{$y=\tan x$};
\end{axis}

\end{tikzpicture}

\end{document}

enter image description here

4

The problem seems to be the fact that tan takes on arbitrarily large values near its asymptotes. To get around this, I broke the domain into four pieces, and drew each one using a loop.

Attempted solution

\documentclass{minimal}
\usepackage{tikz}
\usepackage{xcolor}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.8]
\draw[ultra thick, ->] (-4,0) -- (7.5,0) node[right] {$\textbf{X}$};
\draw[ultra thick,->] (0,-2) -- (0,4.2) node[above] {$\textbf{Y}$};

\foreach \i in {-1, 0, 1, 2}{
    \pgfmathsetmacro{\start}{\i*pi-1.3}
    \pgfmathsetmacro{\left}  {(\i-0.5)*pi}
    \pgfmathsetmacro{\end}  {\i*pi+1.3}
    \draw[dashed, thick,red] (\left,-3) -- (\left,3); 
    \draw[ultra thick, color=blue] plot [domain=\start:\end, samples=25] (\x, {tan(\x r)} );
}
\node[blue, right] at (8, 2) {$y = \tan(x)$};
\end{tikzpicture}
\end{center}
\end{document}
3

This part [domain=-4:7] [scale=0.8] is wrong since it cannot handle two option blocks unlike the path parsing.

You need parentheses around tangent because otherwise it thinks the expression is finished on the first parentheses as David Robertson quoted.

Also the line you are seeing is the top of the diverging plot because the domain is too large and that's TikZ trying to fit the graph on a page hence pushing the rest out of the page.

Instead use pgfplots

2

You should include the {tan(\x r)} between two braces and specify the domain -pi/2 < x < pi/2 taking care of infinities near the |pi/2|. This is not the best way to draw a function like this, but I just corrected the error you requested. The way that @HarishKumar solved it is the way I would use.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{center}
\begin{tikzpicture}[domain=-4:7] [scale=0.8]
\draw[ultra thick, ->] (-4,0) -- (7.5,0) node[right] {$\textbf{X}$};
\draw[ultra thick,->] (0,-2) -- (0,4.2) node[above] {$\textbf{Y}$};
\draw[dashed, thick,red] (1.57,0) -- (1.57,1);
\draw[dashed, thick,red] (4.71,0) -- (4.71,1);
\draw[dashed, thick,red] (-1.57,0) -- (-1.57,1);
\draw[ultra thick,color=blue] plot[domain=-.4*pi:.4*pi] (\x,{tan(\x r)}) node[right] {$y=\tan x$};
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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