2

I need to define a latex macro that takes two points p1, p2 in a tikz picture, and draws a box the diagonal of which is given by p1 -- ({max(hor component of p1, hor component of p2)}, ver component of p2).

I'm not getting the syntax right for the various components of a point. This is what I have:

\makeatletter
\newcommand{\HOR}[1]{\pgf@x{#1}}
\newcommand{\VER}[1]{\pgf@y{#1}}
\makeatother

\newcommand{\TIKZBOX}[2]{
  \draw (#1) rectangle ({max(\HOR{#1}, \HOR{#2})}, \VER{#2});
}

2 Answers 2

2

One possible solution is to load the package tkz-euclide, which is based on TikZ. It had various commands to define a new shape, one of them being the square. I wrapped it in a \newcommand so that you don't have to write everything again. You only need to provide the 2 coordinates, it will take care of the rest.

Output

enter image description here

Code

\documentclass[tikz, margin=10pt]{standalone}
\usepackage{tkz-euclide}

\newcommand\Squarify[3][]{%
    \tkzDefPoint(#2){A} \tkzDefPoint(#3){B}
    \tkzDefSquare(A,B)
    \tkzDrawPolygon[#1](A,B,tkzFirstPointResult,%
    tkzSecondPointResult)
}

\pgfmathsetseed{1234}% ensures same result in output for this example

\begin{document}
\begin{tikzpicture}

    \Squarify{0,0}{1,0}% reference without color

    \foreach \colors in {red,blue,yellow,orange,green,violet}{%
        \pgfmathsetmacro{\Xa}{random(3)+random(2)}
        \pgfmathsetmacro{\Ya}{random(3)+random(2)}
        \pgfmathsetmacro{\Xb}{random(3)+random(2)}
        \pgfmathsetmacro{\Yb}{random(3)+random(2)}
        \Squarify[color=\colors]{\Xa,\Ya}{\Xb,\Yb}
    }
\end{tikzpicture}
\end{document}
1

This is another solution only using TikZ without loading other packages.

Output

enter image description here

Code

\documentclass[tikz, margin=10pt]{standalone}

\newcommand\Squarify[3][]{%
    \coordinate (A) at (#2);
        \path (#2);
        \pgfgetlastxy{\tempAx}{\tempAy}
    \coordinate (B) at (#3);
        \path (#3);
        \pgfgetlastxy{\tempBx}{\tempBy}
    \pgfmathtruncatemacro\XCoordA{\tempAx*1pt/1cm}
    \pgfmathtruncatemacro\YCoordA{\tempAy*1pt/1cm}
    \pgfmathtruncatemacro\XCoordB{\tempBx*1pt/1cm}
    \pgfmathtruncatemacro\YCoordB{\tempBy*1pt/1cm}
    \pgfmathtruncatemacro\XCoordC{\XCoordB-(\YCoordB-\YCoordA)}
    \pgfmathtruncatemacro\YCoordC{\YCoordB+(\XCoordB-\XCoordA)}
    \pgfmathtruncatemacro\XCoordD{\XCoordA-(\YCoordB-\YCoordA)}
    \pgfmathtruncatemacro\YCoordD{\YCoordA+(\XCoordB-\XCoordA)}
    \draw[#1] (A) -- (B) -- 
        (\XCoordC,\YCoordC) coordinate (C) -- 
        (\XCoordD,\YCoordD) coordinate (D) -- cycle;
%   
}

\pgfmathsetseed{1234} % again to ensure same colored squares

\begin{document}
\begin{tikzpicture}
    \Squarify{0,0}{1,0}
    \foreach \colors in {red,blue,yellow,orange,green,violet}{%
        \pgfmathsetmacro{\Xa}{random(3)+random(2)}
        \pgfmathsetmacro{\Ya}{random(3)+random(2)}
        \pgfmathsetmacro{\Xb}{random(3)+random(2)}
        \pgfmathsetmacro{\Yb}{random(3)+random(2)}
        \Squarify[color=\colors]{\Xa,\Ya}{\Xb,\Yb}
    }
\end{tikzpicture}
\end{document}
2
  • Very good for both answers.
    – Sebastiano
    Oct 13, 2020 at 21:09
  • 1
    @Sebastiano thanks!
    – Alenanno
    Oct 13, 2020 at 22:24

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.