[Tex/LaTex] Draw a rectangular prism and label its dimensions

3dshapestikz-pgf

I'm trying to prepare a worksheet for my students on calculating the volume of a rectangular prism (cuboid). Using the code below I manage to generate the rectangular prism, but how do I add the dimensions? I'd like to have, for example, "4cm" for the length, placed just under the length line, "4cm" for the width, placed below right of the width line, and "6cm" for the height, placed to the right of the height line.

  1. what is the code for doing that?
  2. Generally speaking, how do I position these dimensions where I like? I may want to rotate the prism and place the dimensions above, below, center, right or left of the prism outlines. How do I do that?

My code:

\begin{tikzpicture}
\pgfmathsetmacro{\x}{1}
\pgfmathsetmacro{\y}{1}
\pgfmathsetmacro{\z}{1.5}
\path (0,0,\y) coordinate (A) (\x,0,\y) coordinate (B) (\x,0,0) coordinate (C) (0,0,0)
coordinate (D) (0,\z,\y) coordinate (E) (\x,\z,\y) coordinate (F) (\x,\z,0) coordinate (G)
(0,\z,0) coordinate (H);
\draw (A)--(B)--(C)--(G)--(F)--(B) (A)--(E)--(F)--(G)--(H)--(E);
\draw [black] (A)--(D)--(C) (D)--(H);
\end{tikzpicture}

Best Answer

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}[>=latex,scale=2]
\pgfmathsetmacro{\x}{1}
\pgfmathsetmacro{\y}{1}
\pgfmathsetmacro{\z}{1.5}
\path (0,0,\y) coordinate (A) (\x,0,\y) coordinate (B) (\x,0,0) coordinate (C) (0,0,0)
coordinate (D) (0,\z,\y) coordinate (E) (\x,\z,\y) coordinate (F) (\x,\z,0) coordinate (G)
(0,\z,0) coordinate (H);
\draw (A)--(B)--(C)--(G)--(F)--(B) (A)--(E)--(F)--(G)--(H)--(E);
\draw (A)--(D)--(C) (D)--(H);

\draw[thin,|<->|] ($(A)+(0,-4pt)$) -- node[below]{4cm}($(B)+(0,-4pt)$);
\draw[thin,|<->|] ($(B)+(-45:4pt)$) -- node[below,sloped]{4cm}($(C)+(-45:4pt)$);
\draw[thin,|<->|] ($(C)+(4pt,0)$) -- node[below,sloped]{6cm}($(G)+(4pt,0)$);

\end{tikzpicture}

\end{document}

enter image description here