TransWikia.com

TikZ nested text boxes margins

TeX - LaTeX Asked by Prunus Persica on August 2, 2020

I have nested nodes with text. The text could be variable size, however I would like a constant margin between the inner box and outer box.

I’ve played around with the text width property, but this is trial and error based, and not a perfect solution.

In this example, how do I keep the current layout, but have the right padding match the auto-generated left padding in the blue box.

documentclass[tikz, border=2mm]{standalone}
usepackage{tikz}
usetikzlibrary{shapes.multipart}

begin{document}

definecolor{CoreBlue}{HTML}{5b9bd5}
definecolor{CoreOrange}{HTML}{ec7d2d}

tikzset{
    Core/.style={rectangle, draw, fill=CoreBlue,  draw opacity=0, text=white},
    Core2/.style={rectangle, draw, fill=CoreOrange,  draw opacity=0, text=white},
}

begin{tikzpicture}[]
  node[Core, text width=17mm, align=center] {Lorem 
     tikz{ node[Core2,align=center,text width=15mm] {scriptsize Ipsum};}
    };
end{tikzpicture}

end{document}

enter image description here

4 Answers

To long for the comment:

  • Don't nest TikZ pictures!
  • One of possibilities for your node is
documentclass[tikz, border=2mm]{standalone}
usetikzlibrary{backgrounds, 
                fit,
                positioning,
                shapes.multipart}
definecolor{CoreBlue}{HTML}{5b9bd5}
definecolor{CoreOrange}{HTML}{ec7d2d}
begin{document}
tikzset{
    base/.style = {text width=#1, align=center, text=white, outer sep=0pt},
    base/.default = 17mm,
      CB/.style = {base=#1, fill=CoreBlue},
      CO/.style = {base=#1, fill=CoreOrange},
        }
begin{tikzpicture}[]
  node (1a) [CB] {Lorem};
  node (1b) [CO,below=0mm of 1a] {Ipsum}; 
scoped[on background layer]
  node (1)  [CB, fit=(1a) (1b)] {};
end{tikzpicture}
begin{tikzpicture}[]
  node (1a) [CB] {Lorem};
  node (1b) [CO,below=0mm of 1a] {Lorem Ipsum}; 
scoped[on background layer]
  node (1) [CB, fit=(1a) (1b)] {};
end{tikzpicture}
end{document}

enter image description here

or

documentclass[tikz, border=2mm]{standalone}
usetikzlibrary{backgrounds, 
                fit,
                positioning,
                shapes.multipart}
definecolor{CoreBlue}{HTML}{5b9bd5}
definecolor{CoreOrange}{HTML}{ec7d2d}

usepackage{lipsum}
begin{document}
tikzset{
    base/.style = {text width=#1, align=center, text=white, outer sep=0pt},
    base/.default = 21mm,
      CB/.style = {base=#1, fill=CoreBlue},
      CO/.style = {base=#1, fill=CoreOrange, font=scriptsize},
        }
begin{tikzpicture}[]
  node (1a) [CB] {Lorem};
  node (1b) [CO,below=0mm of 1a] {Ipsum}; 
scoped[on background layer]
  node (1)  [CB, fit=(1a) (1b)] {};
end{tikzpicture}
begin{tikzpicture}[]
  node (1a) [CB] {Lorem};
  node (1b) [CO,below=0mm of 1a] {lipsum[1][1]}; 
scoped[on background layer]
  node (1) [CB, fit=(1a) (1b)] {};
end{tikzpicture}
end{document}

enter image description here

Correct answer by Zarko on August 2, 2020

I suggest to draw it directly rather than nested node. If they are repeated in your code, put it in a pic.

documentclass[tikz, border=2mm]{standalone}
usepackage{tikz}
usetikzlibrary{fit, positioning, backgrounds}

begin{document}

definecolor{CoreBlue}{HTML}{5b9bd5}
definecolor{CoreOrange}{HTML}{ec7d2d}

tikzset{
    Core/.style={rectangle, draw, fill=CoreBlue,  draw opacity=0, text=white},
    Core2/.style={rectangle, draw, fill=CoreOrange,  draw opacity=0, text=white},
}

begin{tikzpicture}
  defpadwidth{2mm}
  node[Core2,align=center,text width=15mm] (a) {scriptsize Ipsum};
  path ([xshift=-padwidth]a.west) coordinate (a-l)
    ([xshift=padwidth]a.east) coordinate (a-r);
  node[above=0pt of a] (b) {Lorem};
  pgfkeysgetvalue{/pgf/inner ysep}wrapnodetbsep
  path ([yshift=-wrapnodetbsep]a.south) coordinate (a-b);
  scoped[on background layer] node[fit=(a)(b)(a-l)(a-r)(a-b), inner sep=0pt, fill=CoreBlue] {};
end{tikzpicture}

end{document}

enter image description here

Answered by ZhiyuanLck on August 2, 2020

This is a simple and quick solution/suggestion: use minimum width,minimum height, text width, and align=center. You may make a newcomnand to fit your need. The above Zarko's answer has some advantage over mine.

enter image description here

documentclass[tikz,border=2mm]{standalone}
usepackage{lipsum}
begin{document}
definecolor{CoreBlue}{HTML}{5b9bd5}
definecolor{CoreOrange}{HTML}{ec7d2d}
begin{tikzpicture}
path 
(0,0) node[fill=CoreBlue,minimum width=30mm,minimum height=12mm] (A) {}
(A.center)+(90:3mm) node[text=white] {Lorem}
(A.center)+(-90:2mm) node[fill=CoreOrange,text=white,minimum width=25mm,text width=20mm,align=center,font=scriptsize]{Ipsum};
end{tikzpicture}
end{document}

Answered by Black Mild on August 2, 2020

An alternative solution with a matrix of nodes. A matrix is a node which contains other nodes, therefore, the outer node can be filled with blue color, and inner nodes with its own color, blue for top most nodes and orange for others.

documentclass[tikz, border=2mm]{standalone}
usepackage{tikz}
usetikzlibrary{matrix}

begin{document}

definecolor{CoreBlue}{HTML}{5b9bd5}
definecolor{CoreOrange}{HTML}{ec7d2d}

tikzset{
    Core/.style={matrix of nodes, draw=CoreBlue, fill=CoreBlue, text=white,
        nodes={anchor=center, text width=17mm, align=center, fill=CoreOrange, draw=CoreOrange},
        row 1/.style={nodes={fill=CoreBlue, draw=CoreBlue}}},
}

begin{tikzpicture}[]
  matrix[Core] {Lorem 
     {scriptsize Ipsum}};
end{tikzpicture}

end{document}

enter image description here

Answered by Ignasi on August 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP