3

I am having trouble working with xtick and ytick functionality within \pgfplotsset tag which I use for axis manipulation for all my tikzpicture tags for sketching graphs.

I call this pair many times before the following code, which I am having trouble with.

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{bigints}
\usepackage{color}
\usepackage{tcolorbox}
\usepgfplotslibrary{fillbetween}
\begin{document}
\textbf{1}\\
\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
ylabel={$y=\cos 3x$},   % default put y on y-axis
ticks=both,
ytick={-1,0,1},
yticklabels={-1,0,1},
xtick={0,0.523,1.046,1.57},
xticklabels={0,$\frac{\pi}{6}$,$\frac{\pi}{3}$,$\frac{\pi}{2}$} 
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=2,
ymin=-2,ymax=2,
domain = 0:1.58
]
\plot[thick][samples=50,domain=0:1.57] {cos(3*deg(x)))};    
\addplot[dashed] expression {-1};
\addplot[dashed] expression {1};
\end{axis}
\end{tikzpicture}
\\
\\
\textbf{27}\enskip Since the gradient of the tangent is 1, we 
find all possible $x$ values for which $\dfrac{d}{dx}(x+\sin x)=1$ is satisfied.\\
$\dfrac{d}{dx}(x+\sin x)=1$ is equivalent to $1+\cos x=1$ which simplifies to $\cos x=0$. \\
\\
\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
ylabel={$y=\cos x$},   % default put y on y-axis
ticks=both,
ytick={-1,0,1},
yticklabels={-1,0,1},
xtick={0,1.57,3.14,4.71,6.28,7.85,9.42},
xticklabels={0,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$,$\frac{5\pi}{2}$,$3\pi$}   
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=9.6,
ymin=-2,ymax=2
]
\plot[thick][samples=50,domain=0:9.43] {cos(deg(x))};   
\addplot[dashed,domain=0:9.43] expression {-1};
\addplot[dashed,domain=0:9.43] expression {1};
\end{axis}
\end{tikzpicture}\\
As shown in the cosine graph above $x$ can take are $\dfrac{\pi}{2}$,$\dfrac{3\pi}{2}$ and $\dfrac{5\pi}{2}$.\\
\\
\textbf{30}\enskip The equation of the tangent to the curve $y=e^{x}$ at the point $(1,e)$ is $y-e=e^{1}(x-1)$ which is $y=ex$.\\
Since this tangent touches the curve $y=2\sqrt{x-k}$ at some point, let this point be $x=\alpha$. We must have
$$\begin{cases}
e\alpha=2\sqrt{\alpha-k} & (1)\\
e=\dfrac{1}{\sqrt{\alpha-k}} & (2)
\end{cases}$$
where (1) and (2) are from the fact that the line $y=ex$ being the tangent to curve $y=2\sqrt{x-k}$ at $x=\alpha$.\\
\\
Solving (1) and (2) simulataneously gives $k=\dfrac{1}{e^2}$.\\
\\
\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
ylabel={$y$},   % default put y on y-axis
ticks=none
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xmin=-0.4,xmax=3,
ymin=-1,ymax=5,
domain = 0:3
]
\plot[thick,brown][samples=50,domain=-0.5:1.57] {2.718^x};
\node [right] at (axis cs: 1.6, 4.8) {$y=e^{x}$};
\plot[thick,black][samples=50,domain=0:1.57] {2*sqrt{x-0.1353}};
\node [right] at (axis cs: 1.6, 4) {$y=ex$};
\plot[thick,red][samples=50,domain=-0.5:1.57] {2.718*x)};
\node [right] at (axis cs: 1.6, 2.3) {$y=2\sqrt{x-k}$};
\draw[style=dashed] (axis cs:1,0) -- (axis cs:1,2.71);
\draw[style=dashed] (axis cs:0,2.71) -- (axis cs:1,2.71);
\node [below] at (axis cs: 1,0) {$1$};
\draw[style=dashed] (axis cs:0.2707,0) -- (axis cs:0.2707,0.73575);
\end{axis}
\end{tikzpicture}
\newline
\end{center}
\textbf{3}
The following is the plot of the function $y=\dfrac{1}{x-3}+x$:\\\\
\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
ylabel={$y$},   % default put y on y-axis
ticks=none
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{tikzpicture}
\begin{axis}[
xmin=-1,xmax=7,
ymin=-10,ymax=20,
]
\plot[thick][samples=100,domain=-1:2.95] {1/(x-3)+x};
\plot[thick][samples=100,domain=3.05:7] {1/(x-3)+x};
\plot[dashed][samples=100,domain=-1:7] {x};
\draw[dashed] (axis cs:3,-10) -- (axis cs:3,20);
\node [below] at (axis cs: 3.2, 0) {$3$};
\draw[black,thick,fill=black] (axis cs: 0.3819,0) circle (0.4mm);
\node [below] at (axis cs: 0.6, 0) {$\frac{3-\sqrt{5}}{2}$};
\draw[black,thick,fill=black] (axis cs: 2.618,0) circle (0.4mm);
\node [below] at (axis cs: 2.2, 0) {$\frac{3+\sqrt{5}}{2}$};
\draw[black,thick,fill=black] (axis cs: 0,-0.333) circle (0.mm);
\node [left] at (axis cs: 0, -2) {$-\frac{1}{3}$};
\node [below] at (axis cs: 3.2, 0) {$3$};
\end{axis}
\end{tikzpicture}\\
We call the asymptote $y=x$ an \textbf{oblique}.\\\\
\\
\textbf{39}\\
\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
ylabel={$f(x)$},   % default put y on y-axis
ytick={-1,0,1,2,3,4,5},
yticklabels={$-1$,$0$,$1$,$2$,$3$,$4$,$5$},
xtick={-3,-2,-1,0,1,2,3,4,5,6},
xticklabels={$-3$,$-2$,$-1$,$0$,$1$,$2$,$3$,$4$,$5$,$6$}
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{tikzpicture}
\begin{axis}[
xmin=-4,xmax=7,
ymin=-2,ymax=5,
]
\plot[thick][samples=100,domain=-2.5:7] {4-(2)^(-x)};
\draw[dashed] (axis cs:-4,4) -- (axis cs:7,4);
\end{axis}
\end{tikzpicture}
\end{document}

