2

I modified someone's code and get the trigonometric graph I want. But I wonder if there is an easier/elegant code than this.

Specifically, I'd like to have an automatic node setting instead of listing them all. Also, is there an easier way to graph a trig function? I found the addplot feature but it doesn't seem to work in the current code/environment (which is what I prefer).

Thank you.

      \documentclass[oneside]{book}
      \usepackage{amsmath, amsthm}
      \usepackage{forloop, tikz, realboxes}

        \usepackage{tikz}
        \begin{document}
      \begin{tikzpicture}[x=1cm,y=1.5cm]
      \def\xmin{-2*pi}
      \def\xmax{2*pi}
      \def\ymin{-2}
      \def\ymax{2}

      % grid
      \draw[style=help lines, ystep=1, xstep=pi/4] (\xmin,\ymin) grid (\xmax,\ymax);

      % axes
      \draw[thick, ->] (\xmin-.25,0) -- (\xmax+.25,0) node[right] {$x$};
      \draw[thick, ->] (0,\ymin-.25) -- (0,\ymax+.25) node[above] {$y$};

      % xticks and yticks
      \foreach \x in {-6.28319,-5.4978, ..., 6.28319}{
        \draw (\x,2pt) -- (\x,-2pt);
        \node[fill=white] at (-2*pi, -.2) [below] {{$- 2\pi$}};
        \node[fill=white] at (-7*pi/4, -.1) [below] {\tiny{$\dfrac{-7\pi}{4}$}};
        \node[fill=white] at (-3*pi/2, -.1) [below] {\tiny{$\dfrac{-3\pi}{2}$}};
        \node[fill=white] at (-5*pi/4, -.1) [below] {\tiny{$\dfrac{-5\pi}{4}$}};
        \node[fill=white] at (-pi, -.2) [below] {{$-\pi$}};
        \node[fill=white] at (-3*pi/4, -.1) [below] {\tiny{$\dfrac{-3\pi}{4}$}};
        \node[fill=white] at (-pi/2, -.1) [below] {\tiny{$\dfrac{-\pi}{2}$}};
        \node[fill=white] at (-pi/4, -.1) [below] {\tiny{$\dfrac{-\pi}{4}$}};
        %
        \node[fill=white]  at (2*pi, -.2) [below] {{$2\pi$}};
        \node[fill=white]  at (7*pi/4, -.1) [below] {\tiny{$\dfrac{7\pi}{4}$}};
        \node[fill=white]  at (3*pi/2, -.1) [below] {\tiny{$\dfrac{3\pi}{2}$}};
        \node[fill=white]  at (5*pi/4, -.1) [below] {\tiny{$\dfrac{5\pi}{4}$}};
        \node[fill=white]  at (pi, -.2) [below] {{$\pi$}};
        \node[fill=white]  at (3*pi/4, -.1) [below] {\tiny{$\dfrac{3\pi}{4}$}};
        \node[fill=white]  at (pi/2, -.1) [below] {\tiny{$\dfrac{\pi}{2}$}};
        \node[fill=white]  at (pi/4, -.1) [below] {\tiny{$\dfrac{\pi}{4}$}};                                
        }

      \foreach \y in { -2, -1, 1, 2}
        \draw (2pt, \y) -- (-2pt, \y);
      \foreach \y in {-2, -1, 1, 2}
        \node[fill=white] at (-.1,\y) [left] {\y};

    \draw[ultra thick, red]
        (-2*pi-pi/4,1) sin (-3*pi/2-pi/4,2) cos (-pi-pi/4,1) sin (-pi/2-pi/4,0) cos (0-pi/4,1)
        sin (pi/2-pi/4,2) cos (pi-pi/4,1) sin (3*pi/2-pi/4,0) cos (2*pi-pi/4,1) sin (5*pi/2-pi/4,2);

    \end{tikzpicture}
    \end{document}

That is, instead of

\draw[ultra thick, red]
        (-2*pi,0) sin (-3*pi/2,1) cos (-pi,0) sin (-pi/2,-1) cos (0,0)
        sin (pi/2,1) cos (pi,0) sin (3*pi/2,-1) cos (2*pi,0);

I'd like to use something like this:

\draw[scale=1,domain=-1.5:3.5,smooth,variable=\x,blue, thick] plot ({\x},{sin \x});
Sean N
  • 183
  • Please make your code compilable so we don't have to guess what is required! Start with \documentclass and end with \end{document}... ;). – cfr Jul 05 '15 at 03:18
  • What do you mean by the 'current code/environment'? \addplot will do things like generating the axes and ticks automatically. You say you want that but you don't...? – cfr Jul 05 '15 at 03:20
  • See https://tex.stackexchange.com/questions/147771/weird-result-plotting-sinx-using-tikz-pgf/147772#147772. Is this the kind of thing you want? I'm not really clear what function you are trying to plot but perhaps that is simply my ignorance. – cfr Jul 05 '15 at 03:29
  • \addplot {sin(deg(x))}; This does not work if I include it in the code. – Sean N Jul 05 '15 at 03:32
  • or this: \addplot[samples=500,domain=0:2*pi]{sin(deg(x))}; – Sean N Jul 05 '15 at 03:33
  • Basically I want to put a trig graph in this code. – Sean N Jul 05 '15 at 03:35
  • But what are you trying to do with this code? Why can't you just use the plots stuff to plot the function you need? But you can add it if you put it in the axis environment as shown in that link. (And load the package, of course.) – cfr Jul 05 '15 at 03:38

