1

I want to create a kind of a multinode in tikz with little comments in the corners of the node. In my example I solved it via a label and the comment it is in the right lower corner.(1) I want to avoid that my comment is running in other areas of the node and I set the text width.

But I have problems to set the width to the half of the main box.

How can I set the text width of the label relative to the size of the main box?

(1) I could also do it by defining a new macro, like in http://www.texample.net/tikz/examples/periodic-table-of-chemical-elements/ But I prefer to do it with labels.

\documentclass[tikz,margin=5mm]{standalone}
\usepackage{luatex85}%if used with lualatex
\usetikzlibrary{positioning}
\begin{document}
  \begin{tikzpicture}[
      box/.style={
          text width=5cm,align=center,minimum height=4cm,rectangle,draw,rounded corners,
      },
      commentedbox/.style={
          %The label should be 1/2 of the width of the actual node
          label={[anchor=south east,
          %text width=0.5\tikzlastnode.x,% half width of current main node,  How???
          font=\footnotesize,]south east:{#1}}
      }
    ]
  %label as argument of node  with manual text width
  \node[box,
      label={[anchor=south east,font=\footnotesize,text width=25mm]south east:{Here is a comment for box 1. It should use half of the main box size}}
    ] (box1) {The main text of box 1};
  %label as argument of style
  \node[below =of box1.south,
      box,commentedbox={Here is a comment for box 2.  It should use half of the main box size}
    ] {The main text of box 2};

  \end{tikzpicture}
\end{document}

enter image description here

In box 2 I want the same look as in box 1, but without a fix text width. The text in the corner should be the half width of the box.

  • If you don't set the text width (or use a \parbox), you can't wrap the text. You can compute the width of a named node, but it is more effort than it is worth. Easiest is to set the width of the parent using a macro or length which can be accessed by the label. – John Kormylo Sep 19 '17 at 19:14
4

It's possible to use an extra node with text width based in \tikzlastnode (main node) width:

\documentclass[tikz,margin=5mm]{standalone}
\usepackage{luatex85}%if used with lualatex
\usetikzlibrary{positioning, calc}
\begin{document}
  \begin{tikzpicture}[
      box/.style={
          text width=5cm,
          align=center,
          minimum height=4cm,
          rectangle,
          draw,
          rounded corners,
      },
      commentedbox/.style={
            append after command={
                \pgfextra
                \path let 
                \p1=($(\tikzlastnode.south)-(\tikzlastnode.south east)$), 
                \n2={veclen(\x1,\y1)} in
                node[draw, rounded corners,
                    %inner xsep=0pt,
                    text width=\n2-.666em,
                    anchor=south east, font=\footnotesize] 
                    at (\tikzlastnode.south east) {#1};
                \endpgfextra}}
    ]
  %label as argument of node  with manual text width
  \node[box,
  label={[anchor=south east, font=\footnotesize,
  text width=25mm] south east:{Here is a comment for box 1. It should use half of the main box size}}
    ] (box1) {The main text of box 1};
  %label as argument of style
  \node[below =of box1.south,
      box, commentedbox={Here is a comment for box 2.  It should use half of the main box size}
    ] (box2) {The main text of box 2};
    \draw (box2.south)--(box2.north);
  \end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

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.