I have the following code.

\documentclass {standalone}
\usepackage    {tikz}

\def\width {6}
\def\x     {0.8}

\begin{tikzpicture}[line cap=round, line join=round,magenta]
\foreach\i/\j in {0/0, 0/\height-\x, \width-\x/0, \width-\x/\height-\x}
    \draw[dashed,fill=magenta!15] (\i,\j) rectangle (\i+\x,\j+\x);
    \node at (0.5*\x,3) {$x$};
     \node at (0.5*\x,1) {$x$};
      \node at (1.3*\x,0.4) {$x$};
         \node at (1.3*\x,3.6) {$x$};
            \node at (7*\x,3) {$x$};
     \node at (7*\x,1) {$x$};
      \node at (6.3*\x,0.4) {$x$};
         \node at (6.3*\x,3.6) {$x$};
  \draw[white] (0,-1) --(6,-1);
 %  \draw[black,|-stealth-stealth] (0,-0.5) -- (6,-0.5);
  \draw[thick] (0,0) rectangle (\width,\height);
  \draw[white] (\x,\x) rectangle (\width-\x,\height-\x);
%  \draw[|<->|,black] (0,-0.5)--(6,-0.5) node[midway,below] {$36$};
    \draw[white] (-1,0) --(-1,5);
  %  \draw[|<->|,black] (-0.5,0)--(-0.5,4) node[midway,sloped] {$24$};
       \draw[white] (6.5,0) --(6.5,5);
       \draw[|<->|,black] (-0.5,0)--node[fill=white] {$24$} (-0.5,4);
        \draw[|<->|,black] (0,-0.5)--node[fill=white] {$36$} (6,-0.5);

\begin{tikzpicture}[scale=1.25,x={(-0.9220cm,-0.1626cm)},y={(0.3872cm,-0.3872cm)},z={(0cm,0.9076cm)},line cap=round,line join=round]
  \begin{scope}[canvas is xy plane at z=0,magenta]
    \foreach\i/\j in {0/0, 0/\height-\x, \width-\x/0, \width-\x/\height-\x}
%    {
%      \draw[dashed,fill=blue!15] (\i,\j) rectangle (\i+\x,\j+\x);
%    }
    %\draw (0,0) rectangle (\width,\height);
    \draw[thick,magenta,fill=magenta!4] (\x,\x) rectangle (\width-\x,\height-\x);
  \begin{scope}[canvas is xy plane at z=\x]
    \draw[thick,magenta] (\x,\x) rectangle (\width-\x,\height-\x);
  \foreach\i/\j in {\x/\x, \x/\height-\x, \width-\x/\x, \width-\x/\height-\x}
    \draw[thick,magenta] (\i,\j,0) -- (\i,\j,\x);

I want to change two things.

  1. In 24 times 36 inches I want to color white part to magenta and magenta to white. How can I do that?

  2. In 3D box, I want to rotate my box little bit so it doesnot cover so much page and still looks nice. And I want to color my box, which is off course without top.

Thanks in advance.


1 Answer 1


Let's start with the 2d picture.

Here I define a \tikzrectwithsquares command that takes the x value, the width and the height and constructs the rectangle accordingly.

First, all kinds of coordinates will be defined (not all of those will be used). These are the corners of the rectangle as well as all the points of that plus-shaped inner polygon. These extra coordinates and the -|/|- path operations can then be used to outline this plus shape and fill it while not filling the four squares in the corner.

The keys x and y are used to scale the coordinate system so that we can use 36 and 24 directly in the drawing code.

The 3d picture is actually easier using the 3d view key. Here I'm using an azimuth of −60° and a levitation of 20° to rotate the rectangle a bit more landscape-y.

If you don't want the sides behind the front to shine through, remove fill opacity = .5 from rf/front.

We could have used the 3d library's canvas is plane … keys but with these simple rectangles it is almost easier to just use the corners directly.


