1

I need to plot a variety of sine waves and almost have the graph looking exactly how I want. However, I am unable to remove the 0π or make it look like 3π/2.

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
  \pgfkeys{/pgf/number format/precision=2}
  \pgfmathparse{#1/pi}
  \pgfmathroundtozerofill{\pgfmathresult}
  \pgfmathifisint{\pgfmathresult}{
    \pgfmathprintnumber{\pgfmathresult}$\pi$%}
  }{
    \pgfmathprintnumber[/pgf/number format/frac, frac whole=false]{\pgfmathresult}$\pi$
  }
}

\begin{document}
  \begin{tikzpicture}
    \datavisualization [
      school book axes,
      all axes={
        grid={
          major={style={red!50!black, opacity=.25}},
          minor={style={green!25!black, opacity=.25}},
          minor steps between steps=3,
      }},
      x axis={
        label=$\phi$,
        ticks and grid={
          stack,
          step=(2*pi),
          tick typesetter/.code=\mytypesetter{##1},
      }},
      y axis={
        label=$v$,
        grid={step=1}
      },
      style sheet=vary hue,
      visualize as line/.list={sin1}
    ]
    data [set=sin1, format=function] {
      var x : interval [-2*pi:2*pi] samples 100;
      func y = sin(\value x/2 r);
    };
  \end{tikzpicture}
\end{document}
3

Welcome! You can switch the 0\pi off as follows.

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
  \pgfkeys{/pgf/number format/precision=2}
  \pgfmathparse{#1/pi}
  \pgfmathroundtozerofill{\pgfmathresult}
  \pgfmathifisint{\pgfmathresult}{\pgfmathtruncatemacro{\itest}{\pgfmathresult}%
  \unless\ifnum\itest=0
    \pgfmathprintnumber{\pgfmathresult}$\pi$%
  \fi
  }{
    \pgfmathprintnumber[/pgf/number format/frac, frac whole=false]{\pgfmathresult}$\pi$
  }
}

\begin{document}
  \begin{tikzpicture}
    \datavisualization [
      school book axes,
      all axes={
        grid={
          major={style={red!50!black, opacity=.25}},
          minor={style={green!25!black, opacity=.25}},
          minor steps between steps=3,
      }},
      x axis={
        label=$\phi$,
        ticks and grid={
          stack,
          step=(2*pi),
          tick typesetter/.code=\mytypesetter{##1},
      }},
      y axis={
        label=$v$,
        grid={step=1}
      },
      style sheet=vary hue,
      visualize as line/.list={sin1}
    ]
    data [set=sin1, format=function] {
      var x : interval [-2*pi:2*pi] samples 100;
      func y = sin(\value x/2 r);
    };
  \end{tikzpicture}
\end{document}

enter image description here

Or a variant that suppresses factors of magnitude 1.

\documentclass[tikz, border=5mm]{standalone}

\usetikzlibrary{datavisualization.formats.functions, arrows}

\def\mytypesetter#1{
  \pgfkeys{/pgf/number format/precision=2}
  \pgfmathparse{#1/pi}
  \pgfmathroundtozerofill{\pgfmathresult}
  \pgfmathifisint{\pgfmathresult}{\pgfmathtruncatemacro{\itest}{\pgfmathresult}%
  \unless\ifnum\itest=0
    \ifnum\itest=1
     $\pi$%
    \else
     \ifnum\itest=-1
      $-\pi$%
     \else
      \pgfmathprintnumber{\pgfmathresult}$\pi$%
     \fi
    \fi
  \fi
  }{
    \pgfmathprintnumber[/pgf/number format/frac, frac whole=false]{\pgfmathresult}$\pi$
  }
}

\begin{document}
  \begin{tikzpicture}
    \datavisualization [
      school book axes,
      all axes={
        grid={
          major={style={red!50!black, opacity=.25}},
          minor={style={green!25!black, opacity=.25}},
          minor steps between steps=3,
      }},
      x axis={
        label=$\phi$,
        ticks and grid={
          stack,
          step=(pi),
          tick typesetter/.code=\mytypesetter{##1},
      }},
      y axis={
        label=$v$,
        grid={step=1}
      },
      style sheet=vary hue,
      visualize as line/.list={sin1}
    ]
    data [set=sin1, format=function] {
      var x : interval [-2*pi:2*pi] samples 100;
      func y = sin(\value x/2 r);
    };
  \end{tikzpicture}
\end{document}

enter image description here

3
  • Perfect! Thank you so much! Curious though, if I change (step=pi), it will say 1pi. Minor I know, but is there an easy way to also remove that 1?
    – Michael
    Feb 25 '20 at 20:36
  • @Michael I added that, too. You only need to add some \ifnum statements.
    – user194703
    Feb 25 '20 at 22:01
  • Ah, got it! Thanks again for helping out a latex newbie! I appreciate it.
    – Michael
    Feb 27 '20 at 21:09

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.