3

I have the following code to generate a simple interval

\begin{tikzpicture}[scale=3, nodes={
       execute at begin node=$,
       execute at end node=$
     }]
\draw[-, thick] (-1,0) -- (1,0) node[above] {};
\foreach \x/\xpar/\xtext in {
    -0.8  / | / +,
    -0.6  / | / ,
    -0.4  / | / +,
    -0.2  / | / -,
     0    / | / ,
     .2   / | / -,
     .4   / | / +,
     .6   / | / -,
     .8   / | / +,   
}   \draw[thick] (\x,0pt) node {\xpar}  node[below=5pt] {\xtext};
\end{tikzpicture}

Which produces

Simple interval

Would there be a way for me to plot the sin function over this interval so that I get something like

Interval with sin(x)

3
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=3, nodes={
       execute at begin node=$,
       execute at end node=$
     }]
\draw[-, thick] (-1,0) -- (1,0) node[above] {};
\foreach \x/\xpar/\xtext in {
    -0.8  / | / +,
    -0.6  / | / ,
    -0.4  / | / +,
    -0.2  / | / -,
     0    / | / ,
     .2   / | / -,
     .4   / | / +,
     .6   / | / -,
     .8   / | / +,   
}   \draw[thick] (\x,0pt) node {\xpar}  node[below=5pt] {\xtext};
\draw[blue] plot[variable=\x,samples=100,domain=-1:1] (\x,{0.2*sin((\x-0.3)*900)});
\end{tikzpicture}
\end{document}

enter image description here

A simpler and perhaps nicer possibility:

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=3, nodes={
       execute at begin node=$,
       execute at end node=$
     }]
\draw[-, thick] (-1,0) -- (1,0) node[above] {};
\foreach \x/\xpar/\xtext in {-0.8,-0.6,...,0.8}
{\pgfmathtruncatemacro{\y}{sign(sin((\x-0.3)*900))}
\ifnum\y=1
\def\xtext{+}
\else
\def\xtext{-}
\fi
\draw (\x,-2pt) -- (\x,2pt);
\node at (\x,{-\y*5pt}) {\xtext};}
\draw[blue] plot[variable=\x,samples=100,domain=-1:1] (\x,{0.2*sin((\x-0.3)*900)});
\end{tikzpicture}
\end{document}

enter image description here

3

Some hacky golf code :

\documentclass[tikz,border=7pt]{standalone}
\begin{document}
  \tikz
    \draw[blue](,)foreach~in{+,-,+,-,+,-,+}
      {sin+(,~1)+(0,~-1)node[black,label={[scale=2]~-90:$~$}]{$|$}+(0,0)cos+(,~-1)}
      +(,0)edge[black,latex-](0,);
\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.