\usetikzlibrary{arrows.meta, perspective}
\tikzset{scale xyz/.code={%
  \pgf@xx=\pgfmathresult\pgf@xx \pgf@xy=\pgfmathresult\pgf@xy
  \pgf@yx=\pgfmathresult\pgf@yx \pgf@yy=\pgfmathresult\pgf@yy
  \pgf@zx=\pgfmathresult\pgf@zx \pgf@zy=\pgfmathresult\pgf@zy}}
  % rectangle with squares:
    x=+.16667cm, y=+.16667cm, line cap=round, line join=round,
    magenta, >={Straight Barb[angle=60:3pt 2]},
    xy shift/.style 2 args={shift={({##1(#1)},{##2(#1)})}}},
  rws/make extra coordinates/.style n args={3}{
    label={[shape=coordinate,xy shift={#1}{#2},name=\tikzlastnode']center:},
    label={[shape=coordinate,xy shift={#1}{0*},name=\tikzlastnode-x]center:},
    label={[shape=coordinate,xy shift={0*}{#2},name=\tikzlastnode-y]center:}},
  %%% drawing styles:
  rws/wo x/.style={fill=magenta!15},
  rws/with x/.style={auto=right, draw, dashed},
  rws/rect/.style={draw, thick},
  rws/measure/.style={black, nodes={fill=white}, |<->|},
  % rectangle folded:
    3d view={-60}{20}, scale xyz=1/6,
    line cap=round, line join=round, magenta,
    >={Straight Barb[angle=60:3pt 2]},},
  rf/make extra coordinate/.style={
  %%% drawing styles:
  rf/bottom/.style={draw, fill=magenta!15},
  rf/front/.style={rf/bottom, fill opacity=.5},
\newcommand*\tikzrectwithsquares[4][]{% #2 = x, #3 = w, #4 = h
  \coordinate[rws/make extra coordinates=++] (BL) at (0,0)
   coordinate[rws/make extra coordinates=-+] (BR) at (right:{#3})
   coordinate[rws/make extra coordinates=--] (TR) at ({#3},{#4})
   coordinate[rws/make extra coordinates=+-] (TL) at (up:{#4});
  \path[rws/wo x](BL-x) -| (BR') -| (TR-y) -| (TR-x) -| (TL') -| (BL-y) -| cycle;
  \path[rws/with x] foreach[count=\rot from 0] \corner in {BL, BR, TR, TL}{
      [rotate=\rot*90] (\corner) -| node [near end]{$x$} (\corner')
                                 -| node [near start]{$x$} cycle          };
  \draw[rws/rect] (BL) rectangle (TR);
  \path[rws/measure] ([xshift=+-5mm]BL) edge node{$#4$} ([xshift=+-5mm]TL)
                     ([yshift=+-5mm]BL) edge node{$#3$} ([yshift=+-5mm]BR);
\newcommand*\tikzrectfolded[4][]{% #2 = x, #3 = w, #4 = h
  \coordinate[rf/make extra coordinate={#2}] (BL) at (0,0,0)
   coordinate[rf/make extra coordinate={#2}] (BR) at ({#3},0,0)
   coordinate[rf/make extra coordinate={#2}] (TR) at ({#3},{#4},0)
   coordinate[rf/make extra coordinate={#2}] (TL) at (0,{#4},0);
   \path[rf/back]   (TL) -- (TR) -- (TR') -- (TL') -- cycle;
   \path[rf/back]   (TR) -- (BR) -- (BR') -- (TR') -- cycle;
   \path[rf/bottom] (BL) -- (BR) -- (TR)  -- (TL)  -- cycle;
   \path[rf/front]  (TL) -- (BL) -- (BL') -- (TL') -- cycle;
   \path[rf/front]  (BL) -- (BR) -- (BR') -- (BL') -- cycle;
\tikzrectfolded     [baseline=0]{4}{36}{24}
\tikzset{debug node/.style={append after command={


enter image description here

  • A bit compicated code. How about 3D?
    – Guest
    Mar 27, 2023 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .