# Labeled horizontal tick marks for sine wave in tikz

I'm looking to generate sine waves in tikz which go in increments of pi/6 (or some other radian increments). I would like the tick mark labels to be in reduced fraction form with no coefficient in front of the pi's when it's unnecessary.

Here is what I created myself:

        \documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns, shapes, arrows}
\usetikzlibrary{calc}
\usepackage{tkz-euclide}
\usetkzobj{all}

\begin{document}
$\begin{tikzpicture}[scale=2] \draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {x}; \draw[->,thick] (0,-1.25) -- (0, 1.25) node[above] {y}; \foreach \s in {-12,-11,...,12} \draw[thin, gray] ({0.524*\s}, -1.25) -- ({0.524*\s}, 1.25); \foreach \t in {-1,-1/2,1/2,1} \draw[thin, gray] (-6.5,\t) -- (6.5,\t); \foreach \u in {-12,-11,...,12} \draw ({0.524*\u}, -0.1) -- ({0.524*\u}, 0.1); \foreach \v in {-1,-1/2,1/2,1} \draw (0.1,\v) -- (-0.1,\v) node[fill=white, left] {\v}; \foreach \w in {-11,-7,-5,5,7,11} \node[fill=white, below] at ({0.524*\w}, -0.1){\frac{\w\pi}{6}}; \foreach \x in {-5,-4,-2,2,4,5} \node[fill=white, below] at ({1.047*\x}, -0.1) {\frac{\x\pi}{3}}; \foreach \y in {-3,3} \node[fill=white, below] at ({1.571*\y}, -0.1) {\frac{\y\pi}{2}}; \node[fill=white, below] at (-3.142,-0.1) {-\pi}; \node[fill=white, below] at (3.142,-0.1) {\pi}; \foreach \z in {-2,2} \node[fill=white, below] at ({3.142*\z}, -0.1) {\z\pi}; \node[fill=white, below] at (-1.571,-0.1) {-\frac{\pi}{2}}; \node[fill=white, below] at (1.571,-0.1) {\frac{\pi}{2}}; \node[fill=white, below] at (-1.047,-0.1) {-\frac{\pi}{3}}; \node[fill=white, below] at (1.047,-0.1){\frac{\pi}{3}}; \node[fill=white, below] at (-0.524,-0.1) {-\frac{\pi}{6}}; \node[fill=white, below] at (0.524,-0.1) {\frac{\pi}{6}}; \draw[<->,thick,domain=-6.5:6.5,samples=300,variable=\x] plot ({\x},{sin(57.296*\x)}); \end{tikzpicture}$

\end{document}


As you can see, this was a lot of work, which is fine, but I'm planning to generate a lot of graphs like this. Due to the ubiquity of sine graphs, it seems like someone would have come up with something easier.

One further comment: I'm aware that pspicture can be used to generate fantastic sine graphs (e.g. Graphing trigonometric functions, axes labeled with tick marks). But I have two problems (1) I plead ignorance on the functionality of the pspicture package, and (2) it seems to require graph specific code in the preamble which could turn into quite a mess if a lot of graphs are generated.

Like this (pure TikZ solution):

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, patterns, shapes, arrows}
%        \usepackage{tkz-euclide}
%        \usetkzobj{all}

\begin{document}
\begin{tikzpicture}[
tl/.style = {% tick labels
fill=white, inner sep=1pt, font=\scriptsize,
},
]
% grid
\draw[gray, very thin, xstep=0.5235, ystep=0.5] (-6.6,-1.2) grid (6.6,1.2);

% y tick label
\foreach \y in {-1, -1/2, 1/2, 1}{\node[tl,left=1mm] at (0,\y) {$\y$};}
% x tick label
\foreach \x [count=\xx from -4] in
{-2\pi,          %-\frac{11\pi}{2}, -\frac{5\pi}{3},
-\frac{3\pi}{2},%-\frac{4\pi}{3},  -\frac{7\pi}{6},
-\pi,           %-\frac{5\pi}{6},  -\frac{2\pi}{3},
-\frac{\pi}{2}, %-\frac{4\pi}{3},  -\frac{7\pi}{6},
{ },
%\frac{\pi}{6},  \frac{\pi}{3},
\frac{\pi}{2},
%\frac{2\pi}{3}, \frac{5\pi}{6},
\pi,           %-\frac{7\pi}{6},   \frac{4\pi}{3},
\frac{3\pi}{2}, %-\frac{5\pi}{3},  \frac{11\pi}{6},
2\pi
}{\node[tl,below=1mm] at (3*0.5235*\xx,0) {$\x$};}
% axes
\draw[->,thick] (-6.5,0) -- (6.5,0) node[right] {$x$};
\draw[->,thick] (0,-1.25) -- (0, 1.25) node[above] {$y$};
% curve
\draw[<->,thick,draw=red,
domain=-6.5:6.5,samples=300,variable=\x]
plot (\x,{sin(deg{\x})});
\end{tikzpicture}
\end{document}


However, this can be also done by pgfplots.

• Very nice! Thank you so much! I'm still learning LaTeX features, despite having casually used it for nine years. – Charles0349 Sep 11 '16 at 3:00
• I'm glad to help you. Hopefully the code in my MWE (minimal working example) is enough self-explanatory ... – Zarko Sep 11 '16 at 9:22