1

I have a cube as given below. I want to add a text attribute for the cube, such that the text below the cube is rotated along with the figure

enter image description here

This is my definition of the cube

  \tikzset{
    annotated cuboid/.pic={
      \tikzset{%
        every edge quotes/.append style={midway, auto},
        /cuboid/.cd,
        #1
      }
      \draw [\cubeline,every edge/.append style={pic actions,\cubeback, opacity=.5}, pic actions]
      (0,0,0) coordinate (o-\cubelabel) -- ++(-\cubescale*\cubex,0,0) coordinate (a-\cubelabel) -- ++(0,-\cubescale*\cubey,0) coordinate (b-\cubelabel) edge coordinate [pos=1] (g-\cubelabel) ++(0,0,-\cubescale*\cubez)  -- ++(\cubescale*\cubex,0,0) coordinate (c-\cubelabel) -- cycle
      (o-\cubelabel) -- ++(0,0,-\cubescale*\cubez) coordinate (d-\cubelabel) -- ++(0,-\cubescale*\cubey,0) coordinate (e-\cubelabel) edge (g-\cubelabel) -- (c-\cubelabel) -- cycle
      (o-\cubelabel) -- (a-\cubelabel) -- ++(0,0,-\cubescale*\cubez) coordinate (f-\cubelabel) edge (g-\cubelabel) -- (d-\cubelabel) -- cycle;

    },
    /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,
    label/.store in=\cubelabel,
    line/.store in=\cubeline,
    backline/.store in=\cubeback,
    width=10,
    height=10,
    depth=10,
    units=cm,
    scale=.1,   
    line=draw,
    backline=densely dashed
  }

My full code (MWE):

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{quotes,arrows.meta, decorations.pathreplacing}
  \tikzset{
    annotated cuboid/.pic={
      \tikzset{%
        every edge quotes/.append style={midway, auto},
        /cuboid/.cd,
        #1
      }
      \draw [\cubeline,every edge/.append style={pic actions, \cubeback, opacity=.5}, pic actions]
      (0,0,0) coordinate (o-\cubelabel) -- ++(-\cubescale*\cubex,0,0) coordinate (a-\cubelabel) -- ++(0,-\cubescale*\cubey,0) coordinate (b-\cubelabel) edge coordinate [pos=1] (g-\cubelabel) ++(0,0,-\cubescale*\cubez)  -- ++(\cubescale*\cubex,0,0) coordinate (c-\cubelabel) -- cycle
      (o-\cubelabel) -- ++(0,0,-\cubescale*\cubez) coordinate (d-\cubelabel) -- ++(0,-\cubescale*\cubey,0) coordinate (e-\cubelabel) edge (g-\cubelabel) -- (c-\cubelabel) -- cycle
      (o-\cubelabel) -- (a-\cubelabel) -- ++(0,0,-\cubescale*\cubez) coordinate (f-\cubelabel) edge (g-\cubelabel) -- (d-\cubelabel) -- cycle;
      ;
    },
    /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,
    label/.store in=\cubelabel,
    line/.store in=\cubeline,
    backline/.store in=\cubeback,
    width=10,
    height=10,
    depth=10,
    units=cm,
    scale=.1,
    line=draw,
    backline=densely dashed,
  }
\newcommand{\cuboid}[2]{
    \begin{tikzpicture}
     \pic [#1] at (0,0) {annotated cuboid={#2}};
    \end{tikzpicture}
}
\tikzstyle{cubecontainer}=[outer sep = 0pt, inner sep= 0pt]
\tikzstyle{connectarrow}=[-{Triangle[angle=60:0pt 2]}, line width= 10pt, shorten >=3mm,shorten <=3mm, draw=gray!60]
\begin{document}
\begin{tikzpicture}
\node[cubecontainer] (a) at (0,0) {\cuboid{fill=cyan!20, text=green!50!black, draw=black}{label=A, width=6, height=20, depth=15, units=mm}};
%\node[cubecontainer] (b) at (2,0) {\cuboid{fill=cyan!20, text=green!50!black, draw=black}{label=B, width=6, height=20, depth=15, units=mm}};
%\node[cubecontainer] (c) at (5,0) {\cuboid{fill=cyan!20, text=green!50!black, draw=black}{label=C, width=6, height=20, depth=15, units=mm}};
%%\node[draw,dotted,fit=(a) (b) (c)] {};
\end{tikzpicture}
\end{document}
  • Have you tried putting a node below? – TeXnician Apr 16 '17 at 13:11
  • Node is so expensive. I have many cubes, so that is reason why I need add a attribute for that – John Apr 16 '17 at 13:15
  • 1
    \pgftext is less expensive. And \hbox plus \pgfqboxsynced is even less expensive. And \hbox plus \pgfqbox is way too cheap. – Symbol 1 Apr 16 '17 at 15:48
  • Does my answer here, tex.stackexchange.com/questions/254010/…, fit your need? – Steven B. Segletes Apr 17 '17 at 3:11
2

If a simple node like this: \node [below of=a, node distance=1.4cm,rotate=45,xshift=-1cm] (critical) {20x30x20}; is to expensive for you, you can set the label inside tikzset cubecontainer, for rotation you can use \rotatebox{45}{#1}. #1 is the label, defined in the document:

\tikzset{cubecontainer/.style={outer sep= 0pt, inner sep= 0pt,label={265:\rotatebox{45}{#1}}}
}%optional with red background ... [fill=red]265 ....

265 is the positioning angle, counted anticlockwise from 0=east to 360=east. (By a square: 0=east, 90=north, 180=west, 270=south,... )

... and use it with labeling the node with label = 20x20x30 (\node[cubecontainer=20x20x30]):

\node[cubecontainer=20x20x30] (a) at (0,0) {\cuboid{fill=cyan!20, text=green!50!black, draw=black}{label=A, width=6, height=20, depth=15, units=mm}};

.


EDIT: 2.Verison of cubecontainer - Change font size with the following cubecontainer/.style

\tikzset{cubecontainer/.style={outer sep= 0pt, inner sep= 0pt,label={265:\rotatebox{45}{\Huge #1}}} }

  1. Version:

enter image description here

  1. Version: font size (\Huge)

(Comparison of the first(left) and second version(right))

enter image description here

MWE:

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{quotes,arrows.meta, decorations.pathreplacing}
  \tikzset{
    annotated cuboid/.pic={
      \tikzset{%
        every edge quotes/.append style={midway, auto},
        /cuboid/.cd,
        #1
      }
      \draw [\cubeline,every edge/.append style={pic actions, \cubeback, opacity=.5}, pic actions]
      (0,0,0) coordinate (o-\cubelabel) -- ++(-\cubescale*\cubex,0,0) coordinate (a-\cubelabel) -- ++(0,-\cubescale*\cubey,0) coordinate (b-\cubelabel) edge coordinate [pos=1] (g-\cubelabel) ++(0,0,-\cubescale*\cubez)  -- ++(\cubescale*\cubex,0,0) coordinate (c-\cubelabel) -- cycle
      (o-\cubelabel) -- ++(0,0,-\cubescale*\cubez) coordinate (d-\cubelabel) -- ++(0,-\cubescale*\cubey,0) coordinate (e-\cubelabel) edge (g-\cubelabel) -- (c-\cubelabel) -- cycle
      (o-\cubelabel) -- (a-\cubelabel) -- ++(0,0,-\cubescale*\cubez) coordinate (f-\cubelabel) edge (g-\cubelabel) -- (d-\cubelabel) -- cycle;
      ;
    },
    /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,
    label/.store in=\cubelabel,
    line/.store in=\cubeline,
    backline/.store in=\cubeback,
    width=10,
    height=10,
    depth=10,
    units=cm,
    scale=.1,
    line=draw,
    backline=densely dashed,
  }
\newcommand{\cuboid}[2]{
    \begin{tikzpicture}
     \pic [#1] at (0,0) {annotated cuboid={#2}};
    \end{tikzpicture}
}
\tikzset{cubecontainer/.style={outer sep= 0pt, inner sep= 0pt,label={[fill=white]265:\rotatebox{45}{#1}}}
}
%\tikzset{cubecontainer/.style={outer sep= 0pt, inner sep= 0pt,label={265:\rotatebox{45}{\Huge #1}}} }
\tikzstyle{connectarrow}=[-{Triangle[angle=60:0pt 2]}, line width= 10pt, shorten >=3mm,shorten <=3mm, draw=gray!60]
\begin{document}
\begin{tikzpicture}
\node[cubecontainer=20x20x30] (a) at (0,0) {\cuboid{fill=cyan!20, text=green!50!black, draw=black}{label=A, width=6, height=20, depth=15, units=mm}};
%\node [below of=a, node distance=1.4cm,rotate=45,xshift=-1cm]  (critical) {20x30x20};
\end{tikzpicture}
\end{document}
|improve this answer|||||
  • Great solution. How can I change the label size. It means the number font size? – John Nov 3 '17 at 9:03
  • 1
    @user8264 you could use font size \Huge,\footnotesize or \tiny... with \tikzset{cubecontainer/.style={outer sep= 0pt, inner sep= 0pt,label={265:\rotatebox{45}{\Huge #1}}} }. I have updated my answer. – Bobyandbob Nov 3 '17 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.