# Tikz fit inner sep seperate values for all 4 directions

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}


Example of desired outcome:

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

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

• +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
• – 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
• Oh I see. you have created some kind of new command to do this. Mar 31 at 12:40

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

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}

• +1 but let me recommend using the positioning library.
– user194703
Jan 9 '20 at 15:22
• 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) {}; Jan 10 '20 at 9:05

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 (agent) [

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