1

I'm trying to draw a graph of a few functions intersecting using Tikz. I currently have (just basic plot formatting and the actual plotting) :

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xlabel=$x$, y label style={anchor=north east, at={(axis description cs:0,1)}}, ylabel=$y$, xmin=-1, xmax=15, ymin=-5, ymax=30, axis on top, xticklabel style={fill=white}, ] \addplot [id=ghosttan, domain=0:15, samples=500, color=black, dashed] {tan(deg(x))}; \addplot [id=tan, domain=0:15, restrict y to domain=-50:50, samples=1000, color=red, line width=0.5pt]{tan(deg(x))}; \addplot [id=root, domain=0:15, samples=100]{sqrt((12/x)^2 - 1)}; \addplot [id=minx, domain=0:15, samples=2]{-1/2*x}; \node [anchor=north east] at (axis cs:0,0) {$0$}; \end{axis} \end{tikzpicture} \end{document}

The output is:

Tikz pgfplot label backgrounds clearing too much of the graphs

The problem I'm having should be obvious: the white label backgrounds that serve to prevent the graphs crossing/interfering with the labels, clear too much, especially around the 6, the 8, and the 14, for example. The final two axis options in the MWE have this purpose, and they were inspired by this thread.

The idea I have is having the labels have a slightly bigger white copy of themselves between the label and the graph. That way, in this example, the 4 would only clear the graph when it hit its diagonal edge passing over part of the label, for example.

Ideally, I want to keep the axes in the back as well, but the labels are of higher priority. I don't know how to manage either, though, which is my question: how do I manage this?

Mew
  • 843

1 Answers1

1

You can use the contour package and the layers options (you ahve to put the axis tick labels on top, otherwise you'll lose the effect of the contour...:

\usepackage[outline]{contour}
\contourlength{1.2pt}
\pgfplotsset{
    /pgfplots/layers/main on top/.define layer set={
        axis background,axis grid, axis ticks,axis lines,
        axis descriptions,axis foreground, main, axis tick labels,
    }{/pgfplots/layers/standard},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=middle,
    xlabel=$x$,
    y label style={anchor=north east, at={(axis description cs:0,1)}},
    ylabel=$y$,
    xmin=-1, xmax=15,
    ymin=-5, ymax=30,
    xticklabel={\contour{white}{\pgfmathprintnumber{\tick}}},
    set layers = main on top,
    ]
    \addplot [id=ghosttan, domain=0:15, samples=500, color=black, dashed] {tan(deg(x))};
    \addplot [id=tan, domain=0:15, restrict y to domain=-50:50, samples=1000, color=red, line width=0.5pt]{tan(deg(x))};
    \addplot [id=root, domain=0:15, samples=100]{sqrt((12/x)^2 - 1)};
    \addplot [id=minx, domain=0:15, samples=2]{-1/2*x};
    \node [anchor=north east] at (axis cs:0,0) {$0$};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • Awesome! One concern I have: if I wanted to use \pgfmathprintnumber{\tick} with symbolic ticks (e.g. fractions of π, most notably), I don't think that'd work, since pgfmathprintnumber converts numbers to floating-point representation. Is there a fix that could generalise the answer to any kind of tick? – Mew Jun 20 '20 at 23:31
  • For symbolic you probably want to remove the pgfprintnumber - - - and maybe also de outline option of contour (see https://tex.stackexchange.com/questions/8602/how-can-i-put-a-coloured-outline-around-fraction-lines). I do not know how to do it automatically... – Rmano Jun 21 '20 at 07:09