# Label for tick mark interferes with graph

I have a plot of a rational function. On this plot, tick marks along the x-axis at $-\sqrt{3}$ and $\sqrt{3}$ are placed. Labels for them are typeset with the options

extra x ticks={-1.732, 1.732},
extra x tick labels={\hspace*{-1pt}\makebox[0pt][r]{$-$}$\sqrt{3}$,
$\sqrt{3}$\hspace*{-5pt}},
extra x tick style={font=\tiny, circle, fill=white, inner sep=0.5pt},


in the axis environment. Why is the graph drawn through the label for $-\sqrt{3}$?

\documentclass{amsart}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}

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

\usepackage{mathtools,array}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5in, axis equal image, axis on top,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-11,ymax=10,
restrict y to domain=-11:10,
xtick={\empty},ytick={\empty},
extra x ticks={-1.732, 1.732},
extra x tick labels={\hspace*{-5pt}\makebox[0pt][r]{$-$}$\sqrt{3}$, $\sqrt{3}$\hspace*{-5pt}},
extra x tick style={font=\tiny, circle, fill=white, inner sep=0pt},
xlabel=$x$,ylabel=$y$,
axis line style={latex-latex},
axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
xlabel style={at={(ticklabel* cs:1)}, xshift=7.5pt, anchor=north west},
ylabel style={at={(ticklabel* cs:1)}, yshift=7.5pt, anchor=south west}
]

\addplot[samples=501,domain=-9:-1.1789,blue] {(x^(2) - 3)/ (x + 1)} node[anchor=east,pos=0.9,font=\footnotesize]{$y=f(x)$};
\addplot[samples=501,domain=-0.72508:9,blue] {(x^(2) - 3)/ (x + 1)};

\addplot [dashed, latex-latex,domain=-10:10] {x - 1} node [pos=0.9, anchor=south, font=\footnotesize, sloped]{$y=x-1$};
\addplot [dashed, latex-latex,domain=-10:10] (-1,x) node [pos=0.1, anchor=south, font=\footnotesize, sloped]{$x=-1$};

\end{axis}
\end{tikzpicture}

\end{document}

• I'm not sure what you expect. pgfplots just places ticklabels right below the ticks on the x-axis, it doesn't move them around to avoid plotlines. Are there any plotting tools at all that do that? – Torbjørn T. Aug 25 '16 at 7:27
• @Torbjørn T. I don't want the labels for the tick marks to be moved. I don't want the graph to be drawn over them. I thought that fill=white, inner sep=0.5pt would interrupt the graph. – user74973 Aug 25 '16 at 18:59
• Sorry, my bad. See my answer though. – Torbjørn T. Aug 25 '16 at 20:55

I don't know why, but axis on top doesn't seem to affect extra x ticks. However, in this case there is no need to use extra x ticks, just set and modify the standard ticks.

You may want to modify the inner sep, but that fine tuning I'll leave to you.

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=5in, axis equal image,axis on top,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-11,ymax=10,
restrict y to domain=-11:10,
ytick={\empty},
xtick={-1.732, 1.732},
xticklabels={\hspace*{-5pt}\makebox[0pt][r]{$-$}$\sqrt{3}$, $\sqrt{3}$\hspace*{-5pt}},
xticklabel style={font=\tiny, circle, fill=white, inner sep=0pt},
xlabel=$x$,ylabel=$y$,
axis line style={latex-latex},
axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
xlabel style={at={(ticklabel* cs:1)}, xshift=7.5pt, anchor=north west},
ylabel style={at={(ticklabel* cs:1)}, yshift=7.5pt, anchor=south west}
]

\addplot[samples=501,domain=-9:-1.1789,blue] {(x^(2) - 3)/ (x + 1)} node[anchor=east,pos=0.9,font=\footnotesize]{$y=f(x)$};
\addplot[samples=501,domain=-0.72508:9,blue] {(x^(2) - 3)/ (x + 1)};

\addplot [dashed, latex-latex,domain=-10:10] {x - 1} node [pos=0.9, anchor=south, font=\footnotesize, sloped]{$y=x-1$};
\addplot [dashed, latex-latex,domain=-10:10] (-1,x) node [pos=0.1, anchor=south, font=\footnotesize, sloped]{$x=-1$};

\end{axis}
\end{tikzpicture}

\end{document}

