5

I am plotting lattice paths to two points. I would like to have help lines drawn on top of the Cartesian plane so that any line that is an integral distance from an axis is a solid line and any line that is an odd integral multiple of 0.5 from an axis is dashed. The help lines that I have are not even consistent with the axes!

How to I get the O, A, and B typeset over the help lines. The option [fill=white,circle,inner sep=0pt] is ignored in my code. So, one of the (misplaced) help lines is drawn over it.

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning}



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

\begin{document}

\hspace*{\fill}
\begin{tikzpicture}
\begin{axis}[
    xmin=-11,xmax=11,ymin=-11,ymax=11,
    axis lines=middle,
    enlargelimits={abs=0.5},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
        ]

\coordinate (O) at (0,0);
\node[fill=white,circle,inner sep=0pt] (O-label) at ($(O)+(-135:10pt)$) {$O$};
\coordinate (A) at (-2.5,9.5);
\node[fill=white,circle] (A-label) at ($(A)+(-180:10pt)$) {$A$};
\coordinate (B) at (7,-3.5);
\node[fill=white,circle] (B-label) at ($(B)+(-45:10pt)$) {$B$};

\draw[fill] (O) circle (1.5pt);
\draw[fill] (A) circle (1.5pt);
\draw[fill] (B) circle (1.5pt);

%These commands are for drawing a path from O to A.
\coordinate (A1) at (-0.5,0);
\coordinate (A2) at (-.5,3);
\coordinate (A3) at (-2.5,3);

\draw[line width=1pt] (O) -- (A1) -- (A2) -- (A3) -- (A);

%These commands are for drawing a path from O to B.
\coordinate (B1) at (2,0);
\coordinate (B2) at (2,-2.5);
\coordinate (B3) at (5,-2.5);
\coordinate (B4) at (5,-3.5);
\coordinate (B) at (7,-3.5);

\draw[line width=1pt] (O) -- (B1) -- (B2) -- (B3) -- (B4) -- (B);

\draw[help lines,line width=0.1pt,blue] (-10.5, -10.5) grid (10.5,10.5);


\end{axis}
\end{tikzpicture}
\hspace{\fill}
\vskip0.25in

\end{document}
user74973
  • 4,071
  • 1
    Your points need to be given as (axis cs:x,y) or you need to upgrade to version 1.12 and explicitly put compat=1.12. Then it will automatically assume axis coordinates. – percusse May 21 '15 at 17:33
  • @percusse Thanks. I can add axis cs: to the points. How do I upgrade to version 1.12? I am looking at page 12 of the manual at http://pgfplots.sourceforge.net/pgfplots.pdf. It says that "Windows users often use MikTEX which downloads the latest stable package versions automatically. You do not need to install anything manually here." I am using WinEdt 8.1. – user74973 May 21 '15 at 19:06
  • In your MikTeX start menu options there is an item Upgrad (admin) or something like that. If you select that it will search and show you the updatable packages . See this one http://tex.stackexchange.com/questions/55437/how-do-i-update-my-tex-distribution – percusse May 21 '15 at 19:08
  • @percusse I am using WinEdt. – user74973 May 21 '15 at 19:11
  • WinEdt is your editor where you type your document. It uses a TeX distribution which is MikTeX or TeX Live. Windows users usually use MikTeX. Look at your start menu you should see it there. Then follow the instructions. – percusse May 21 '15 at 20:23
  • @percusse OK. (I haven't heard of "Tex distribution," and I don't know much about programming.) I am looking at "Installation on Windows" on page 12. It says that "MikTeX downloads the latest stable package versions automatically." I changed \pgfplotsset{compat=1.11} to \pgfplotsset{compat=1.12} in the preamble and tried to compile it. I got errors. – user74973 May 21 '15 at 20:41
  • Look at your start menu. You have to find your distribution and I'm sure it is MikTeX. You need to find and do the update with it. WinEdt will not update anything. – percusse May 21 '15 at 20:53
  • @percusse I went to "Programs" in the start menu and selected MikTeX2.7. A menu appeared. I selected "Update." Another menu appeared. I selected "I want to get updated packages from a remote package repository" and "Use the nearest package repository." I select "Next" and I get the error "The operation could not be completed because MikTeX is not supported anymore." What should I do to update to MikTeX2.9? – user74973 May 21 '15 at 21:20
  • 1
    Oh yes. Definitely. Uninstall that and then install 2.9 – percusse May 21 '15 at 21:27

1 Answers1

7

This is also an answer to the comment for another answer of another question.

