1

So I want to create a macro that I can use to automatically generate a rectangle in TikZ. I would like to specify text (an equation) that should be placed in the middle and automatically generate nodes that I can attach arrows and lines to.

I'll be honest, I don't really have any idea what I'm doing. Here is my attempt so far:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}
​
\newcommand{\tikzTF}[5]{%
    \tikzmath{
        \hcenter = 0.5*#1 + 0.5*#3;
        \vcenter = 0.5*#2 + 0.5*#4;
        function min(\x,\y) {
            if \x > \y then {return \y;} else {return \x;}
        };
        function max(\x,\y) {
            if \x > \y then {return \x;} else {return \y;}
        };
        \l = min(#1,#3);
        \ri = max(#1,#3);
        \t = max(#2,#4);
        \b = min(#2,#4);
    \draw [black] (#1,#2) rectangle (#3,#4);
        \node (#5-c) at ( \hcenter, \vcenter ) {#5};
        \node (#5-tc) at ( \hcenter, \t ) {#5};
        \node (#5-bc) at ( \hcenter, \b ) {#5};
        \node (#5-lc) at ( \ri , \vcenter ) {#5};
        \node (#5-rc) at ( \l , \vcenter ) {#5};
    }
}

\begin{tikzpicture}
    \tikzTF{0}{0}{1.5}{1}{text}
\end{tikzpicture}
\end{document}
3
  • Hi! Please, make your code compilable!
    – user156344
    Feb 14, 2019 at 12:08
  • Hi, I added includes and the document body, but the compiler errors were part of what I was struggling with. I am not at all proficient with coding in latex and I straight-up cannot get this to work. I thought posting my code would be better than nothing since people could see what my general idea was.
    – JPI
    Feb 14, 2019 at 12:32
  • Thank you very much! I can work with this skeleton now. Really appreciated.
    – JPI
    Feb 14, 2019 at 12:54

2 Answers 2

3

Something like this? I use \ifnum instead of \tikzmath

\documentclass[tikz,margin=3mm]{standalone}
\def\whatyouneed (#1,#2) and (#3,#4) node #5; {
    \draw (#1,#2) rectangle (#3,#4);
    \def\maxx{\ifnum#3>#1 #3 \else #1 \fi}
    \def\minx{\ifnum#3>#1 #1 \else #3 \fi}
    \def\maxy{\ifnum#4>#2 #4 \else #2 \fi}
    \def\miny{\ifnum#4>#2 #2 \else #4 \fi}
    \node[fill=white] (#5-c) at ({(#1+#3)/2},{(#2+#4)/2}) {#5};
    \node[fill=white] (#5-t) at ({(#1+#3)/2},\maxy) {#5};
    \node[fill=white] (#5-b) at ({(#1+#3)/2},\miny) {#5};
    \node[fill=white] (#5-l) at (\minx,{(#2+#4)/2}) {#5};
    \node[fill=white] (#5-r) at (\maxx,{(#2+#4)/2}) {#5};
}
\begin{document}
\begin{tikzpicture}
\whatyouneed (0,0) and (3,3) node {Hello};
\draw (Hello-t)--(0,5);
\end{tikzpicture}
\end{document}

enter image description here

1

I think you are trying to do to many things at once. I would suggest starting by looking at LaTeX/Macro on the Wikibook to see how commands are defined.

As for your tikz command, if I understand correctly, this should work as your preamble:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc} % For coordinate calculations
\newcommand\tikzTF[6]
{   % (#1,#2), (#3,#4): coordinates of rectangle
    % #5: Corner coordinate label base text
    % #6: Text placed inside rectangle
    % Store text to be used to generate coordinate labels
    \def\baselabel{#5}
    % Draw rectangle
    \draw [black] (#1,#2) rectangle (#3,#4);
    % Add central node with text #6
    % ($(#1,#2)!.5!(#3,#4)$) means `use the coordinate that is half way from (#1,#2) to (#3,#4)'
    \node at ($(#1,#2)!.5!(#3,#4)$) {#6}; 
    % Now create coordinates we can reference in main picture
    \coordinate (\baselabel1) at (#1,#2);
    \coordinate (\baselabel2) at (#1,#4);
    \coordinate (\baselabel3) at (#3,#4);
    \coordinate (\baselabel4) at (#3,#2);
}

Note that I loaded the calc library to conveniently do a coordinate calculation (you could also do this by hand).

For example,

\tikzTF{0}{0}{3}{2}{figa}{\(y=x^2\)}

creates a rectangle with bottom left corner at (0,0), top right corner at (3,2) and the equation $y=x^2$ at the centre. The fifth parameter, figa, is used as a 'baselabel' so that each corner is now associated with a named coordinate; figa1, figa2, figa3, figa4 (starting from the bottom left hand corner and going clockwise). Within the current tikzpicture, you can use these just like you want any other tikz coordinate. For example:

\draw [red] (figa2) -- (1.5,3) -- (figa3);

Adds a little red roof to our rectangle.

Complete body code and output:

\begin{document}
\begin{tikzpicture}
    \tikzTF{0}{0}{3}{2}{figa}{\(y=x^2\)}
    \draw [red] (figa2) -- (1.5,3) -- (figa3);
\end{tikzpicture}
\end{document}

output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .