TikZ PGF Diagrams Grids – How to Draw Triangular Grid in TikZ diagramsgridstikz-pgf Is there an easy way to draw a triangular grid in TikZ, like this? Best Answer Like Leo said: use \foreach and some math: \usetikzlibrary{calc} \newcommand*\rows{10} \begin{tikzpicture} \foreach \row in {0, 1, ...,\rows} { \draw ($\row*(0.5, {0.5*sqrt(3)})$) -- ($(\rows,0)+\row*(-0.5, {0.5*sqrt(3)})$); \draw ($\row*(1, 0)$) -- ($(\rows/2,{\rows/2*sqrt(3)})+\row*(0.5,{-0.5*sqrt(3)})$); \draw ($\row*(1, 0)$) -- ($(0,0)+\row*(0.5,{0.5*sqrt(3)})$); } \end{tikzpicture} Related SolutionsTikZ-PGF – Missing Grid Borders In the pgfmanual it says on p. 145 that due to rounding errors, the "last" lines of a grid may be omitted. In this case, you have to add an epsilon to the corner points In this case, you will have to subtract an epsilon, i.e. a very small value, from the corner point. Something like \draw[step=0.5cm,color=gray] (2-0.001,2-0.001) grid (4,4); will work without any visible artefacts. [Tex/LaTex] Using \evaluate in nested \foreach loops with TikZ You need to enclose the content of the \foreach loop in curly braces: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} % Grid \draw[step=1,color=gray] (0,0) grid (5,5); % Pads \draw (4.5,4.5) node {G}; \foreach \x [evaluate=\x as \ystart using int(\x-6), evaluate=\x as \xx using int(\x+1)] in {6, 7,..., 10}{ \foreach \y [evaluate=\y as \yy using int(\y+1)] in {\ystart, ..., 4}{ \draw [color=gray] (\x,\y) rectangle (\xx,\yy) node {\y, \yy} ; } } % Pads \draw (10.5,4.5) node {G}; \end{tikzpicture} \end{document} Related QuestionTikZ Grid Without Edges – How to Createtikz-pgf – How to Draw a Grid on Shapes Made of Curved Lines[Tex/LaTex] TikZ grid inside a circletikz-pgf,grids – How to Design a Centered Grid of 1×2 Over 1×3 Using TikZ
Best Answer
Like Leo said: use
\foreach
and some math: