0

So I want to draw nodes every unit from a given x-Coordinate of a point, to the x-Coordinate of another Point, to label a custom axis. I do wanna skip the 0. The manual example looks like:

\foreach \x in {-1,1,2,...,10}
    \draw (\x,-.1) -- (\x,.1) node[below=4pt] {$\scriptstyle\x$};

But as I will have to reuse this code quite often, I don't want to always set start and end of the range manually. I would much rather navigate from two given points.

Basically something like:

\foreach \x in {"x-Coordinate of Point DL",.., skip 0,"x-Coordinate of Point TR"}
    \draw (\x,-.1) -- (\x,.1) node[below=4pt] {$\scriptstyle\x$};

A not so minimal working example of what I am trying to do is this:

\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}[cross/.style={draw, cross out,
        minimum size=2*(#1-1pt), inner sep=0pt, outer sep=0pt}, x=1cm, y=1cm]
    %----------------
    %--- Define x/y axis by setting x-min and y-min
    \tkzDefPoint(-1,-1){DL} %down left border
    \tkzDefPoint(10,10){TR} %top right border
    %----------------
    %--- Draw the grid  
    \draw [color=gray!50]  [step=5mm] ($(DL)+(-1,-1)$) grid ($(TR)+(1,1)$);
    %----------------
    %--- Drawing the axis
    %--- x-axis
    \draw[->,thick] let \p{DL}=(DL), \p{TR}=(TR), in
    (-13+\x{DL},0) -- (13+\x{TR},0) 
    node[right] {$x$};
    %--- y-axis
    \draw[->,thick] let \p{DL}=(DL), \p{TR}=(TR), in
    (0,-13+\y{DL}) -- (0,13+\y{TR}) 
    node[above right] {$y$};
    %----------------
    %--- Labeling of both axes
    \foreach \x in {-1,1,2,...,10}
    \draw (\x,-.1) -- (\x,.1) node[below=4pt] {$\scriptstyle\x$};
    \foreach \y in {-1,1,2,...,10}
    \draw (-.1,\y) -- (.1,\y) node[left=4pt] {$\scriptstyle\y$};
\end{tikzpicture} 
\end{document}

enter image description here

I do know that I could use pgfplots, to do what I am trying to achieve much easier, but I always had trouble getting a custom grid. As I have to print this and students sketch on it, the grid size is very important (which is why I for now am using tikz directly).

Black Mild
  • 17,569
curies
  • 199
  • a side comment: using \path (-1,-1) coordinate (DL) % down left border (10,10) coordinate (TR) % top right border ; and you need not to load tkz-euclide – Black Mild Sep 13 '22 at 16:51
  • I think it is possible to add unusual styles in pgfplots, you will have some image of how you want it to look?, check this answer.... I think you are in time to leave brute force for an elegant framework. – J Leon V. Sep 13 '22 at 21:56

0 Answers0