TransWikia.com

Tikz fit inner sep seperate values for all 4 directions

TeX - LaTeX Asked by d4rty on June 25, 2021

Assume I have two nodes in tikz and I want to highlight them via tikz fit.
If I want to add a margin I can use inner sep = xpt, however, all 4 sites are increased by the same value. Is it possible to specify for each side a separate value, e.g., inner sep = x1pt x2pt x3pt x4pt?

documentclass[]{standalone}
usepackage{tikz}
usetikzlibrary{fit}

begin{document}
begin{tikzpicture}[every label/.style={text=gray},node distance=2cm]
node[,draw] (n1) {hello};
node[right of=n1,draw] (n2) {hello2};
node[draw,fit=(n1) (n2),inner sep=10pt] (t31) {};

end{tikzpicture}
end{document}

enter image description here

Example of desired outcome:
enter image description here

3 Answers

This comes with a key fit margins that does what I think you want. You can use it as e.g. in

node[draw,fit margins={left=3pt,right=12pt,bottom=3pt,top=8pt},fit=(n1) (n2)] (t31) {};

to obtain

enter image description here

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary{fit,positioning}
tikzset{fit margins/.style={/tikz/afit/.cd,#1,
    /tikz/.cd,
    inner xsep=pgfkeysvalueof{/tikz/afit/left}+pgfkeysvalueof{/tikz/afit/right},
    inner ysep=pgfkeysvalueof{/tikz/afit/top}+pgfkeysvalueof{/tikz/afit/bottom},
    xshift=-pgfkeysvalueof{/tikz/afit/left}+pgfkeysvalueof{/tikz/afit/right},
    yshift=-pgfkeysvalueof{/tikz/afit/bottom}+pgfkeysvalueof{/tikz/afit/top}},
    afit/.cd,left/.initial=2pt,right/.initial=2pt,bottom/.initial=2pt,top/.initial=2pt}
begin{document}
begin{tikzpicture}[]
node[draw] (n1) {hello};
node[right=of n1,draw] (n2) {hello2};
node[draw,fit margins={left=3pt,right=12pt,bottom=3pt,top=8pt},fit=(n1) (n2)] (t31) {};
end{tikzpicture}
end{document}

I also employ the positioning library for easier positioning.

Correct answer by user194703 on June 25, 2021

You can add some points to fit around. For example, to get

enter image description here

use the code:

documentclass[]{standalone}
usepackage{tikz}
usetikzlibrary{fit}

begin{document}
  begin{tikzpicture}[every label/.style={text=gray},node distance=2cm]
    node[draw] (n1) {hello};
    node[right of=n1,draw] (n2) {hello2};
    coordinate[above left of=n1] (n1left);
    node[draw,fit=(n1) (n1left) (n2)] (t31) {};
  end{tikzpicture}
end{document}

Answered by user30471 on June 25, 2021

user194703's answer is good, and works, and I up-voted it, and used the information in it, to write this answer. That said, what I am finding works for me is to use the settings user194703 uses in his/her macro, but use them directly, which settings are yshift, xshift, inner xsep and inner ysep, e.g:

tikzstyle{block} = [draw, inner sep=0.2cm, node distance=0.6cm, fill=yellow!10]
begin{figure}
    centering
    begin{tikzpicture}[auto]
    node [block] (sender) {sender};
    node [block] (receiver) [right=of sender] {receiver};
    node (agent) [
          fit=(sender) (receiver),
          yshift=-0.2cm, inner ysep=0.4cm,inner xsep=0.2cm,
          draw,
          label={[label distance=-1.0cm]190:agent}, rounded corners] {};
    end{tikzpicture}
    caption{Sender receiver}
end{figure}

Result:

enter image description here

(basically, the issue I found with the macro is I have to specify all four margins. the macros seems to me to be incompatible with using inner xsep etc myself, since the macro over-rides them).

Answered by Hugh Perkins on June 25, 2021

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