0

How to make a label near the inner corners of a rectangle in Tikz? I want to add a letter to each corner of the small rectangle. The letter must still be inside the rectangle.

\documentclass{beamer}
\usepackage{tikz}

\begin{document} \begin{frame} \begin{center} \begin{tikzpicture} \fill [orange] (-2,-1) rectangle (2,2); \draw (-2, -1) rectangle (2,2); \fill [white] (0,0) rectangle (1,1); \draw (0,0) rectangle (1,1); \draw[->, thick] (0.5, 0.5) -- (2.3, 1); \fill [orange] (2.5,0.5) rectangle (1+2.5,1.5); \draw (2.5,0.5) rectangle (1+2.5,1.5); \end{tikzpicture} \end{center} \end{frame} \end{document}

Qrrbrbirlbel
  • 119,821

3 Answers3

2

I suggest a more versatile way unlike Miyase's solution that uses absolute coordinates.

You can insert node path operator after rectangle and by adjusting anchor option make nodes inside.

This way nodes do not depend on any absolute coordinates and by changing rectangle dimensions, labels will stay at the right place.

\documentclass{beamer}
\usepackage{tikz}

\begin{document} \begin{frame} \begin{center} \begin{tikzpicture} \fill [orange] (-2,-1) rectangle (2,2); \draw (-2, -1) rectangle (2,2); \fill [white] (0,0) rectangle (1,1); \draw (0,0) rectangle (1,1); \draw[->, thick] (0.5, 0.5) -- (2.3, 1);

   \draw[font=\tiny, outer sep=5pt, fill=orange] (2.5,0.5) rectangle node[anchor=north west]{1} node[anchor=north east]{2} node[anchor=south west]{3} node[anchor=south east]{4} (1+2.5,1.5);

   \draw (2.5,0.5) rectangle (1+2.5,1.5);
  \end{tikzpicture}
  \end{center}

\end{frame} \end{document}

enter image description here


Edit:

As Qrrbrbirlbel pointed out, I was wrong about automatic placement for arbitrary rectangle. The previous solution is still valid as long as you are ok with tweaking around outer sep option.

However, in sake of versatility that I was after, here's slightly different approach:

Instead of using rectangle path operator, you can use

(2.5,0.5) |- (2+2.5,1.5) |- cycle

that draw the same rectangle as

(2.5,0.5) rectangle (2+2.5,1.5)

But the advantage of that approach is that you can now specify nodes explicitly before and after |- operator and the nodes will already placed in corners.

enter image description here

\draw[font=\tiny, fill=orange] (2.5,0.5) node{1} |- node{2} (2+2.5,1.5) node{3} |- node{4} cycle;

Now it's only matter of aligning labels so that they will appear inside the rectangle. You can do it via either anchor option as in the previous solution or via left, right, above, below combination that are equivalent to anchor ones. And the last this is to add inner sep to add some padding

\documentclass{beamer}
\usepackage{tikz}

\begin{document} \begin{frame} \begin{center} \begin{tikzpicture} \fill [orange] (-2,-1) rectangle (2,2); \draw (-2, -1) rectangle (2,2); \fill [white] (0,0) rectangle (1,1); \draw (0,0) rectangle (1,1); \draw[->, thick] (0.5, 0.5) -- (2.3, 1);

   \draw[font=\tiny, inner sep=2pt, fill=orange] (2.5,0.5) node[above right]{1} |- node[below right]{2} (2+2.5,1.5) node[below left]{3} |- node[above left]{4} cycle;

  \end{tikzpicture}
  \end{center}

\end{frame} \end{document}

enter image description here

antshar
  • 4,238
  • 9
  • 30
  • Yes, that's what I had in mind when I said "There are probably more elegants ways to do it" but I still need to develop better habits with TikZ. Thanks. – Miyase Jul 23 '22 at 11:24
  • “nodes do not depend on any absolute coordinates and by changing rectangle dimensions, labels will stay at the right place” → No, they will always be placed 5pt away from the center of the rectangle. – Qrrbrbirlbel Jul 23 '22 at 12:37
  • @Qrrbrbirlbel thank you for the remark, I added alternative approach. – antshar Jul 24 '22 at 11:26
1

You can combine

\fill [orange] (-2, -1) rectangle (2,2);
\draw          (-2, -1) rectangle (2,2);

to

\filldraw[fill=orange] (-2, -1) rectangle (2,2);

which saves you specifying the coordinates twice.

If you want to cut out a rectangle inside a rectangle you can do this with the even odd rule in one path:

\filldraw[fill=orange] (-2, -1) rectangle (2,2) (0,0) rectangle (1,1);

For placing the letters inside the corners, I'll provide you with a custom to path named rectangle mark corners inside:

\tikzset{
  rectangle mark corners inside/.style n args={4}{%
    to path={
      rectangle (\tikztotarget)
      node[anchor=north east, corner label/.try] at (\tikztotarget) {#1}
      node[anchor=north west, corner label/.try] at (\tikztostart|-\tikztotarget) {#2}
      node[anchor=south west, corner label/.try] at (\tikztostart) {#3}
      node[anchor=south east, corner label/.try] at (\tikztostart-|\tikztotarget) {#4}}}}

This basically still does (<start>) rectangle (<target>) but it uses the coordinates given by you to place the nodes at the corners.

This still allows you to use the even odd rule without having to give the coordinates twice:

\filldraw [fill=orange, even odd rule]
  (-2, -1)    rectangle                           (2, 2)
   (0,  0) to[rectangle mark corners inside=ABCD] (1, 1);

I've added the style corner label to the corner nodes so that you can set the style of those nodes in a central place.

Code

\documentclass[tikz,convert]{standalone}
\tikzset{
  rectangle mark corners inside/.style n args={4}{%
    to path={
      rectangle (\tikztotarget)
      node[anchor=north east, corner label/.try] at (\tikztotarget) {#1}
      node[anchor=north west, corner label/.try] at (\tikztostart|-\tikztotarget) {#2}
      node[anchor=south west, corner label/.try] at (\tikztostart) {#3}
      node[anchor=south east, corner label/.try] at (\tikztostart-|\tikztotarget) {#4}}}}
\begin{document}
\begin{tikzpicture}[corner label/.style={font=\tiny}]
  \filldraw [fill=orange, even odd rule]
    (-2, -1)    rectangle                           (2, 2)
     (0,  0) to[rectangle mark corners inside=ABCD] (1, 1);
  \draw[->, thick] (0.5, 0.5) -- (2.3, 1);
  \filldraw [fill=orange!50]
    (2.5, 0.5) to[rectangle mark corners inside={1}{foo}{bar}{4}] (1+2.5,1.5);
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
0

There are probably more elegants ways to do it, but simple \node commands should do the trick:

\documentclass{beamer}

\usepackage{tikz}

\begin{document} \begin{frame} \begin{center} \begin{tikzpicture} \fill [orange] (-2,-1) rectangle (2,2); \draw (-2, -1) rectangle (2,2); \fill [white] (0,0) rectangle (1,1); % Position nodes in the corners \node at (0,0)[above right]{\small $A$}; \node at (1,0)[above left]{\small $B$}; \node at (1,1)[below left]{\small $C$}; \node at (0,1)[below right]{\small $D$}; % \draw (0,0) rectangle (1,1); \draw[->, thick] (0.5, 0.5) -- (2.3, 1); \fill [orange] (2.5,0.5) rectangle (1+2.5,1.5); \draw (2.5,0.5) rectangle (1+2.5,1.5); \end{tikzpicture} \end{center} \end{frame} \end{document}

result

Miyase
  • 2,544
  • 2
  • 12
  • 26