0

I have been using the following code to draw cubes for a volume worksheet. Full cubes are no problem but when there are half cubes for a dimension, it places the layer of half cubes in the middle somewhere. I was wondering if someone could help me put the fractional layer at the top for height or on the end for depth or width. I have two questions: 1. Can I make the half sizes go on top or the end 2. Can I refer to the direct height or width rather than the hack I used to get 1 less than the actual so the code fits.
This is what I get. enter image description here

but I would like the half sizes to go on top.
Maybe I'm getting the whole code macros wrong. Someone help please.

\documentclass[11pt,fleqn]{examdesign}
    \usepackage{savesym}
    \usepackage{amsmath}
    \savesymbol{iint}
    \usepackage{txfonts}
    \restoresymbol{TXF}{iint}
    %%\usepackage{amssymb}  causes an error when loaded
    \usepackage{pifont}
    \usepackage[usenames,dvipsnames]{xcolor}
    \usepackage{xspace}
    \SectionFont{\large\sffamily}
    \usepackage[shortlabels,inline]{enumitem}  %%shortlabels here for matching
    \usepackage{tkz-euclide} %%%%%%%%%%%for marking angles
    \usetkzobj{all}  %%%%%%%%%%%necessary cause using overleaf
    \usepackage{tikz,ifthen,tikz-3dplot}
    \usepackage[font=scriptsize,hypcap=false]{caption}
    \graphicspath{ {./dir1/} }
    \usepackage{hyperref}
    \Fullpages
    \ContinuousNumbering
    \ShortKey
    %%\NoKey
    \DefineAnswerWrapper{}{}
    \NumberOfVersions{1}
    \IncludeFromFile{foobar.tex}

    \class{necessary for examdesign}

    %%%%%%%%%%%%%%%%%%%%% tikz set for cube%%%%%%%%%%%%%%%%%%%%%%
    \newcommand{\cubelength}{0.93}
    \tikzset{
      cube/.pic={
        \draw[] (0,0,0) -- (0,\cubelength,0) -- (\cubelength,\cubelength,0) -- (\cubelength,0,0) -- cycle;
        %draw the back-right of the cube
        \draw[] (0,0,0) -- (0,\cubelength,0) -- (0,\cubelength,\cubelength) -- (0,0,\cubelength) -- cycle;
        %draw the back-left of the cube
        \draw[] (0,0,0) -- (\cubelength,0,0) -- (\cubelength,0,\cubelength) -- (0,0,\cubelength) -- cycle;
        %draw the front-right of the cube
        \draw[fill=magenta] (\cubelength,0,0) -- (\cubelength,\cubelength,0) -- (\cubelength,\cubelength,\cubelength) -- (\cubelength,0,\cubelength)-- cycle;
        %draw the front-left of the cube
        \draw[fill=orange] (0,\cubelength,0) -- (\cubelength,\cubelength,0) -- (\cubelength,\cubelength,\cubelength) -- (0,\cubelength,\cubelength) -- cycle;
        %draw the top of the cube
        \draw[fill=cyan] (0,0,\cubelength) -- (0,\cubelength,\cubelength) -- (\cubelength,\cubelength,\cubelength) -- (\cubelength,0,\cubelength) -- cycle;        
                }
            }
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%
    %%%BEGIN DOCUMENT
    %%%%%
    \begin{document}
                \begin{scriptsize}
    \newcommand{\Depth}{3}
    \newcommand{\Height}{3.5}
    \newcommand{\Width}{4}
    \newcommand{\dcube}{\the\numexpr\Depth-1\relax}
    \newcommand{\wcube}{\the\numexpr\Width-1\relax}
    \pgfmathsetmacro{\hcube}{(\Height-1)}
 \begin{tikzpicture}[scale=1]
    \coordinate (A) at (0,0,0);
    \coordinate (B) at (\Width,0,0);
    \coordinate (C) at (\Width,0,\Depth);
    \coordinate (D) at (0,0,\Depth);
    \coordinate (E) at (0,\Height,\Depth);
    \coordinate (F) at (0,\Height,0);
    \coordinate (G) at (\Width,\Height,0);
    \coordinate (H) at (\Width,\Height,\Depth);



    \draw[fill=red!20,opacity=0.2] (A) -- (B) -- (C) -- (D) -- cycle;% Bottom Face
    \draw[fill=gray!50] (F) -- (G) -- (B) -- (A) -- cycle;% Back Face
    \draw[fill=gray!30] (A) -- (D) -- (E) -- (F) -- cycle;% Left Face
    \draw[fill=gray!30] (B) -- (C) -- (H) -- (G) -- cycle;% Right Face
    \draw[] (E) -- (F) -- (G) -- (H) -- cycle;% Top Face
    \draw[dashed,gray] (A) -- (F);
    \draw[dashed,gray] (D) -- (A) --(B);
    \draw[fill=gray!30] (B) -- (C) -- (H) -- (G); %%right face
    \draw[fill=none] (C) -- (D) --(E);
    \tkzMarkRightAngle[draw=blue,size=.2](C,B,G);
    \draw[fill=gray!50] (C) -- (D) -- (E) -- (H) -- cycle;% Front Face
    \draw[<->,blue,>=latex]([xshift=4mm]B)--node[fill=white,xshift=3mm]{\Height\text{ cm}}([xshift=4mm]G);
    \draw[<->,blue, >=latex]([yshift=-3mm]C)--node[fill=white,yshift=-3mm]{\Width \text{  cm}}([yshift=-3mm]D);
    \draw[<->,blue]([xshift=5mm]B)--node[xshift=3mm,yshift=-3mm,fill=white]{\Depth\text{ cm}}([xshift=5mm]C);

    \draw[|-|,blue,>=latex]([xshift=-4mm]E)--node[right,fill=white,xshift=-3mm,yshift=0.5mm]{$w$}([xshift=-4mm]F);

        \foreach \x in {0,...,\wcube}{
        \foreach \y in {0,1,2,\hcube}{
        \foreach \z in {0,...,\dcube}{
        \pic at (\x,\y,\z) {cube};
       }}}

       \pic at (0,\Height+2,0) {cube};

    \end{tikzpicture}

    \end{scriptsize}
    \end{document}
