Graphics, Figures & TablesPSTricks | Nested Loops

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks | Nested Loops

Post by svend_tveskaeg »

Hi all.

Consider the following MWE:

Code: Select all

\documentclass{article}

\usepackage{auto-pst-pdf,pstricks-add}
\usepackage[locale=DE]{siunitx}
\definecolor{nicered}{rgb}{0.796,0.255,0.329}

\begin{document}

\begin{figure}
\def\WidthPallet{46}
\def\length{22.8}
\def\height{5.4}
\def\rows{2}
\def\EachRow{8}
\pstFPdiv\faktor{\EachRow}{\rows}
\def\startX{\WidthPallet\space \EachRow\space \height\space mul sub 2 div}
\def\startY{\WidthPallet\space \rows\space \length\space mul sub 2 div}
 \centering
 \psset{unit=0.25cm}
  \begin{pspicture}(\WidthPallet,\WidthPallet)
    \pnode(0,0){P1}
    \pnode(0,\WidthPallet){P2}
    \pnode(\WidthPallet,\WidthPallet){P3}
    \pnode(\WidthPallet,0){P4}
    \pspolygon[fillstyle=solid,fillcolor=brown](P1)(P2)(P3)(P4)
    \multido{\iA=1+1,\iB=0+1}{\EachRow}{%
      \psRelNode(!\startX\space \startY\space)(!\startX\space \height\space add \startY\space){\iB}{A\iA}
      \psRelNode(!\startX\space \startY\space \length\space add)(!\startX\space \height\space add \startY\space \length\space add){\iB}{B\iA}
      \psRelNode(!\startX\space \startY\space 2 \length\space mul add)(!\startX\space \height\space add \startY\space 2 \length\space mul add){\iB}{C\iA}
      \psRelNode(!\startX\space \height\space add \startY\space 2 \length\space mul add)(!\startX\space 2 \height\space mul add \startY\space 2 \length\space mul add){\iB}{D\iA}
      \psRelNode(!\startX\space \height\space add \startY\space \length\space add)(!\startX\space 2 \height\space mul add \startY\space \length\space add){\iB}{E\iA}
      \psRelNode(!\startX\space \height\space add \startY\space)(!\startX\space 2 \height\space mul add \startY\space){\iB}{F\iA}
      \pspolygon[fillstyle=solid,fillcolor=nicered](A\iA)(B\iA)(E\iA)(F\iA)
      \pspolygon[fillstyle=solid,fillcolor=nicered](B\iA)(C\iA)(D\iA)(E\iA)
    }
   \psset{nrot=:U,offset=-19pt,linestyle=dashed,fillcolor=nicered}
    \pcline[offset=12pt]{<->}(P1)(P4)
    \ncput*{\SI{\WidthPallet}{\cm}}
    \pcline{<->}(B1)(C1)
    \ncput*{\SI{\length}{\cm}}
    \pcline{<->}(C1)(D1)
    \ncput*{\scriptsize{\SI{\height}{\cm}}}
  \end{pspicture}
\end{figure}

\end{document}
How do I use a nested \multido in order to `automatically' draw \rows rows of \EachRow rectangles on top of each other? In other words: How do I extend the above code in order to draw \rows rows on top of each other instead of just 2 rows?

Thank you in advance!

P.S. I have looked at this answer but I cannot make it work.

Update
I forgot to post (one of) my initial tries:

Code: Select all

\begin{figure}
\def\WidthPallet{46}
\def\length{22.8}
\def\height{5.4}
\def\rows{2}
\def\EachRow{8}
\pstFPdiv\faktor{\EachRow}{\rows}
\def\startX{\WidthPallet\space \EachRow\space \height\space mul sub 2 div}
\def\startY{\WidthPallet\space \rows\space \length\space mul sub 2 div}
 \centering
 \psset{unit=0.25cm}
  \begin{pspicture}(\WidthPallet,\WidthPallet)
    \pspolygon[fillstyle=solid,fillcolor=brown](0,0)(0,\WidthPallet)(\WidthPallet,\WidthPallet)(\WidthPallet,0)
    \multido{\iA=1+1,\iB=0+1}{\EachRow}{%
     \multido{\iC=1+1}{\rows}{%
      \psRelNode(!\startX\space \startY\space)(!\startX\space \height\space add \startY\space \iC \row mul add){\iB}{A\iA}
      \psRelNode(!\startX\space \startY\space \length\space \iC \row mul add add)(!\startX\space \height\space add \startY\space \length\space \iC \row mul add add){\iB}{B\iA}
      \psRelNode(!\startX\space \height\space add \startY\space \length\space \iC \row mul add add)(!\startX\space 2 \height\space mul add \startY\space \iC \row mul add \length\space add){\iB}{C\iA}
      \psRelNode(!\startX\space \height\space add \startY\space \iC \row mul add)(!\startX\space 2 \height\space mul add \startY\space \iC \row mul add){\iB}{D\iA}
      \pspolygon[fillstyle=solid,fillcolor=nicered](A\iA)(B\iA)(C\iA)(D\iA)
     }
    }
   \psset{nrot=:U,offset=-19pt,linestyle=dashed,fillcolor=nicered}
    \pcline{<->}(A1)(B1)
    \ncput*{\SI{\length}{\cm}}
    \pcline{<->}(B1)(C1)
    \ncput*{\scriptsize{\SI{\height}{\cm}}}
  \end{pspicture}
\end{figure}
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks | Nested Loops

Post by CrazyHorse »

[quote="svend_tveskaeg"][/quote]

think _always_ easy! Drawing simple lines or rectangles should be drawn as what they are! And by the way: Using siunitx here is a bit of overkill ...

Code: Select all

\documentclass{article}

\usepackage{pstricks-add}
\usepackage[locale=DE]{siunitx}
\definecolor{nicered}{rgb}{0.796,0.255,0.329}

\begin{document}

\def\WidthPallet{46}
\def\length{22.8}
\def\height{5.4}
\def\rows{2}
\def\EachRow{8}
\pstFPdiv\faktor{\EachRow}{\rows}
\def\startX{\WidthPallet\space \EachRow\space \height\space mul sub 2 div }
\def\startY{\WidthPallet\space \rows\space \length\space mul sub 2 div }
 \psset{unit=0.25cm}

  \begin{pspicture}(50,50)
    \pspolygon[fillstyle=solid,fillcolor=brown](0,0)(0,\WidthPallet)(\WidthPallet,\WidthPallet)(\WidthPallet,0)
\psset{fillstyle=solid,fillcolor=nicered,dimen=middle}
\rput(!\startX \startY){%
  \multido{\rA=0+\height}{\EachRow}{%
    \multido{\rB=0.0+\length}{\rows}{%
        \psframe(\rA,\rB)(!\rA\space \height\space add \length\space \rB\space add)
    }}}%
\psset{nrot=:U,offset=-19pt,linestyle=dashed,fillcolor=nicered}
\pcline{<->}(!\startX \length\space 0.9 mul)%
            (!\startX \height\space add \length\space 0.9 mul)
\ncput*{\scriptsize\SI{\height}{\cm}}
\pcline{<->}(!\startX \startY)(!\startX \length)
\ncput*{\scriptsize\SI{\length}{\cm}}
\end{pspicture}

\end{document}
Last edited by CrazyHorse on Thu Nov 01, 2012 11:00 am, edited 1 time in total.
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

PSTricks | Nested Loops

Post by svend_tveskaeg »

CrazyHorse wrote:think _always_ easy!
That is the difference between an expert and a novice. I get stock with the first idea that comes to my mind and you (immediately?) think of an elegant solution. :D

Your code works like a charm!

Thank you very much for helping me once again!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
CrazyHorse
Posts: 351
Joined: Sat Aug 02, 2008 8:47 am

PSTricks | Nested Loops

Post by CrazyHorse »

svend_tveskaeg wrote:
CrazyHorse wrote:think _always_ easy!
That is the difference between an expert and a novice. I get stock with the first idea that comes to my mind and you (immediately?) think of an elegant solution. :D

Your code works like a charm!
I corrected it. The \rB in the first loop is superflous
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: PSTricks | Nested Loops

Post by svend_tveskaeg »

Appreciated!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply