14

How can I draw these solid figures with LaTeX?

enter image description here

\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{3}
\draw[red,fill=yellow] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw[red,fill=yellow] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[red,fill=yellow] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}

I have used that code. How can I draw and write the quotes and dimensions?

Cragfelt
  • 4,005
ryuk
  • 2,257

1 Answers1

27

You can annotate the diagram in various ways. Here's one approach.

coordinates are placed when constructing the cuboid. These are then used in a later path as starting points. Further coordinates are placed offset from the originals (a little below, a little left, a little right...) which are then used to draw the lines. Drawing these with the edge operation makes it possible to add arrow tips for the start and end of each line without using separate paths, and the quotes library is then used to label these edges using the edge ["<label>"] syntax. The options for these labels are set to auto to offset them. Where TikZ would 'jump the wrong way', a ' is added after the quoted label to 'swap' the offset e.g. left rather than right.

\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{quotes,arrows.meta}
\begin{document}
\begin{tikzpicture}[every edge quotes/.append style={auto, text=blue}]
  \pgfmathsetmacro{\cubex}{5}
  \pgfmathsetmacro{\cubey}{1}
  \pgfmathsetmacro{\cubez}{3}
  \draw [draw=blue, every edge/.append style={draw=blue, densely dashed, opacity=.5}, fill=magenta]
    (0,0,0) coordinate (o) -- ++(-\cubex,0,0) coordinate (a) -- ++(0,-\cubey,0) coordinate (b) edge coordinate [pos=1] (g) ++(0,0,-\cubez)  -- ++(\cubex,0,0) coordinate (c) -- cycle
    (o) -- ++(0,0,-\cubez) coordinate (d) -- ++(0,-\cubey,0) coordinate (e) edge (g) -- (c) -- cycle
    (o) -- (a) -- ++(0,0,-\cubez) coordinate (f) edge (g) -- (d) -- cycle;
  \path [every edge/.append style={draw=blue, |-|}]
    (b) +(0,-5pt) coordinate (b1) edge ["30cm"'] (b1 -| c)
    (b) +(-5pt,0) coordinate (b2) edge ["5cm"] (b2 |- a)
    (c) +(3.5pt,-3.5pt) coordinate (c2) edge ["10cm"'] ([xshift=3.5pt,yshift=-3.5pt]e)
    ;
\end{tikzpicture}
\end{document}

annotated cuboid

If you need to draw a group of these or several in different places, defining a pic may be a convenient solution. As I've defined it, the pic annotated cuboid takes a single, optional argument which can set various parameters: width, height, depth, units (for the labels) and scale. To set colours, fills etc., you can pass an optional argument to the \pic to specify pic actions e.g. \pic [draw=red, fill=yellow, text=green] {annotated cuboid={width=10, height=45, depth=89.6, scale=.05, units=mm}};. Defaults are the current defaults for colours etc., a cuboid 10x10x10, scaled to one tenth natural size, with unit labels in centimetres.

For example:

\begin{tikzpicture}
  \pic {annotated cuboid};
  \pic [fill=magenta, text=blue, draw=blue] at (5,0) {annotated cuboid={width=30, height=5, depth=10}};
  \pic [fill=green, text=green!50!black, draw=green!25!black] at (5,-2) {annotated cuboid={width=6, height=20, depth=15, units=mm}};
  \pic at (1,-3) {annotated cuboid={width=150, height=200, depth=250, scale=.01, units=m}};
  \pic [fill=cyan, text=blue!75!cyan, draw=blue!75!cyan] at (-3,-2) {annotated cuboid={width=15, height=18, depth=13.5, units=}};
\end{tikzpicture}

produces

annotated cuboids

Complete code:

\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{quotes,arrows.meta}
\tikzset{
  annotated cuboid/.pic={
    \tikzset{%
      every edge quotes/.append style={midway, auto},
      /cuboid/.cd,
      #1
    }
    \draw [every edge/.append style={pic actions, densely dashed, opacity=.5}, pic actions]
    (0,0,0) coordinate (o) -- ++(-\cubescale*\cubex,0,0) coordinate (a) -- ++(0,-\cubescale*\cubey,0) coordinate (b) edge coordinate [pos=1] (g) ++(0,0,-\cubescale*\cubez)  -- ++(\cubescale*\cubex,0,0) coordinate (c) -- cycle
    (o) -- ++(0,0,-\cubescale*\cubez) coordinate (d) -- ++(0,-\cubescale*\cubey,0) coordinate (e) edge (g) -- (c) -- cycle
    (o) -- (a) -- ++(0,0,-\cubescale*\cubez) coordinate (f) edge (g) -- (d) -- cycle;
    \path [every edge/.append style={pic actions, |-|}]
    (b) +(0,-5pt) coordinate (b1) edge ["\cubex \cubeunits"'] (b1 -| c)
    (b) +(-5pt,0) coordinate (b2) edge ["\cubey \cubeunits"] (b2 |- a)
    (c) +(3.5pt,-3.5pt) coordinate (c2) edge ["\cubez \cubeunits"'] ([xshift=3.5pt,yshift=-3.5pt]e)
    ;
  },
  /cuboid/.search also={/tikz},
  /cuboid/.cd,
  width/.store in=\cubex,
  height/.store in=\cubey,
  depth/.store in=\cubez,
  units/.store in=\cubeunits,
  scale/.store in=\cubescale,
  width=10,
  height=10,
  depth=10,
  units=cm,
  scale=.1,
}
\begin{document}
\begin{tikzpicture}
  \pic {annotated cuboid};
  \pic [fill=magenta, text=blue, draw=blue] at (5,0) {annotated cuboid={width=30, height=5, depth=10}};
  \pic [fill=green, text=green!50!black, draw=green!25!black] at (5,-2) {annotated cuboid={width=6, height=20, depth=15, units=mm}};
  \pic at (1,-3) {annotated cuboid={width=150, height=200, depth=250, scale=.01, units=m}};
  \pic [fill=cyan, text=blue!75!cyan, draw=blue!75!cyan] at (-3,-2) {annotated cuboid={width=15, height=18, depth=13.5, units=}};
\end{tikzpicture}
\end{document}
cfr
  • 198,882
  • Awesome! Can you position a \pic relatively? e.g. \pic [] (pic-2) at ($ (pic-1) + (5mm,0mm) $) – Jonathan Komar Apr 20 '16 at 11:25
  • Nevermind, I found my answer here: http://tex.stackexchange.com/questions/241719/name-tikz-pic-as-if-it-was-a-node I added label/.store in=\cubelabel, So my next thing is centering a node in the shape. Your "center" is the front top right corner. Is there are way to put a node at the center of the shape \node (\cubelabel) at (whatever) {displayedtext}? I am not sure what to put in "whatever". Maybe you can help me out here. Something like at (o -| a) – Jonathan Komar Apr 20 '16 at 12:39
  • I used \node [draw,minimum width=\cubescale*\cubex,minimum height=\cubescale*\cubey] (\cubelabel) at ($ (a)!.5!(c) $) {\cubetextlabel}; for the front face, but the scale of the node is not the same as the front face. I am not sure why :( – Jonathan Komar Apr 20 '16 at 13:02
  • @cfr: Great. How to remove the annotation text, I just want to show cube only? Thanks – John Apr 05 '17 at 02:59
  • @user8264 Just don't add it. You can see where it is set, for goodness sake. – cfr Apr 05 '17 at 03:06
  • @user8264 Note that the whole point of this answer was the annotation. Nonetheless, you can just omit it if you don't want it. – cfr Apr 05 '17 at 03:08
  • Thanks. I tried with \pic [fill=gray!20, text=green!50!black, draw=black] at (6,-2) but it was fail. – John Apr 05 '17 at 03:10
  • Ask a new question if you need help. Link here for context and include a compilable example (or not compilable, if you get an error). There is better cube code around than mine, though, if you don't need annotations. Shiny ones. Stackable ones. Rotated ones. Shaded ones. Labelled ones. Mine are about as bog-standard boring as they get. – cfr Apr 05 '17 at 03:14
  • @cfr: Thanks. I asked a new question at http://tex.stackexchange.com/questions/362190/how-to-draw-connection-between-cube-in-tikz. Hope you can solve it help me – John Apr 05 '17 at 03:44