Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

Add number to a grid Tikz LaTeX

How do I add numbers outside the below grid. I tried the following code but it is not centred and I cannot put the number.

  documentclass{article}
    usepackage{geometry}
    geometry{hmargin=1cm,vmargin=1cm}
    usepackage{tikz}
    usetikzlibrary {shapes.geometric, arrows, arrows.meta}
    defwidth{20}
    defhauteur{25}
    egin{document}
    egin{tikzpicture}[x=1cm, y=1cm]
    draw[step=1mm, line width=0.1mm, black!5!white] (0,0) grid (width,hauteur);
    draw[step=5mm, line width=0.2mm, black!10!white] (0,0) grid (width,hauteur);
    draw[step=5cm, line width=0.5mm, black!10!black] (0,0) grid (width,hauteur);
    draw[step=1cm, line width=0.3mm, black!15!white] (0,0) grid (width,hauteur);
    end{tikzpicture}
    end{document}
question from:https://stackoverflow.com/questions/65854496/add-number-to-a-grid-tikz-latex

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could try using a combination of the foreach loop and the ode functionality. I added the source code below:

documentclass{article}
usepackage{geometry}
geometry{hmargin=1cm,vmargin=1cm}
usepackage{tikz}
usetikzlibrary {shapes.geometric, arrows, arrows.meta}
    
defwidth{20}
defhauteur{25}
    
egin{document}
    egin{tikzpicture}[x=1cm, y=1cm]
    draw[step=1mm, line width=0.1mm, black!5!white] (0,0) grid (width,hauteur);
    draw[step=5mm, line width=0.2mm, black!10!white] (0,0) grid (width,hauteur);
    draw[step=5cm, line width=0.5mm, black!10!black] (0,0) grid (width,hauteur);
    draw[step=1cm, line width=0.3mm, black!15!white] (0,0) grid (width,hauteur);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%% ADD THE PART BELOW %%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    foreach x in {0, ..., width} {%
      % Bottom
      
ode[anchor=north] at (x,0) {x};
      
      % Top
      
ode[anchor=south] at (x,hauteur) {x};
    }
    
    foreach y in {0, ..., hauteur} {%
      % Left
      
ode[anchor=east] at (0,y) {y};
      
      % Right
      
ode[anchor=west] at (width,y) {y};
    }
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    end{tikzpicture}
end{document}

This will produce the following output:

Output grid with numbers around it

Of course you can still adjust the spacing a bit if you want. Would this be the result you are looking for?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...