The following is what I get:

enter image description here

However when I call this code on its own I get what I want correctly:

enter image description here

What is suppressing my xtick and ytick functions? I have checked that in the previous call for \pgfplotsset, where necessary I set ticks=\empty, if this helps.

  • We cannot know, because you don't show us the problematic output. You need to make an example that actually demonstrates the problem. – Torbjørn T. Mar 20 '16 at 10:52
  • The first image is the problematic output as it has no x-tick nor y-tick values. – Will Kim Mar 20 '16 at 10:54
  • And your code generates the second image. – Torbjørn T. Mar 20 '16 at 10:56
  • Both images are from the same code, but the difference is, the first one is the output when it is run with other codes that also contain \pgfplotsset and \tikzpicture. I wonder if I have disabled tick. I am sure before I call the particular graph, I set "ticks=\empty". – Will Kim Mar 20 '16 at 11:01
  • How can we possibly know what those "other codes" do when you don't show them to us? – Torbjørn T. Mar 20 '16 at 11:04
2

Instead of having a \pgfplotsset{every axis/...} (and \tikzset{>=stealth}) before every single axis, move the axis settings that are common to all the plots to the preamble, and add individual settings as options to the axis environments.

I only looked quickly, but I think these were the same in all the cases:

\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
}}

so I added that to the preamble (before \begin{document}). The remaining options for each diagram was added to the axis options, i.e. \begin{axis}[<options added here>].

Other comments:

  • You had loaded the pgfplots package twice, which isn't necessary. In addition, as pgfplots loads tikz, it isn't necessary to explicitly say \usepackage{tikz}. Similarly, mathtools loads amsmath.

  • In general you should never use \\ in running text. Generally it's better to add an empty line which starts a new paragraph. I guess you don't want indented paragraphs, so I added \usepackage{parskip} which turns off paragraph indentation, and instead adds some vertical space between paragraphs.

  • Don't use $$ .... $$ for display math. While it mostly works, it's better to use \[ ... \].

  • I would consider placing all tikzpicture environments in a center environment, as you've done with third to last diagram.


\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{parskip}

\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
}}
% arrows as stealth fighters
\tikzset{>=stealth}
\begin{document}
\textbf{1}

\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=2,
ymin=-2,ymax=2,
domain = 0:1.58,
ylabel={$y=\cos 3x$},   % default put y on y-axis
ticks=both,
ytick={-1,0,1},
yticklabels={-1,0,1},
xtick={0,0.523,1.046,1.57},
xticklabels={0,$\frac{\pi}{6}$,$\frac{\pi}{3}$,$\frac{\pi}{2}$}]
\plot[thick][samples=50,domain=0:1.57] {cos(3*deg(x)))};    
\addplot[dashed] expression {-1};
\addplot[dashed] expression {1};
\end{axis}
\end{tikzpicture}

\textbf{27}\enskip Since the gradient of the tangent is 1, we 
find all possible $x$ values for which $\dfrac{d}{dx}(x+\sin x)=1$ is satisfied.

$\dfrac{d}{dx}(x+\sin x)=1$ is equivalent to $1+\cos x=1$ which simplifies to $\cos x=0$. 

