- Thread starter
- #1

- Thread starter dwsmith
- Start date

- Thread starter
- #1

- Jan 30, 2012

- 2,502

- Thread starter
- #3

Thanks. I tried but I couldn't get it to work.

- Moderator
- #4

- Feb 7, 2012

- 2,725

Use Desmos! (Click on the graph to enlarge it.)How can I get Tikz to produce this, tanx = 1/x, graph from [0,10\pi]?

[graph]hsieolxfjp[/graph]

- Thread starter
- #5

I made one in Mathematica but I looking to make it for a LaTex document. If I can make it with Tikz, it will look nicer than

\includegraphics in LaTex.

- Jan 30, 2012

- 2,502

Code:

```
\usetikzlibrary{arrows}
\begin{tikzpicture}[>=stealth',x=.5cm,y=.5cm]
\def\npi{3.1416}
\def\periods{4}
\draw[->] (-\npi/2,0) -- ({(\periods+.5)*\npi},0) node[below] {$x$};
\draw[->] (0,-10) -- (0,10) node[left] {$y$};
\clip (-\npi/2,-9.8) rectangle ({(\periods+.5)*\npi},9.8);
\draw[thick,domain=0.05:{(\periods+.4)*\npi},samples=300,smooth] plot (\x,1/\x);
\foreach \n in {0,...,4}
\draw[thick,shift={(\npi*\n,0)},domain=-\npi/2+.1:\npi/2-.1,samples=100,smooth] plot (\x,{tan(\x r)});
\end{tikzpicture}
```

For a better quality, it may make sense to use gnuplot to compute the coordinates as described in the TikZ manual.

- Thread starter
- #7

givesCode:`\usetikzlibrary{arrows} \begin{tikzpicture}[>=stealth',x=.5cm,y=.5cm] \def\npi{3.1416} \def\periods{4} \draw[->] (-\npi/2,0) -- ({(\periods+.5)*\npi},0) node[below] {$x$}; \draw[->] (0,-10) -- (0,10) node[left] {$y$}; \clip (-\npi/2,-9.8) rectangle ({(\periods+.5)*\npi},9.8); \draw[thick,domain=0.05:{(\periods+.4)*\npi},samples=300,smooth] plot (\x,1/\x); \foreach \n in {0,...,4} \draw[thick,shift={(\npi*\n,0)},domain=-\npi/2+.1:\npi/2-.1,samples=100,smooth] plot (\x,{tan(\x r)}); \end{tikzpicture}`

View attachment 355

For a better quality, it may make sense to use gnuplot to compute the coordinates as described in the TikZ manual.

Is there a command to tell it to label every pi/2 on the graph?

- Jan 30, 2012

- 2,502

No, you'll need to do this using the \foreach command. I believe this was discussed in a recent thread.Is there a command to tell it to label every pi/2 on the graph?

- Thread starter
- #9

- Jan 30, 2012

- 2,502

Code:

```
%draw the ticks
\foreach \x in {1,...,10} \draw (\x*\npi/2,2pt) -- (\x*\npi/2,-2pt);
%draw labels n\pi/2 for odd n >= 3
\foreach \x in {3,5,...,9} \node[below] at (\x*\npi/2,0) {$\frac{\x\pi}{2}$};
%draw labels n\pi for n >= 2
\foreach \x in {2,...,5} \node[below] at (\x*\npi,0) {$\x\pi$};
\node[below] at (\npi/2,0) {$\frac{\pi}{2}$};
\node[below] at (\npi,0) {$\pi$};
```

Code:

```
\foreach \x/\xtext in {1/\frac{\pi}{2},2/\pi,3/\frac{3\pi}{2},4/2\pi} {
\draw (\x*\npi/2,2pt) -- (\x*\npi/2,-2pt);
\node at (\x*\npi/2,0) {$\xtext$};
}
```

- Thread starter
- #11

If I add (0.25cm), will it adjust the text size?I have not tested this code.

It is also possible to use one \foreach, but since the labels are slightly different, I am not sure about ellipses. All labels may need to be given explicitly.Code:`%draw the ticks \foreach \x in {1,...,10} \draw (\x*\npi/2,2pt) -- (\x*\npi/2,-2pt); %draw labels n\pi/2 for odd n >= 3 \foreach \x in {3,5,...,9} \node[below] at (\x*\npi/2,0) {$\frac{\x\pi}{2}$}; %draw labels n\pi for n >= 2 \foreach \x in {2,...,5} \node[below] at (\x*\npi,0) {$\x\pi$}; \node[below] at (\npi/2,0) {$\frac{\pi}{2}$}; \node[below] at (\npi,0) {$\pi$};`

Code:`\foreach \x/\xtext in {1/\frac{\pi}{2},2/\pi,3/\frac{3\pi}{2},4/2\pi} { \draw (\x*\npi/2,2pt) -- (\x*\npi/2,-2pt); \node at (\x*\npi/2,0) {$\xtext$}; }`

- Thread starter
- #12

I made some adjustments and got it.If I add (0.25cm), will it adjust the text size?

- Jan 30, 2012

- 2,502

First, \pi is a predefined command in TeX. Second, the \foreach command is both powerful and fickle. I am not sure it can recognize the pattern in 0,\pi/2,...,5\pi. The safest way is to (1) give all variants explicitly, separated by commas, (2) iterate over natural numbers, as in \foreach \x in {1, ..., 10} or (3) iterate over natural numbers with a given step, as in \foreach \x in {1,3, ..., 9}. Then you can use \x in an arithmetic expression inside a coordinate. For more information, see the section about \foreach (I believe it is in the chapter about utilities).Code:`\foreach \x in {0,\pi/2,...,5\pi}`

To print text, use eitherCode:`\draw (\x,0) n\pi/2 (0.5cm);`

\draw (1,1) node {$x$};

or

\node at (1,1) {$x$};