E.Yu
  • 337

1 Answers1

1

This is an ad hoc fix of the issue. Some integer trickery. It redefines the basis vectors for fractional cubes. (Note that xscale and yscale won't do.)

\documentclass[11pt,fleqn]{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{tkz-euclide} 
\usepackage{hyperref}
  %%%%%%%%%%%%%%%%%%%%% tikz set for cube%%%%%%%%%%%%%%%%%%%%%%
  \newcommand{\cubelength}{0.93}
  \tikzset{
    cube/.pic={
      \draw[] (0,0,0) -- (0,\cubelength,0) -- (\cubelength,\cubelength,0) -- (\cubelength,0,0) -- cycle;
      %draw the back-right of the cube
      \draw[] (0,0,0) -- (0,\cubelength,0) -- (0,\cubelength,\cubelength) -- (0,0,\cubelength) -- cycle;
      %draw the back-left of the cube
      \draw[] (0,0,0) -- (\cubelength,0,0) -- (\cubelength,0,\cubelength) -- (0,0,\cubelength) -- cycle;
      %draw the front-right of the cube
      \draw[fill=magenta] (\cubelength,0,0) -- (\cubelength,\cubelength,0) -- (\cubelength,\cubelength,\cubelength) -- (\cubelength,0,\cubelength)-- cycle;
      %draw the front-left of the cube
      \draw[fill=orange] (0,\cubelength,0) -- (\cubelength,\cubelength,0) -- (\cubelength,\cubelength,\cubelength) -- (0,\cubelength,\cubelength) -- cycle;
      %draw the top of the cube
      \draw[fill=cyan] (0,0,\cubelength) -- (0,\cubelength,\cubelength) -- (\cubelength,\cubelength,\cubelength) -- (\cubelength,0,\cubelength) -- cycle;        
              }
          }
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%
    %%%BEGIN DOCUMENT
    %%%%%
\begin{document}
\newcommand{\Depth}{3}
\newcommand{\Height}{3.5}
\newcommand{\Width}{4}
\pgfmathsetmacro{\effDepth}{\Depth-1}
\pgfmathsetmacro{\effHeight}{\Height-1}
\pgfmathsetmacro{\effWidth}{\Width-1}
\begin{tikzpicture}[scale=1]
    \coordinate (A) at (0,0,0);
    \coordinate (B) at (\Width,0,0);
    \coordinate (C) at (\Width,0,\Depth);
    \coordinate (D) at (0,0,\Depth);
    \coordinate (E) at (0,\Height,\Depth);
    \coordinate (F) at (0,\Height,0);
    \coordinate (G) at (\Width,\Height,0);
    \coordinate (H) at (\Width,\Height,\Depth);



    \draw[fill=red!20,opacity=0.2] (A) -- (B) -- (C) -- (D) -- cycle;% Bottom Face
    \draw[fill=gray!50] (F) -- (G) -- (B) -- (A) -- cycle;% Back Face
    \draw[fill=gray!30] (A) -- (D) -- (E) -- (F) -- cycle;% Left Face
    \draw[fill=gray!30] (B) -- (C) -- (H) -- (G) -- cycle;% Right Face
    \draw[] (E) -- (F) -- (G) -- (H) -- cycle;% Top Face
    \draw[dashed,gray] (A) -- (F);
    \draw[dashed,gray] (D) -- (A) --(B);
    \draw[fill=gray!30] (B) -- (C) -- (H) -- (G); %%right face
    \draw[fill=none] (C) -- (D) --(E);
    \tkzMarkRightAngle[draw=blue,size=.2](C,B,G);
    \draw[fill=gray!50] (C) -- (D) -- (E) -- (H) -- cycle;% Front Face
    \draw[<->,blue,>=latex]([xshift=4mm]B)--node[fill=white,xshift=3mm]{\Height\text{ cm}}([xshift=4mm]G);
    \draw[<->,blue, >=latex]([yshift=-3mm]C)--node[fill=white,yshift=-3mm]{\Width \text{  cm}}([yshift=-3mm]D);
    \draw[<->,blue]([xshift=5mm]B)--node[xshift=3mm,yshift=-3mm,fill=white]{\Depth\text{ cm}}([xshift=5mm]C);

 \draw[|-|,blue,>=latex]([xshift=-4mm]E)--node[right,fill=white,xshift=-3mm,yshift=0.5mm]{$w$}([xshift=-4mm]F);
     \foreach \x in {0,...,\effWidth,\effWidth}{
        \pgfmathifisint{\x}{\pgfmathsetmacro{\myx}{1}}{\pgfmathsetmacro{\myx}{\x-int(\x))}}
     \foreach \y in {0,...,\effHeight,\effHeight}{
        \pgfmathifisint{\y}{\pgfmathsetmacro{\myy}{1}}{\pgfmathsetmacro{\myy}{\y-int(\y))}}
     \foreach \z in {0,...,\effDepth,\effDepth}{
        \pgfmathifisint{\z}{\pgfmathsetmacro{\myz}{1}}{\pgfmathsetmacro{\myz}{\z-int(\z))}}
     \path (\x+1-\myx,\y+1-\myy,\z+1-\myz) 
     pic[z={(-0.5*\myz,-0.5*\myz)},x={(\myx,0)},y={(0,\myy)}] {cube};
    }}}
    \pic at (0,\Height+2,0) {cube};

\end{tikzpicture}

\end{document}

enter image description here

If you change the \Width to become \newcommand{\Width}{4.5}, you get

enter image description here

and if you change the \Depth to become \newcommand{\Depth}{3.5}

enter image description here

It works with other fractions, too, e.g.

\newcommand{\Depth}{3}
\newcommand{\Height}{3.2}
\newcommand{\Width}{4}

yields

enter image description here

Of course, these are not orthogonal projections. There are cube arrays available that do not have this limitation. There are, to the best of my knowledge, no fractional versions thereof on the market. The above trick will work for those, too.

  • Thank you. This will do very well for my purpose. Much appreciated. – E.Yu Apr 09 '20 at 14:24
  • I used resize box to make the digram fit for longer lengths but I had to make the text really Large in order to accommodate the change. Any way to scale this another way to constrain the picture to a reasonable size but keep the proportions? – E.Yu Apr 09 '20 at 19:32
  • @E.Yu At the moment you have scale=1, you could just decrease the scale, e.g. scale=0.6. This does (by default) not change the size of the texts, but scales the distances/coordinates. –  Apr 09 '20 at 19:37