如何设置相对于父节点的标签宽度?

如何设置相对于父节点的标签宽度?

我想在 tikz 中创建一种多节点,节点角落处有一些注释。在我的示例中,我通过标签解决了这个问题,注释位于右下角。(1) 我想避免注释在节点的其他区域运行,因此我设置了文本宽度。

但是我无法将宽度设置为主框的一半。

如何设置标签相对于主框大小的文本宽度?

(1)我也可以通过定义一个新的宏来实现,例如http://www.texample.net/tikz/examples/periodic-table-of-chemical-elements/ 但我更喜欢用标签来做。

\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}

在此处输入图片描述

在框 2 中,我希望其外观与框 1 相同,但没有固定的文本宽度。角落中的文本应为框宽度的一半。

答案1

可以使用基于(主节点)宽度的extra节点:text width\tikzlastnode

\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}

在此处输入图片描述

相关内容