5

This is a follow up to Style to make axis tick label opaque, where a suggestion was made to use the extra y ticks to specify special tick marks and use extra y tick style to apply a style to only those tick labels.

I attempted to adapt Jake's answer to the above referenced question and apply that by making -2 not a normal tick, but an extra y tick:

ytick={-1,1,2,3,4},%    Note: -2 not listed here
extra y ticks={-2},% 
extra y tick style={draw=none, inner sep=0pt, outer sep=0.3333em, fill=white, text opacity=1},

but this does not seem to have any effect on the y tick label at -2:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}

\newcommand{\XMin}{-2.2}%
\newcommand{\XMax}{1.5}%

\pgfkeys{/pgfplots/My Axis Style/.append style={
    width=7cm, height=8cm,
    xmin=\XMin, xmax=\XMax, ymin=-3.2, ymax=2.5,
    axis y line=center,
    axis x line=middle, 
    axis on top=true,
    ytick={-1,1,2},
    extra y ticks={-2},
    extra y tick style={draw=none, inner sep=0pt, outer sep=0.3333em, fill=white, text opacity=1},
    }
}

\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$}}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[My Axis Style]
    \AddPlotFunction;
    \AddPlotAsymptote;
\end{axis}
\end{tikzpicture}
\end{document}
5

You need to supply the fill option to extra y tick style={y tick label style={<options>}}.

This is similar to how you need to supply it to y tick label style, not y tick style in case you're using "normal" ticks. Unfortunately, there's no every y tick label style, so you'll have to use y tick label style inside extra y tick style.

\documentclass{standalone}
\usepackage{pgfplots}

\newcommand{\XMin}{-2.2}%
\newcommand{\XMax}{1.5}%

\pgfplotsset{
    My Axis Style/.append style={
        width=7cm, height=8cm,
        xmin=\XMin, xmax=\XMax, ymin=-3.2, ymax=2.5,
        axis y line=center,
        axis x line=middle, 
        axis on top=true,
        ytick={-1,1,2},
        extra y ticks={-2},
        extra y tick style={
            y tick label style={
                fill=white,
                draw=none,
                inner sep=0pt,
                outer sep=0.3333em
            }       
        },
    }
}

\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$}}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[My Axis Style]
    \AddPlotFunction;
    \AddPlotAsymptote;
\end{axis}
\end{tikzpicture}
\end{document}

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.