4

I want to decorate a rectangular node with little black boxes in the corners: Solution with Gimp :D

So far, I was able to put a black box in the lower left corner using the decorations.markings TikZ library:

\tikzset{
  blackcorner/.style={ draw,
    decoration={ markings,
      mark=at position 0 with {
        \draw[very thick](0pt,1pt) -- (1pt,1pt) -- (1pt,0pt);
      }
    },
    postaction={decorate}
  }
}
\tikz{\node[blackcorner]{test};}

Solution with markings

However, I cannot get this to draw black boxes in the other corners. Is it possible to do this with decorations or otherwise? (Also, the black corners are very near to the text, how can I increase the width of the node without increasing the height?)

  • 2
    I do not know about the decoration, but for the horizontal padding you can try inner xsep=10pt – Rmano May 6 '16 at 15:44
6

Since the locations are well-defined, you can get away with path picture of a node.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[corner stone/.style={draw,
  path picture={
    \foreach \x/\xi in {north/-1,south/1}{
      \foreach \y/\yi in {west/45,east/135}{
        \fill (path picture bounding box.\x\space\y) rectangle ++(\xi*\yi:#1);
      }
    }
  }
},
corner stone/.default=2mm
]
\node[corner stone] {My node};
\node[corner stone=3mm] at (0,2) {My node};
\end{tikzpicture}
\end{document}

enter image description here

3

Here's a version which uses append after command in the definition of blackcorner to draw the 4 corners.

The size can be configured using size=<dimension> in an optional argument to blackcorner which may also include other options for the corner nodes, if desired. Despite the name, one of the examples below shows how to create blue blackcorners.

To increase the margin between text and corners, simply add inner sep=<dimension> to the options for the node. Note: not as options for blackcorner - you don't want to change it there. You want to increase the margin for the main node, so the border is further from the text.

The upshot of all this is that

\tikz{%
  \node [blackcorner] {test};
  \node [blackcorner={size=2.5pt}, inner sep=5pt, blue] at (0,1) {test};
  \node [blackcorner={size=2.5pt, blue}, inner sep=5pt, blue] at (0,2) {test};
}

produces three nodes: the bottom node has the default spacing and corner size, all in black; the middle node is blue with larger corners and margin, but the default black is still used for the corners; finally, the top node also changes the corners' colour to blue.

3 nodes

Complete code:

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{calc}
\begin{document}
\tikzset{%
  blackcorner/.style={%
    draw,
    append after command={%
      let \p1=(\tikzlastnode.south west), \p2=(\tikzlastnode.north west), \p3=(\tikzlastnode.north east), \p4=(\tikzlastnode.south east) in (\p1) node [black corner/.cd, #1, corner, anchor=south west] {} (\p2) node [black corner/.cd, #1, corner, anchor=north west] {} (\p3) node [black corner/.cd, #1, corner, anchor=north east] {} (\p4) node [black corner/.cd, #1, corner, anchor=south east] {}
    },
  },
  black corner/.search also={/tikz},
  black corner/.cd,
  corner/.style={%
    inner sep=0pt, draw, fill, minimum size=\cornersize,
  },
  size/.store in=\cornersize,
  size=1.8pt,
}
\tikz{%
  \node [blackcorner] {test};
  \node [blackcorner={size=2.5pt}, inner sep=5pt, blue] at (0,1) {test};
  \node [blackcorner={size=2.5pt, blue}, inner sep=5pt, blue] at (0,2) {test};
}
\end{document}

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.