# Keeping labels for tick marks on a graph using pgfplots the same distance from x-axis

I have labels for two tick marks. One of the labels, -8, is to be surrounded by white space so that it gives the appearance of being typeset over the graph. I do this with the following commands.

extra x ticks={-8},
extra x tick style={ticklabel style={font=\scriptsize, inner xsep=10pt, fill=white}},
extra x tick labels={\makebox[0pt][r]{$-$}8},


There is too much white space, though. What is the default distance between the center of the node and the sides? I would like either two-thirds this distance or half the distance. How do I decrease the dimensions of the box containing -8 and the white space while maintaining consistency of distances between the labels -8 and 8 and their respective tick marks?

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

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

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

\begin{document}

\begin{tikzpicture}

\begin{axis}[width=3.5in, height=3.5in, axis lines=middle, axis on top, clip=false,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-2.5,ymax=10,
restrict y to domain=-2.5:10,
xtick={8}, ytick={8},
ticklabel style={font=\scriptsize},
xticklabels={8},
extra x ticks={-8},
extra x tick style={ticklabel style={font=\scriptsize, inner xsep=10pt, fill=white}},
extra x tick labels={\makebox[0pt][r]{$-$}8},
axis line style={latex-latex},
xlabel=$x$,ylabel=$y$,
axis line style={shorten >=-12.5pt, shorten <=-12.5pt},
xlabel style={at={(ticklabel* cs:1)}, xshift=12.5pt, anchor=north west},
ylabel style={at={(ticklabel* cs:1)}, yshift=12.5pt, anchor=south west}
]

\addplot[samples=2, blue, domain=-10:0] {x + 8} node [anchor=south east, pos=0.35, font=\footnotesize] {$y= \vert x \vert + y = 8$};
\addplot[samples=2, blue, domain=0:10] {-x + 8};

\addplot[dashed, latex-latex, domain=-2.5:10] (6,x) node [pos=0.9, anchor=north, font=\footnotesize, sloped] {$x=6$};

\coordinate (A) at (6,0);
\coordinate (B) at (6,2);
\coordinate (C) at (0,8);
\coordinate (D) at (-8,0);

\end{axis}

\fill[gray!50, fill opacity=0.25] (A) -- (B) -- (C) -- (D) -- cycle;

\end{tikzpicture}

\end{document}


Assuming you're referring to the vertical distance here, the default value of the inner sep, which is the distance from the node content to the node border, is 0.333em in both x and y directions.

It may not be a surprise that the vertical padding is set with inner ysep, similar to inner xsep which sets the horizontal padding.

If you reduce that the node label will move up a bit, but you can shift it downwards to take that into account with yshift. So for example, add inner ysep=0.15em,yshift=-0.183em to your ticklabel style. 0.333-0.15=0.183, hence the values.

• I am guessing what you mean by "node content" is "the smallest rectangle containing the node content" and so the default distance between this rectangle and the node border is 0.333em. – Adelyn Oct 28 '16 at 14:43
• I have the tick label 8 typeset using the default values. The 8 is typeset a certain distance below the x-axis. -8 is to be typeset with less vertical spacing between -8 and the top and bottom edges of the border. In particular, it is to be decreased from the default 0.333em to 0.15em. To get -8 the same distance below the x-axis as 8, it has to be shifted downward by 0.333dm - 0.15em = 0.183em. – Adelyn Oct 28 '16 at 14:43
• @Adelyn Ehm, yes, that looks about right I think. – Torbjørn T. Oct 28 '16 at 15:29

Another possibility is using a \colorbox for the extra tick label:

\documentclass{amsart}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{calc,intersections}

\newcommand\whitebg[2][2pt]{{%
\setlength\fboxsep{#1}%
\raisebox{0pt}[\dimexpr\height-#1\relax][\dimexpr\depth-#1\relax]{\colorbox{white}{#2}}%
}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=3.5in, height=3.5in,
axis on top,
clip=false,
axis lines=middle,
xmin=-10,xmax=10,
ymin=-2.5,ymax=10,
restrict y to domain=-2.5:10,
xtick={8}, ytick={8},
ticklabel style={font=\scriptsize},
xticklabels={8},
extra x ticks={-8},
extra x tick style={ticklabel style={font=\scriptsize}},
extra x tick labels={\whitebg{\makebox[2em]{\makebox[0pt][r]{$-$}8}}},
axis line style={latex-latex},
xlabel=$x$,ylabel=$y$,
axis line style={shorten >=-12.5pt, shorten <=-12.5pt},
xlabel style={at={(ticklabel* cs:1)}, xshift=12.5pt, anchor=north west},
ylabel style={at={(ticklabel* cs:1)}, yshift=12.5pt, anchor=south west}
]

\addplot[samples=2, blue, domain=-10:0] {x + 8}
node [anchor=south east, pos=0.35, font=\footnotesize] {$y= \vert x \vert + y = 8$};
\addplot[samples=2, blue, domain=0:10] {-x + 8};
node [pos=0.9, anchor=north, font=\footnotesize, sloped] {$x=6$};
`