7

The following MWE constructs some kind of custom node (combination of two nodes) to get the desired result:

\documentclass{article}

\usepackage{tikz}

\tikzset{
    every node/.style={fill=blue!30,draw,rectangle,inner sep=0pt,outer sep=0pt},
    innerRectangle/.style={minimum height=2cm, minimum width=2cm},
    outerRectangle/.style={fill=red!50,minimum height=3cm, minimum width=2.5cm},
}

\begin{document}
    \begin{tikzpicture}
        \node[outerRectangle] (o) {};
        \node[innerRectangle,anchor=east] (i) at (o.east) {};
    \end{tikzpicture}
\end{document}

The output is:

enter image description here

Question: Is there an easier way, e.g. a single node definition with custom inner seps for one or each side?

Update 1 (triggered by Ignasi's answer):

I did not mention that I would like to insert text into the inner node and that this text should honor the custom inner sep. The following MWE (based on Ignasi's answer, I just added the lipsum package and replaced minimum width by text width) demonstrates the problem:

\documentclass{article}

\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
    every node/.style={
        fill=blue!30,
        draw,
        rectangle,
        inner sep=0pt,outer sep=0pt
    },
    outerRectangle/.style={%
        fill=red!50,
        minimum height=3cm,
        text width=5cm,
    },%
    innerRectangle/.style={%
        path picture={%
            \draw[fill=blue!30] ([yshift=-#1]path picture bounding box.north east)
                -|([xshift=#1]path picture bounding box.west)
               |-([yshift=#1]path picture bounding box.south east)
               --cycle;}},
    innerRectangle/.default=5mm%<- when called without parameter
}

\begin{document}

    \begin{tikzpicture}
        \node[outerRectangle, innerRectangle] (x) {\lipsum[1]};
   \end{tikzpicture}
\end{document}

Screenshot of the box the update produces:

enter image description here

9

You can use path picture option to add an inner filled region to your node:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
    every node/.style={fill=blue!30,draw,rectangle,inner sep=0pt,outer sep=0pt},
    outerRectangle/.style={fill=red!50,minimum height=3cm, minimum width=2.5cm},
    innerRectangle/.style={%
        path picture={%
            \draw[fill=blue!30] ([yshift=-#1]path picture bounding box.north east)
                -|([xshift=#1]path picture bounding box.west)
               |-([yshift=#1]path picture bounding box.south east)
               --cycle;}},
    innerRectangle/.default=3mm
}

\begin{document}

    \begin{tikzpicture}
        \node[outerRectangle] (o) {A};

        \node[outerRectangle,anchor=east, 
        innerRectangle=5mm, right=of o] (o1) {ABC};

        \node[outerRectangle, innerRectangle=2mm, below= of o] (o2) {text};

        \node[outerRectangle,anchor=east, innerRectangle, right=of o2] (o3) {12345};
   \end{tikzpicture}
   \end{document}

enter image description here

Update:

This is an alternative solution which I hope solves the problem of which area to use for text. Previous solution used a node over which an inner border was drawn. In this case an inner node is used for text and an extra path is added as outer border. This outer border is defined with a fit node. Although this outer node has its own name for reference I don't know how to use it for positioning, so the positioning is only done with inner node.

\documentclass{article}

\usepackage{tikz}
\usepackage{lipsum}
\usetikzlibrary{fit, positioning}

\tikzset{
    myborder/.style={%
        append after command={%
            \pgfextra
                \node[fit={([yshift=-#1]\tikzlastnode.south east) 
                ([shift={(-#1,#1)}]\tikzlastnode.north west)}, 
                fill=red!30, draw, inner sep=0pt]  (\tikzlastnode-b) {};\endpgfextra}},
    myborder/.default=3mm
    }
\begin{document}
    \begin{tikzpicture}
    \node[draw, fill=blue!30, text width=4cm,
        myborder] (A) {\lipsum[2]};

    \node[draw, fill=green!30,
        myborder=2mm, right= 0pt of A] (B) {ABC};

    \end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • I just stumbled across a problem: If I replace example text, e.g. 12345 by \lipsum[1] the outer (red) box is filled. I don't wanted the sep-area to get filled - do you have an Idea how to just fill the blue rectangle? – lAtExFaN Jun 21 '17 at 11:11
  • I've updated the answer, although it still show some problems related with positioning this kind of nodes, it solves the problem regarding text. – Ignasi Jun 21 '17 at 13:05

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.