8

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}
  • 1
    If you don't want the nodes to move, you could, if you really wanted to, fill behind them after they are drawn. You would need a layer in between the plot and the nodes. At least, that would be easiest, I guess. – cfr Feb 10 '16 at 2:35
8

This answer offers two solutions, one with pgfplots and one with tikz.

pgfplots

This would be much easier to do with pgfplots, you don't need to draw everything manually.

Output

enter image description here

Code

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

\pgfplotsset{compat=1.13}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=-1, xmax=9,
    ymin=-1, ymax=2,
    axis equal,
    minor y tick num=1,
    minor x tick num=1,
    yticklabel style={font=\scriptsize, fill=white},
    xticklabel style={font=\scriptsize, fill=white},
    axis lines=center, no markers,
    grid=both, grid style={dashed,gray,very thin},
    xlabel={$x$},
    ylabel={$y$},
]

\plot[black,thick] coordinates {(0,-1) (2,1) (3,0) (4,2) (6,2) (7,1) (8,3)};
\end{axis}
\end{tikzpicture}
\end{document}

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 writing inner sep=0.15cm and you'll see the difference.

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}%{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand\myshift{.10}
\newcommand\xmax{5}
\newcommand\ymax{2.5}

\begin{document}
\begin{tikzpicture}

% axes + grid
\draw[step=5mm,gray,dashed, line width=.2pt] ({\xmax-5.75},{\ymax-3.75}) grid ({\xmax-.25},{\ymax-.25});
\draw[latex-latex] (-1,0) -- (\xmax,0) node[font=\tiny, right] {$x$};
\draw[latex-latex] (0,{\ymax-4.25}) -- (0,\ymax)   node[font=\tiny, above] {$y$};

% x tick labels
\foreach \label [count=\xx] in {2,4,6,8}{%
    \node[fill=white, anchor=north, inner sep=\myshift cm, font=\tiny] at (\xx,0) {\label};
}

% y tick labels
\foreach \label [evaluate=\label as \yy using int(\label/2)] in {-2,2,4}{%
    \node[fill=white, anchor=east, inner sep=\myshift cm, font=\tiny] at (0,\yy) {\label};
}

%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}
  • Hi, Alenanno. It has been a long time. I want to keep the code in TikZ. – Adelyn Feb 6 '16 at 16:38
  • @Adelyn Hello! :) See edit. – Alenanno Feb 6 '16 at 16:54
  • Your code is more efficient. I have not referred to the manual to use the \foreach command. I should refer to this post later for that. – Adelyn Feb 6 '16 at 18:29
  • Yes, there is a difference between inner sep=0.15 and inner sep=0.15cm. Isn't the default measurement in centimeters? Apparently, it isn't the default for measurements in the options of a node command. What does inner sep=0.15 do? – Adelyn Feb 6 '16 at 19:07
  • I like the spacing provided by the option anchor=north and the location ($(1,0) +(0,-0.15)$) for the labels 2, 4, 6, and 8 below the x-axis. The only modification that I want is to have some white space about them - without pushing the labels downward. – Adelyn Feb 6 '16 at 19:07
6
+25

I can imagine why you might want to stick with TikZ syntax for self-confidence, familiarity and so on, but I would still recommend pgfplots for this or at least TikZ' own graphdrawing library.

Anyways, for the inner sep outer sep stuff, maybe a visualization might help.

The node contents are put in a placeholder (an \hbox or minipage environment and then TikZ measures this placeholder height and width to draw the node shape. inner sep is added to this measurement.

outer sep is kind of the same idea but works differently. When you want to place something next to it or draw a line to/from this node or simply place the node itself by mentioning the border anchors, it computes the point on the border and then retracts outer sep many points away from the node.

\begin{tikzpicture}
\node[draw,outer sep=1cm,inner sep=1cm] (a){\fbox{Contents}} ;

\draw[|-|,thick,red] (a.south) node[below,black]{Here is south anchor} 
    --++(0,1cm) node[midway,right]{1cm (outer sep)};
\draw[|-|,thick,red] (a.south) ++(0,1cm) 
    --++(0,1cm)node[midway,right]{1cm (inner sep)};
\node[text width=1.5cm,align=right,inner sep=0,outer sep=0,draw,left] 
    (b) at(a.west) {a relative placed node};
\draw[|-|,blue,thick] (b.east) -- ++(1cm,0);
\end{tikzpicture}

enter image description here

As you can see, the padding is now a bit more visible. Now when you anchor your labels the outer sep is pushing them below as you have seen already. But anchoring doesn't need to be absolute you can still nudge things around via:

\begin{tikzpicture}
\node[outer sep=0cm,inner sep=0cm] (O){O} ;
\node[outer sep=1cm,inner sep=1cm,draw,anchor=north,yshift=1cm] at (O) {Node};
\end{tikzpicture}

enter image description here

We see that even if the anchor is set to north we can still shift things around and anchor is still respected.

  • Using this information, how would you put the requested white space about each node as suggested in the post? I am not that familiar with TikZ. I think that I can get the desired display with the following two commands for each node: \node[fill=white, anchor=north, inner sep=0, font=\tiny] at ($(1,0) +(0,-0.15)$){2}; and \node[fill=white, anchor=north, inner sep=0.075cm, font=\tiny] at ($(1,0) +(0,-0.15)$){\phantom{2}};. It may not be efficient, but it does "frame" the node with 0.075cm of white space. Right? – user143462 Feb 9 '16 at 19:36
  • @percusse Your code showing the boxes surrounding a node with inner sep and outer sep is useful. I now have code that gives me the desired placement of the nodes and white space surrounding them: \node[fill=white, anchor=north, inner sep=0.1cm, font=\tiny] at ($(1,0) +(0,-0.05)$){2};. – Adelyn Feb 10 '16 at 23:40
3

An alternative possible solution with TikZ:

enter image description here

It is generated by the following (to my opinion very concise) code:

\documentclass{amsart}
    \usepackage{amsmath,amssymb}
    \usepackage{tikz}
    \usetikzlibrary{arrows,calc,positioning}
% for show only a picture
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
    \setlength\PreviewBorder{5mm}

\begin{document}
    \begin{tikzpicture}[
    x=5mm,  y=5mm,
AL/.style = {% Axis Labels 
   fill=white, inner sep=0.5mm, font=\tiny} 
                        ]
% grid
\draw[gray,dashed,very thin] (-1,-3) grid[step=1] (9,5); 
% x tick labels
\foreach \x in {2,4,6,8}
    \node[AL,below=1mm] at (\x,0) {\x}; 
% y tick labels
\foreach \y in {-2,2,4}
\node[AL,left=1mm] at (0,\y) {\y};
% x and y axes
\draw[latex-latex] (-1,0) -- (9,0) node[AL,below left=1mm and 0mm] {$x$};
\draw[latex-latex] (0,-3) -- (0,5) node[AL,below left=0mm and 1mm] {$y$};
% curve
\draw[red, very thick]
    (0,-1) -- (2,1) -- (3,0) -- 
    (4, 2) -- (6,2) -- (7,1) -- (8,3);
    \end{tikzpicture}
\end{document}

In above code I use:

  • grid for drawing grid of graph. With selection step=5mm are defined distances between grid lines. It is equal to size of x and y units for distances in those directions.
  • style for x and y tick label are defined in tikzpicture options. For inner sep, i.e. distance between text in node and node's border, I select 0,5 mm. In it positioning I select that nodes are 1 mm from axis, so distance between them and numbers is sum of both distances (1.5 mm).
  • I just made a comment to Percusse that is similar to what you did, I think. Your anchors for the labels along the x-axis are not north, though. How would you adjust your code for that? (Where is there a description for AL/.style?) – user143462 Feb 9 '16 at 19:52
  • @user143462, definition of AL style is mine (you can defined similarly common styles and picture features) and it is in option of tikzpicture. With it I achieve two things: (i) all tick and axis label have the same style (ii) the code is shorter. About anchors: (i) you can say anchor=north and node will placed below given coordinate or (ii) say below and node will be positioned also below given coordinate. In second case you can say haw much below you like to have, for example below= 1mm (as used in my MWE). With package positioning you can use even more sophisticated positioning. – Zarko Feb 9 '16 at 20:09
1
\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings,decorations.pathreplacing}

\begin{document}

\begin{tikzpicture}

% axes + grid
\draw[step=5mm,gray,dashed, line width=0.2pt] (-0.75,-1.25) grid (4.25,2.25);
\draw[latex-latex] (-1,0) -- (5,0) node[font=\tiny, right] {$x$};
\draw[latex-latex] (0,-1.75) -- (0,2.5)   node[font=\tiny, above] {$y$};

% x tick labels
\foreach \label [count=\xx] in {2,4,6,8}
{\node[fill=white, anchor=north, inner sep=0.1cm, font=\tiny] at (\xx,0) {\label};}

% y tick labels
\foreach \label [evaluate=\label as \yy using int(\label/2)] in {-2,2,4}
{\node[fill=white, anchor=east, inner sep=0.1cm, font=\tiny] at (0,\yy) {\label};}

%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}
  • This code is a modification of code from Alenanno. TikZ cannot compile it. "Illegal unit of measure (pt inserted)." – Adelyn Feb 10 '16 at 15:23
  • It works without any errors for me. What does the full error say? I mean what line is it referring to? – Alenanno Feb 11 '16 at 0:43
  • @Alenanno It does not tell me any line. You copy-and-pasted this code and it compiled for you?! – Adelyn Feb 11 '16 at 0:47
  • yes, no errors or warnings. Not sure why you're having a problem... Your Tex Live distribution is up to date? – Alenanno Feb 11 '16 at 0:49
  • @Alenanno The error message included "[Loading MPS to PDF converter (version 06.09.02).]" I was using WinEdt 8.1. Apparently, something has to be updated. Any suggestions? – Adelyn Feb 11 '16 at 4:05

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.