4

Originally in my code, I had

xtick={-2*pi, -(3/2)*pi, -(1/2)*pi, (1/2)*pi,(3/2)*pi, 2*pi},

to put tick marks at integral multiples of $pi/2$. It would not compile. I had to replace it with cumbersome decimal approximations. Why won't the code compile with expressions like pi? To specify width of the x-axis in the axis environment, I used xmin=-2*pi, and xmax=2*pi,, and to specify the domain of the cosine function, I used domain=-2*pi:2*pi.

\documentclass{amsart}
\usepackage{mathtools}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings,backgrounds,patterns}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}



\begin{document}

\begin{tikzpicture}
\begin{axis}[width=4.5in,clip=false,
    xmin=-2*pi,xmax=2*pi,
    ymin=-1.25,ymax=1.25,
    axis lines=middle,
    %xtick={-2*pi, -(3/2)*pi, -pi, -(1/2)*pi, (1/2)*pi, pi, (3/2)*pi, 2*pi},
    xtick={-6.28318, -4.7123889, -3.14159, -1.5708, 1.5708, 3.14159, 4.7123889, 6.28318},
    xticklabel style={font=\footnotesize,fill=white},
    xticklabels={$-2\pi$,,$-\pi$,,,$\pi$,,$2\pi$},
    ytick={-1},
    yticklabel style={font=\footnotesize,fill=white},
    yticklabels={$-1$},
    extra y ticks={1},
    extra y tick labels={$1$},
    extra y tick style={yticklabel style={font=\footnotesize,fill=white,anchor=south east}},
    ticklabel style={font=\footnotesize,fill=white},
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot [samples=501,mark=none, thick, red,domain=-2*pi:2*pi] {cos(deg(x))};

\end{axis}
\end{tikzpicture}

\end{document}
Adelyn
  • 3,373
  • 1
    I don't have the answer but you can reduce the number of calculations by using xtick={-6.28318, -4.7123889, ..., 6.28318}. – anderstood Jun 12 '15 at 17:33

4 Answers4

9

With the release of PGFPlots v1.16 it is now possible to use the math parser at several places. And thus you can use your provided xtick line now ...

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xtick={-2*pi, -(3/2)*pi, -pi, -(1/2)*pi, (1/2)*pi, pi, (3/2)*pi, 2*pi},
%        xtick={-6.28318, -4.7123889, -3.14159, -1.5708, 1.5708, 3.14159, 4.7123889, 6.28318},
        xticklabel style={font=\footnotesize,fill=white},
        xticklabels={$-2\pi$,,$-\pi$,,,$\pi$,,$2\pi$},
        ytick={-1,1},
        axis lines=middle,
        domain=-2*pi:2*pi,
        samples=501,
        enlargelimits=true,
    ]
        \addplot [thick,red] {cos(deg(x))};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
5

The list can be evaluated separately via \pgfmathparse or \pgfmathsetmacro. The comma list is converted to an array, where the calculated numbers are put into curly braces. Thus the following example defines macro \ArrayToCommaList to get a comma separated list again:

\documentclass{amsart}
\usepackage{mathtools}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings,backgrounds,patterns}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\makeatletter
\newcommand*{\ArrayToCommaList}[1]{%
  \let\ArrayToCommaList@Result\@empty
  \expandafter\@tfor\expandafter\ArrayToCommaList@Entry
  \expandafter:\expandafter=#1\do{%
    \edef\ArrayToCommaList@Result{%
      \ArrayToCommaList@Result,\ArrayToCommaList@Entry
    }%
  }%
  % Remove first comma
  \edef#1{%
    \expandafter\@cdr\ArrayToCommaList@Result\@empty\@nil
  }%
}
\makeatother

\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro\xticklist{-2*pi, -(3/2)*pi, -pi, -(1/2)*pi,
  (1/2)*pi, pi, (3/2)*pi, 2*pi}
