5

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

0
6

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.

4
  • +1 It seems that small mammals cast long shadows and always know how to use tikz better than I! :)
    – user30471
    Jan 9 '20 at 15:32
  • @Andrew chat.stackoverflow.com/transcript/message/48273762#48273762
    – user194703
    Jan 9 '20 at 15:49
  • tikz doesnt recognize fit margins for me. I am using positioning library, and fit itself works ok. Not sure what I'm doing wrong. Mar 31 at 12:38
  • 1
    Oh I see. you have created some kind of new command to do this. Mar 31 at 12:40
5

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}
2
  • +1 but let me recommend using the positioning library.
    – user194703
    Jan 9 '20 at 15:22
  • 1
    You can also include shifts into referenced coordinates in fit and forget creating auxiliar coordinates: \node[draw,fit={([shift={(-1cm,2cm)}]n1.north west) (n2)}] (t31) {};
    – Ignasi
    Jan 10 '20 at 9:05
2

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).

1
  • 2
    This is actually the best and easiest answer.
    – cerebrou
    May 6 at 14:26

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.