Style to make axis tick label opaque

I would like to make a specific tick label have a white opaque fill around it, so that the a line does not cross over it. Ideally I would like to be able to specify exactly which tick, but do not have an elegant method to do that, so below I attempted to use:

\pgfkeys{/pgfplots/My Y Tick Label/.append style={
yticklabel style={draw=none, inner sep=0pt, fill=white, text opacity=1}
}
}


to apply to all the yticklabels. This achieves the effect I want (note the y=-2 label):

But it also has the unwanted side effect of shifting the yticklabel to the right. Well that to me seemed like I just need to use .append style instead of .style so that the prior settings don't get thrown away:

\pgfkeys{/pgfplots/My Y Tick Label Append/.append style={
yticklabel/.append style={draw=none, inner sep=0pt, fill=white, text opacity=1}
}
}


but using this seems to have absolutely no effect.

Questions:

1. How do I get an opaque tick label without adjusting its location?
2. Is there a way where I can apply this only on a specific tick label?
3. Is there perhaps a better way of doing this? I thought about changing the drawing of the asymptote but that is a bit more complicated.

Code:

\documentclass{article}
\usepackage{pgfplots}

\newcommand{\XMin}{-2}%
\newcommand{\XMax}{2}%

\pgfkeys{/pgfplots/My Axis Style/.append style={
width=6cm, height=8cm,
xmin=\XMin, xmax=\XMax, ymin=-3.2, ymax=5,
axis y line=center,
axis x line=middle,
axis on top=true,
ytick={       -4,  -3,   -2, -1,  1,  2,   3,  4},
yticklabels={$-4$, $$, -2,$$, $$, 2,$$, $4$},
}
}

\newcommand*{\AddPlotFunction}{\addplot[smooth, ultra thick, domain=\XMin:\XMax, red]  {exp(x)-2} node [left] {$y=e^x-2$}}%
\newcommand*{\AddPlotAsymptote}{\addplot[smooth, thin,        domain=\XMin:\XMax, blue] {-2} node [below left] {$y=-2$}}%

%---------- Above is to improve readability of MWE

\pgfkeys{/pgfplots/My Y Tick Label/.append style={
yticklabel style={draw=none, inner sep=0pt, fill=white, text opacity=1}
}
}

\pgfkeys{/pgfplots/My Y Tick Label Append/.append style={
yticklabel/.append style={draw=none, inner sep=0pt, fill=white, text opacity=1}
}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[My Axis Style,
%My Y Tick Label,%        This shifts the labels to right
%My Y Tick Label Append,% This has no effect
]

\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[My Axis Style,
My Y Tick Label,%        This shifts the labels to right
%My Y Tick Label Append,% This has no effect
]

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

-
You can win back the shift effect by also adding an outer xsep=3pt. –  percusse Mar 28 '12 at 17:18
Perhaps not the most elegant solution, but you could use yticklabels={$-4$, $$, \colorbox{white}{-2},$$, $$, 2,$$, $4$} when defining the labels. –  Gonzalo Medina Mar 28 '12 at 17:40
@percusse: Well that gets it very close, but there is still some shift. Would be better to append the style in case there are other adjustments necessary as well. –  Peter Grill Mar 28 '12 at 18:27
@GonzaloMedina: Yep that works. I also added \setlength{\fboxsep}{0pt} so as to minimize the effect on any possible drawings nearby. You should make that an answer as that is perfectly useable for the case where one wants to apply that to a particular tick label. –  Peter Grill Mar 28 '12 at 18:29
Perhaps the extra y tick feature helps here? It allows to provide one or more custom ticks - with its own set of styles...? –  Christian Feuersänger Mar 28 '12 at 19:35

Instead of using an explicit yticklabels list, I would use ytick={-4,-2,...,4}, minor y tick num=1, minor tick length=0.15cm (0.15cm is the standard length of the major ticks, so this way the minor and major tick marks would look identical). You can then set the background for all labels to white, without the unwanted side effect of empty labels showing up as white squares.

To avoid the shift introduced by setting inner sep=0pt (which is necessary because otherwise the white background would be too large), you have to set outer sep=0.3333em (which is the value the inner sep had before).

\documentclass{article}
\usepackage{pgfplots}

\newcommand{\XMin}{-2}%
\newcommand{\XMax}{2}%

\pgfkeys{/pgfplots/My Axis Style/.append style={
width=6cm, height=8cm,
xmin=\XMin, xmax=\XMax, ymin=-3.2, ymax=5,
axis y line=center,
axis x line=middle,
axis on top=true,
ytick={-4,-2,...,4},
minor y tick num=1,
minor tick length=0.15cm
}
}

\newcommand*{\AddPlotFunction}{\addplot[smooth, ultra thick, domain=\XMin:\XMax, red]  {exp(x)-2} node [left] {$y=e^x-2$}}%
\newcommand*{\AddPlotAsymptote}{\addplot[smooth, thin,        domain=\XMin:\XMax, blue] {-2} node [below left] {$y=-2$}}%

\pgfkeys{/pgfplots/My Y Tick Label/.append style={
yticklabel style={draw=none, inner sep=0pt, outer sep=0.3333em, fill=white, text opacity=1}
}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[My Axis Style,
My Y Tick Label,
]


Why does the setting of inner sep reset the value of outer sep? Just wondering if the style is really being appended? Also, how could I apply this to only certain tick labels, and not all tick labels? –  Peter Grill Mar 30 '12 at 22:21
It doesn't reset the value of outer sep, but by default outer sep is 0 (well, it's 0.5\pgflinewidth, but almost). But by setting the inner sep to 0pt, the nodes become smaller (the inner sep added empty space around the node content) and thus move to the right, because they're snugly aligned with the axis. So to compensate, you need to add the space that you removed with inner sep=0pt back in by setting outer sep=<the value that inner sep used to be>. –  Jake Mar 30 '12 at 22:26
I think the best way to apply a style to only certain tick labels is to use the extra y ticks, as suggested by Christian, but that would mean that you have to go back to supplying explicit tick label lists. I think giving all labels a white background is the better solution in this case, it's much more straightforward and it shouldn't do any harm. –  Jake Mar 30 '12 at 22:30