I have grid lines drawn on the Cartesian plane. I mark distances 2
, 4
, 6
, and 8
along the x-axis and 2
along the y-axis. To avoid the grid lines being drawn over these labels, I use fill=white
and inner sep=0.15
in the options for the node commands. It seems to me that the display does not have the labels surrounded with 0.15cm
of white space. How do I get the requested white space without moving the labels?
\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
%Horizontal grid lines are drawn.
\draw[dashed,gray!50] (-0.75,-0.5) -- (4.25,-0.5);
\draw[dashed,gray!50] (-0.75,0) -- (4.25,0);
\draw[dashed,gray!50] (-0.75,0.5) -- (4.25,0.5);
\draw[dashed,gray!50] (-0.75,1) -- (4.25,1);
\draw[dashed,gray!50] (-0.75,1.5) -- (4.25,1.5);
%Vertical grid lines are drawn.
\draw[dashed,gray!50] (-0.5,-0.75) -- (-0.5,1.75);
\draw[dashed,gray!50] (0,-0.75) -- (0,1.75);
\draw[dashed,gray!50] (0.5,-0.75) -- (0.5,1.75);
\draw[dashed,gray!50] (1,-0.75) -- (1,1.75);
\draw[dashed,gray!50] (1.5,-0.75) -- (1.5,1.75);
\draw[dashed,gray!50] (2,-0.75) -- (2,1.75);
\draw[dashed,gray!50] (2.5,-0.75) -- (2.5,1.75);
\draw[dashed,gray!50] (3,-0.75) -- (3,1.75);
\draw[dashed,gray!50] (3.5,-0.75) -- (3.5,1.75);
\draw[dashed,gray!50] (4,-0.75) -- (4,1.75);
%Some distances from the origin along the axes are labeled.
\node[fill=white, anchor=north, inner sep=0.15, font=\tiny] at ($(1,0) +(0,-0.15)$){2};
\node[fill=white, anchor=north, inner sep=0.15, font=\tiny] at ($(2,0) +(0,-0.15)$){4};
\node[fill=white, anchor=north, inner sep=0.15, font=\tiny] at ($(3,0) +(0,-0.15)$){6};
\node[fill=white, anchor=north, inner sep=0.15, font=\tiny] at ($(4,0) +(0,-0.15)$){8};
\node[fill=white, anchor=east, inner sep=0.15, font=\tiny] at ($(0,1) +(-0.15,0)$){2};
%The axes are drawn.
\draw[latex-latex] (-1,0) -- (4.5,0);
\draw[latex-latex] (0,-1) -- (0,2);
\node [anchor=north west] at (4.5,0) {$x$};
\node [anchor=south west] at (0,2) {$y$};
%A path is drawn.
\draw (0,-0.5) -- (1,0.5) -- (1.5,0) -- (2,1) -- (3,1) -- (3.5,0.5) -- (4,1.5);
\end{tikzpicture}
\end{document}
Best Answer
This answer offers two solutions, one with
pgfplots
and one withtikz
.pgfplots
This would be much easier to do with
pgfplots
, you don't need to draw everything manually.Output
Code
tikz
If you want to keep using TikZ, here's an alternative version. Your problem is that you said
inner sep=0.15
but didn't specify the type of measurement. Try writinginner sep=0.15cm
and you'll see the difference.Output
Code