\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=9.6,
ymin=-2,ymax=2,
ylabel={$y=\cos x$},   % default put y on y-axis
ticks=both,
ytick={-1,0,1},
yticklabels={-1,0,1},
xtick={0,1.57,3.14,4.71,6.28,7.85,9.42},
xticklabels={0,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$,$\frac{5\pi}{2}$,$3\pi$}
]
\plot[thick][samples=50,domain=0:9.43] {cos(deg(x))};   
\addplot[dashed,domain=0:9.43] expression {-1};
\addplot[dashed,domain=0:9.43] expression {1};
\end{axis}
\end{tikzpicture}

As shown in the cosine graph above $x$ can take are $\dfrac{\pi}{2}$,$\dfrac{3\pi}{2}$ and $\dfrac{5\pi}{2}$.


\textbf{30}\enskip The equation of the tangent to the curve $y=e^{x}$ at the point $(1,e)$ is $y-e=e^{1}(x-1)$ which is $y=ex$.

Since this tangent touches the curve $y=2\sqrt{x-k}$ at some point, let this point be $x=\alpha$. We must have
\[
\begin{cases}
e\alpha=2\sqrt{\alpha-k} & (1)\\
e=\dfrac{1}{\sqrt{\alpha-k}} & (2)
\end{cases}
\]
where (1) and (2) are from the fact that the line $y=ex$ being the tangent to curve $y=2\sqrt{x-k}$ at $x=\alpha$.


Solving (1) and (2) simulataneously gives $k=\dfrac{1}{e^2}$.
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xmin=-0.4,xmax=3,
ymin=-1,ymax=5,
domain = 0:3,
ylabel={$y$},   % default put y on y-axis
ticks=none
]
\plot[thick,brown][samples=50,domain=-0.5:1.57] {2.718^x};
\node [right] at (axis cs: 1.6, 4.8) {$y=e^{x}$};
\plot[thick,black][samples=50,domain=0:1.57] {2*sqrt{x-0.1353}};
\node [right] at (axis cs: 1.6, 4) {$y=ex$};
\plot[thick,red][samples=50,domain=-0.5:1.57] {2.718*x)};
\node [right] at (axis cs: 1.6, 2.3) {$y=2\sqrt{x-k}$};
\draw[style=dashed] (axis cs:1,0) -- (axis cs:1,2.71);
\draw[style=dashed] (axis cs:0,2.71) -- (axis cs:1,2.71);
\node [below] at (axis cs: 1,0) {$1$};
\draw[style=dashed] (axis cs:0.2707,0) -- (axis cs:0.2707,0.73575);
\end{axis}
\end{tikzpicture}
\end{center}
\textbf{3}
The following is the plot of the function $y=\dfrac{1}{x-3}+x$:

\begin{tikzpicture}
\begin{axis}[
xmin=-1,xmax=7,
ymin=-10,ymax=20,
ylabel={$y$},   % default put y on y-axis
ticks=none
]
\plot[thick][samples=100,domain=-1:2.95] {1/(x-3)+x};
\plot[thick][samples=100,domain=3.05:7] {1/(x-3)+x};
\plot[dashed][samples=100,domain=-1:7] {x};
\draw[dashed] (axis cs:3,-10) -- (axis cs:3,20);
\node [below] at (axis cs: 3.2, 0) {$3$};
\draw[black,thick,fill=black] (axis cs: 0.3819,0) circle (0.4mm);
\node [below] at (axis cs: 0.6, 0) {$\frac{3-\sqrt{5}}{2}$};
\draw[black,thick,fill=black] (axis cs: 2.618,0) circle (0.4mm);
\node [below] at (axis cs: 2.2, 0) {$\frac{3+\sqrt{5}}{2}$};
\draw[black,thick,fill=black] (axis cs: 0,-0.333) circle (0.mm);
\node [left] at (axis cs: 0, -2) {$-\frac{1}{3}$};
\node [below] at (axis cs: 3.2, 0) {$3$};
\end{axis}
\end{tikzpicture}

We call the asymptote $y=x$ an \textbf{oblique}.


\textbf{39}

\begin{tikzpicture}
\begin{axis}[
xmin=-4,xmax=7,
ymin=-2,ymax=5,
ylabel={$f(x)$},   % default put y on y-axis
ytick={-1,0,1,2,3,4,5},
yticklabels={$-1$,$0$,$1$,$2$,$3$,$4$,$5$},
xtick={-3,-2,-1,0,1,2,3,4,5,6},
xticklabels={$-3$,$-2$,$-1$,$0$,$1$,$2$,$3$,$4$,$5$,$6$}
]
\plot[thick][samples=100,domain=-2.5:7] {4-(2)^(-x)};
\draw[dashed] (axis cs:-4,4) -- (axis cs:7,4);
\end{axis}
\end{tikzpicture}
\end{document}
  • Thank you so much for a detailed answer Torbjon! It worked beautifully! – Will Kim Mar 20 '16 at 11:38

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.