\ArrayToCommaList\xticklist
\begin{axis}[width=4.5in,clip=false,
    xmin=-2*pi,xmax=2*pi,
    ymin=-1.25,ymax=1.25,
    axis lines=middle,
    xtick=\xticklist,
    xticklabel style={font=\footnotesize,fill=white},
    xticklabels={$-2\pi$,,$-\pi$,,,$\pi$,,$2\pi$},
    minor x tick num=2,
    ytick={-1},
    yticklabel style={font=\footnotesize,fill=white},
    yticklabels={$-1$},
    extra y ticks={1},
    extra y tick labels={$1$},
    extra y tick style={yticklabel style={font=\footnotesize,fill=white,anchor=south east}},
    ticklabel style={font=\footnotesize,fill=white},
    axis line style={latex-latex},
    axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot [samples=501,mark=none, thick, red,domain=-2*pi:2*pi] {cos(deg(x))};

\end{axis}
\end{tikzpicture}
\end{document}

Result

Heiko Oberdiek
  • 271,626
  • Forget the list. Suppose that I wanted to make a tick mark at $\pi/2$. Why can't I have something like xtick={(1/2)*pi}, in my code? – Adelyn Jun 12 '15 at 17:48
  • 1
    @Adelyn xtick expects a plain floating point number, calculations are not supported. Make a feature request. – Heiko Oberdiek Jun 12 '15 at 17:53
  • Please tell me the procedure to make a feature request. – Adelyn Jun 12 '15 at 19:10
  • @Adelyn Go to http://sourceforge.net/p/pgfplots/feature-requests/ and submit your request. You might need an account on Sourceforge though. – Henri Menke Jun 12 '15 at 20:12
  • 5
    I am the author of pgfplots and I accept this as feature request. The reason for the missing math parser support is merely legacy: back in the past, there was no math parser with the required precision. In the meantime, it was either accepted or forgotten. I'll revise the math parsing of input arguments as feature request; this will affect other keys as well. – Christian Feuersänger Jun 13 '15 at 09:23
  • @ChristianFeuersänger --- I was looking at the feature list and see no mention of this... has been already added? Thanks! – Rmano Mar 22 '17 at 09:57
  • @ Rmano the pgfplotstodo.tex in text form contained as associated item. I have converted it into a sourceforge feature request: https://sourceforge.net/p/pgfplots/feature-requests/69/ – Christian Feuersänger Mar 25 '17 at 06:41
3

run with xelatex

\documentclass{amsart}
\usepackage{pst-plot}    
\begin{document}

\psset{yunit=3}
\begin{pspicture}(-6.75,-1.3)(6.75,1.3)
\psframe*[linecolor=yellow!80!black!20](-6.7,-1.3)(6.7,1.3)
\psaxes[trigLabels,dx=3.14]{<->}(0,0)(-6.6,-1.25)(6.6,1.25)
\psplot[algebraic,linewidth=1.5pt,
        linecolor=red,plotpoints=1000]{-6.28}{6.28}{cos(x)}%
\end{pspicture}

\end{document}

enter image description here

And with ticks every pi/2:

\psaxes[trigLabels,trigLabelBase=2,dx=1.57]{<->}(0,0)(-6.6,-1.25)(6.6,1.25)

enter image description here

0

enter image description here

\documentclass[12pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\usepackage{mathrsfs}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\draw[->,color=black] (-4.3,0.) -- (5.1,0.);
\draw[shift={(-3.14,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $-\pi$};
\draw[shift={(-1.6,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $-\pi/2$};
\draw[shift={(1.6,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\pi/2$};
\draw[shift={(3.14,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\pi$};
\draw[shift={(4.7,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $3\pi/2$};
\draw[->,color=black] (0.,-2.11) -- (0.,1.33);
\foreach \y in {-2.,-1.,1.}
\draw[shift={(0,\y)},color=black] (.5pt,0pt) -- (-.5pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\draw[line width=2.pt,color=magenta,smooth,samples=500,domain=-5:5] plot(\x,{cos(((\x))*180/pi)});
\begin{scriptsize}
\draw[color=magenta] (-4.6,.5) node {$f(x)=\cos x$};
\end{scriptsize}
\end{tikzpicture}
\end{document}
Sebastiano
  • 54,118