I'm trying to draw a diagram with slanted grids in it, sort of like the 3D effect given at http://www.texample.net/tikz/examples/3d-graph-model/

Ideally, I'd like a macro slantedgrid so that for example

\draw (10,12) \slantedgrid (7,5);

or maybe even simply


will draw a grid with all vertical lines slanted at 45 degrees, the lower left corner at (10,12) and with 5+1 horizontal lines and 7+1 vertical (slanted) lines.

My first (and so far only) attempt was to define it with LaTeX "newcommand" as

  \foreach \x in {#1,#1+1,...,#1+#3} \draw (\x,#2) -- ++(#4,#4);%
  \foreach \y in {#2,#2+1,...,#2+#4} \draw (\y-#4,\y) -- ++(#3,0);%

However, all this produces is loads of errors - "Illegal unit of measure (pt inserted)" - when I bung slantedgrid{10}{12}{7}{5} into a tikzpicture environment.

Is there any simple method of doing something like this - in particular passing the parameters from a newcommand to TikZ's foreach?

  • 2
    Is \foreach an essential requirement? As it stands, \draw [shift={(10,12)}, xslant=tan 45] grid ++(7,5); seems to do it nicely. – Mark Wibrow May 3 '14 at 13:07
  • Gak, arrgh! xslant makes my task almost trivial. Thank you very much! – Alasdair May 3 '14 at 13:44
  • @MarkWibrow You should put your solution in an answer, I almost missed your comment. – Matheod Sep 1 '19 at 13:38

If you want still to pass a calculated parameters to foreach you can use pgfmathtruncatemacro:

   \foreach \x in {#1,...,\result} \draw (\x,#2) -- ++(#4,#4);%
   \foreach \y in {#2,...,\result} \draw (#1+\y-#2,\y) -- ++(#3,0);%

enter image description here

PS: I changed second foreach to what I think you had in mind.

| improve this answer | |
  • Thanks again! Yes, I was sure my second foreach was incorrect even as I posted it... – Alasdair May 3 '14 at 14:48

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.