To get help lines overprinted, they have to be printed first below the following objects. The \draw command should be placed before the other elements.

The next problem are the misplaced lines. The following command adds the grid lines at least in the area (-10, -10) to (10, 10):

\draw[
  help lines,
  line width=0.1pt,
  blue,
  shift={($(1, 1) - (0, 0)$)},
] (-11, -11) grid[step={($(5, 5) - (0, 0)$)}] (9, 9);

Full example:

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}   
\begin{tikzpicture}
\begin{axis}[
    xmin=-11,xmax=11,ymin=-11,ymax=11,
    axis lines=middle,
    enlargelimits={abs=0.5},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
        ]

\draw[
  help lines,
  line width=0.1pt,
  blue,
  shift={($(1, 1) - (0, 0)$)},
] (-11, -11) grid[step={($(5, 5) - (0, 0)$)}] (9, 9);

\coordinate (O) at (0,0);
\node[fill=white,circle,inner sep=0pt] (O-label) at ($(O)+(-135:10pt)$) {$O$};
\coordinate (A) at (-2.5,9.5);
\node[fill=white,circle] (A-label) at ($(A)+(-180:10pt)$) {$A$};
\coordinate (B) at (7,-3.5);
\node[fill=white,circle] (B-label) at ($(B)+(-45:10pt)$) {$B$};

\draw[fill] (O) circle (1.5pt);
\draw[fill] (A) circle (1.5pt);
\draw[fill] (B) circle (1.5pt);

%These commands are for drawing a path from O to A.
\coordinate (A1) at (-0.5,0);
\coordinate (A2) at (-.5,3); 
\coordinate (A3) at (-2.5,3);

\draw[line width=1pt] (O) -- (A1) -- (A2) -- (A3) -- (A);

%These commands are for drawing a path from O to B.
\coordinate (B1) at (2,0);   
\coordinate (B2) at (2,-2.5);
\coordinate (B3) at (5,-2.5);
\coordinate (B4) at (5,-3.5);
\coordinate (B) at (7,-3.5);

\draw[line width=1pt] (O) -- (B1) -- (B2) -- (B3) -- (B4) -- (B);

\end{axis}
\end{tikzpicture}
\end{document}   

Result

The full plot area from -11 to 11 in both directions can be covered by:

\begin{scope}
  \clip (-11, -11) rectangle (11, 11);
  \draw[
    help lines,
    line width=0.1pt,
    blue,
    shift={($(1, 1) - (0, 0)$)},
  ] (-16, -16) grid[step={($(5, 5) - (0, 0)$)}] (10, 10);
\end{scope}

However, the axis tick labels are overwritten by the help lines. Therefore I would rather use the grid option of the axis environment:

\begin{axis}[
  ...,
  grid,
  grid options={line width=.1pt, draw=gray!30},
]
  ...
\end{axis}

Result grid option

Heiko Oberdiek
  • 271,626
  • Your display is nice. I have seen that there is a canvas coordinate system and an axis coordinate system, and they are not the same. I think the command shift={($(1, 1) - (0, 0)$)}, is relevant to the different coordinate systems. Please explain this command. – user74973 May 28 '15 at 14:04
  • @user74973 The origins of the coordinate systems are different. Especially the origin of the axis coordinate system is not at (0pt, 0pt). Therefore I use the difference to calculate the step width. – Heiko Oberdiek May 28 '15 at 14:33
  • I do not know how shift={($(1, 1) - (0, 0)$)} compensates for this. Can you offer a link at which I can read about this? – user74973 May 28 '15 at 15:50
  • In this case, I agree with you that the grid option of the axis environment is appropriate so that the lines from the grid are not drawn over the tick labels. I am not sure of the syntax. Here is my guess. (-10.5,-10.5) grid[xstep=1, ystep=1] (10.5,10.5). I would like to have line width=0.1pt, and draw=gray!30. – user74973 May 28 '15 at 15:51
  • 1
    @user74973 The plot area starts at (-11, -11), but I want to start the grid at a multiply of the grid step width, (-10, -10). Since the origin of the axis coordinate system is in (0, 0) or eleven x units to the right and eleven y units to the top I can't use shift={(1, 1)}, I have to use the difference. Documentation, see pgf manual and pgfplots manual. The latter explains the configuration of grid in section "4.16 Grid Options" . – Heiko Oberdiek May 28 '15 at 16:04
  • I just looked at the pgfplots manual for the grid options. This "section" is less than a page! I will post a request for help in drawing a grid using the axis options. – user74973 May 28 '15 at 21:54