• Well, you gave me the plot that I wanted. Thanks. (It must be an oversight in pgfplots that axis on top does not apply to extra x ticks.) – user74973 Aug 27 '16 at 19:27
• My concern is that I will have a similar situation in which I will want two tick marks along the x-axis placed using different anchors - like anchor=north east and anchor=north west. I would have to use the extra x ticks. – user74973 Aug 27 '16 at 19:27
• I won't worry about that now. – user74973 Aug 27 '16 at 19:27
• Well, you could use the suggestion by Ross below for that. Though you can't set the anchors to north east and north west directly, you can shift individual ticklabels by varying amounts. – Torbjørn T. Aug 27 '16 at 19:54

You can define only -1.732 as extra x ticks and 1.732 as a simple x tick, in this case you can define anchor for xticklabels separately and avoid overlap like this

xtick={1.732},
xticklabels={$\sqrt{3}$},
extra x ticks={-1.732},
extra x tick labels={$-\sqrt{3}$},
xticklabel style={font=\tiny, inner sep=0pt,below right},
extra x tick style={font=\tiny, inner sep=0pt,xticklabel style={below left,xshift=-3pt}},


Code

\documentclass{amsart}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}

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

\usepackage{mathtools,array}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5in, axis equal image, axis on top,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-11,ymax=10,
restrict y to domain=-11:10,
ytick={\empty},
xtick={1.732},
xticklabels={$\sqrt{3}$},
extra x ticks={-1.732},
extra x tick labels={$-\sqrt{3}$},
xticklabel style={font=\tiny, inner sep=0pt,below right},
extra x tick style={font=\tiny, inner sep=0pt,xticklabel style={below left,xshift=-3pt}},
xlabel=$x$,ylabel=$y$,
axis line style={latex-latex},
axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
xlabel style={at={(ticklabel* cs:1)}, xshift=7.5pt, anchor=north west},
ylabel style={at={(ticklabel* cs:1)}, yshift=7.5pt, anchor=south west}
]

\addplot[samples=501,domain=-9:-1.1789,blue] {(x^(2) - 3)/ (x + 1)} node[anchor=east,pos=0.9,font=\footnotesize]{$y=f(x)$};
\addplot[samples=501,domain=-0.72508:9,blue] {(x^(2) - 3)/ (x + 1)};

\addplot [dashed, latex-latex,domain=-10:10] {x - 1} node [pos=0.9, anchor=south, font=\footnotesize, sloped]{$y=x-1$};
\addplot [dashed, latex-latex,domain=-10:10] (-1,x) node [pos=0.1, anchor=south, font=\footnotesize, sloped]{$x=-1$};

\end{axis}
\end{tikzpicture}

\end{document}


Result

• I like the placement of the labels using the code in my post. Why doesn't fill=white, inner sep=0.5pt interrupt the plotting of the function? – user74973 Aug 25 '16 at 19:02

Another approach, which provides flexibility for moving individual tick labels, is to assign an appropriate xshift or yshift factor. The shift can be applied using an expresssion, in this case, by testing whether the tick label is the first tick (\ticknum == 0) or second tick (\ticknum == 1). This is done using:

    xticklabel style={xshift={(\ticknum == 0 ? "-18pt" : 0)},
xshift={(\ticknum == 1 ? "2pt" : 0)}},


This shifts the label at x=-1.732 18pt to the left and the label at x=+1.732 2pt to the right, giving:

The complete code is:

\documentclass{amsart}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}

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

\usepackage{mathtools,array}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5in, axis equal image, axis on top,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-11,ymax=10,
restrict y to domain=-11:10,
ytick={\empty},
xtick={-1.732,1.732},
xticklabels={{$-$}$\sqrt{3}$,$\sqrt{3}$},
xticklabel style={xshift={(\ticknum == 0 ? "-18pt" : 0)},xshift={(\ticknum == 1 ? "2pt" : 0)}},
xlabel=$x$,ylabel=$y$,
axis line style={latex-latex},
axis line style={shorten >=-7.5pt, shorten <=-7.5pt},
xlabel style={at={(ticklabel* cs:1)}, anchor=north east},
ylabel style={at={(ticklabel* cs:1)}, anchor=south west}
]

\addplot[samples=501,domain=-9:-1.1789,blue] {(x^(2) - 3)/ (x + 1)} node[anchor=east,pos=0.9,font=\footnotesize]{$y=f(x)$};
\addplot[samples=501,domain=-0.72508:9,blue] {(x^(2) - 3)/ (x + 1)};

\addplot [dashed, latex-latex,domain=-10:10] {x - 1} node [pos=0.9, anchor=south, font=\footnotesize, sloped]{$y=x-1$};
\addplot [dashed, latex-latex,domain=-10:10] (-1,x) node [pos=0.1, anchor=south, font=\footnotesize, sloped]{$x=-1$};

\end{axis}
\end{tikzpicture}

\end{document


}