# Diagonal rectangle in TIKZ [duplicate]

Consider the following code in LaTeX:

   \begin{tikzpicture}
\node[circle,fill=red,text=white] (1) at (4,8)  {$R_1$};
\node[circle,fill=red,text=white] (2) at (8,8)  {$R_1$};
\node[circle,fill=blue,text=white] (4) at (4,5)  {$R_2$};
\node[circle,fill=blue,text=white] (3) at (8,5)  {$R_2$};
\draw[-] (1) -- node[above] {7} (2);
\draw[-] (2) -- node[right] {7} (3);
\draw[-] (3) -- node[below] {7} (4);
\draw[-] (4) -- node[left] {7} (1);
\draw[-] (1) -- node[pos=.2,below] {$10$} (3);
\draw[-] (2) -- node[pos=.2,below] {$10$} (4);
\node [draw=blue,  fit= (4) (3)] {};
\node [draw=red, fit= (1) (2)] {};

\end{tikzpicture}


That produce this shape for us:

this image

How it possible to change above code to make a diagonal red enclosing rectangle box as is bellow?

This image

• No, before i checked that but can't help me.
– tstt
Commented Feb 3, 2022 at 12:44
• How it doesn't? Show your code using rotate fit? Commented Feb 3, 2022 at 12:45
• I add this line to my code
– tstt
Commented Feb 3, 2022 at 12:47
• \node [draw, fit=(2)(3), rotate fit=45, inner sep = 12pt] {};
– tstt
Commented Feb 3, 2022 at 12:47
• Can you edit my above code to work correctly?
– tstt
Commented Feb 3, 2022 at 12:47

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}
\node[circle,fill=red,text=white] (1) at (4,8)  {$R_1$};
\node[circle,fill=red,text=white] (2) at (8,8)  {$R_1$};
\node[circle,fill=blue,text=white] (4) at (4,5)  {$R_2$};
\node[circle,fill=blue,text=white] (3) at (8,5)  {$R_2$};
\draw[-] (1) -- node[above] {7} (2);
\draw[-] (2) -- node[right] {7} (3);
\draw[-] (3) -- node[below] {7} (4);
\draw[-] (4) -- node[left] {7} (1);
\draw[-] (1) -- node[pos=.2,below] {$10$} (3);
\draw[-] (2) -- node[pos=.2,below] {$10$} (4);
\node [draw=blue,  fit= (4) (3)] {};
\node [draw=red, fit= (1) (2)] {};
\node [draw=orange, rotate fit=36, fit=(4)(2),  inner sep = 6pt] {};

\end{tikzpicture}
\end{document}


You could also compute te exact angle of the line before drawing the note.

• +1 rotate fit I did not know :)
– JeT
Commented Feb 3, 2022 at 13:34

It is necessary to calculate the angle to use with rotate fit

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}

\begin{document}

\newcommand{\nbox}[4][]{% \nbox[options]{node A}{node B}{fit node's name}
\pgfmathanglebetweenpoints% calculates the angle of vector A to B
{\pgfpointanchor{#2}{center}}{\pgfpointanchor{#3}{center}}
\let\myresult\pgfmathresult
\pgfmathparse{Mod(\myresult,90)}% for the anchors of the fit node to be consistent
\let\myresult\pgfmathresult
\node [draw=green, rotate fit=\myresult, fit = (#2) (#3), #1] (#4) {} ;
}

\begin{tikzpicture}
\node[circle,fill=red,text=white] (1) at (4,8)  {$R_1$};
\node[circle,fill=red,text=white] (2) at (8,8)  {$R_1$};
\node[circle,fill=blue,text=white] (4) at (4,5)  {$R_2$};
\node[circle,fill=blue,text=white] (3) at (8,5)  {$R_2$};
\draw[-] (1) -- node[above] {7} (2);
\draw[-] (2) -- node[right] {7} (3);
\draw[-] (3) -- node[below] {7} (4);
\draw[-] (4) -- node[left] {7} (1);
\draw[-] (1) -- node[pos=.2,below] {$10$} (3);
\draw[-] (2) -- node[pos=.2,below] {$10$} (4);
\node [draw=blue,  fit= (4) (3)] {};
\node [draw=red, fit= (1) (2)] {};
\nbox{2}{4}{5}
\end{tikzpicture}

\end{document}