1 Answers1

8

This is very basic. Obviously you need to adjust the tick labels etc. for your purposes. But you want code which looks something like this:

\documentclass[tikz, border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}% ref: http://tex.stackexchange.com/a/147772/ by Harish Kumar
\addplot
  [
    domain=-0:2*pi,
    smooth,
    blue,
    thick,
  ]
  plot {sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}

basic plot

EDIT

Here is a beginning which illustrates how to think about creating the kind of thing you want. That is, I try to demonstrate the approach you want to use here to make this work.

Basically, if you want to draw everything yourself so that you have complete and direct control over labelling, lines, grids etc., then you can do that. But you need in that case to plot the function yourself, too. Or you have to figure out a way to get the plotted function to align with the axes etc. Since that is exactly what \addplot etc. are designed to do, this seems to be reinventing the wheel, but it is, at the end of the day, your wheel.

On the other hand, if you want to be able to use the kind of syntax you indicated in your question to plot the function, then you need \addplot etc. and you need to use its facilities to configure the axes, labels etc. the way you want.

I used several references in working on this - some from the manual for pgfplots and some questions for this site. I've indicated the questions and their authors in the code as comments, and included pages for the most important points I used from the manual.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots,mathtools}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    [% 290 of manual
      xtick={-7.0686,-6.2831,...,7.0686},
      xticklabels={% ref:http://tex.stackexchange.com/a/34958/ by Peter Grill
        ,
        {$-2\pi$},
        {\tiny$-\frac{7\pi}{4}$},
        {\tiny$-\frac{3\pi}{2}$},
        {\tiny$-\frac{5\pi}{4}$},
        {$-\pi$},
        {\tiny$-\frac{3\pi}{4}$},
        {\tiny$-\frac{\pi}{2}$},
        {\tiny$-\frac{\pi}{4}$},
        {$0$},
        {\tiny$\frac{\pi}{4}$},
        {\tiny$\frac{\pi}{2}$},
        {\tiny$\frac{3\pi}{4}$},
        {$\pi$},
        {\tiny$\frac{5\pi}{4}$},
        {\tiny$\frac{3\pi}{2}$},
        {\tiny$\frac{7\pi}{4}$},
        {$2\pi$},
      },
      grid=major,
      x=10mm,
      y=20mm,
      axis x line=center,% 218
      axis y line=center,
      xlabel={$x$},
      ylabel={$y$},
      every tick label/.style={fill=white},% 309
    ]
  \addplot% ref: http://tex.stackexchange.com/a/147772/ by Harish Kumar
    [
      domain=-2.25*pi:2.25*pi,
      smooth,
      blue,
      thick,
    ]
    plot {sin(deg(x))};
  \end{axis}
\end{tikzpicture}
\end{document}

sin with cutomised look

cfr
  • 198,882
  • Thank you. But my question is, instead of graphing the parabola in this code, I'd like to graph a sine function. Is there a simple entry such as plot ({\x},{sin \x}); ? I tried it and it didn't work. And I'm sorry, I didn't mean to say that I wanted to graph the parabola. – Sean N Jul 05 '15 at 09:54
  • @SeanN See edit but now this just replicates the answer I linked to basically. (The edit is mine even though it says 'Community'. The system is confused.) Let me know if you cannot see the edit. – cfr Jul 05 '15 at 13:21
  • Now, is there a way I can for \addplot [ domain=-0:2*pi, smooth, blue, thick, ] plot {sin(deg(x))}; to work in my 'macro', from my original post? I modified/created a macro that has all the nodes and helplines in the format I want. I now just want to put a sine function in it without having to type \draw[ultra thick, red] (-2pi,0) sin (-3pi/2,1) cos (-pi,0) sin (-pi/2,-1) cos (0,0) sin (pi/2,1) cos (pi,0) sin (3pi/2,-1) cos (2pi,0); – Sean N Jul 05 '15 at 19:02
  • @SeanN It would be better to adapt the axis environment appropriately. How will you otherwise make sure things are lined up correctly? – cfr Jul 06 '15 at 22:36
  • 1
    @SeanN See edit. Let me know if you can't see the images. They've disappeared for me site-wide and so I can't tell. – cfr Jul 07 '15 at 01:01