0

I am creating the legend of a figure in Tikz, and would like to align two rectangles with text:

  \node(legend) at (1.7,-6.75) {Legend: }; 
    \node[right=0.5cm of legend] (myNode) {};
      \draw[](myNode) rectangle +(0.4,0.2);
    \node[right=1.5cm of legend] (myNode) {};
      \draw[](myNode) rectangle +(0.4,0.2);

enter image description here

I am able to draw the rectangles, but they seem to be aligned with their bottom left corner to the center of the 'legend' node. How do I align the center of the rectangles to the center of the 'legend' node?

1
  • Please (always) provide MWE (Minimal Working Example), a small but complete document beginning with \documentclass and ending with `\end{document} which reproduce your problem.
    – Zarko
    Commented Jul 23, 2020 at 16:32

1 Answer 1

3

Instead of drawing a rectangle after specifying its location using a node, the node itself is a rectangle by default. Just add the key draw to the node and specify the width and height. The reason why your rectangles are not vertically centered is that rectangles start drawing from corner adding two lengths to reach the opposite corner.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[rect/.style={draw,minimum width=0.4cm,minimum height=0.2cm}]
\node (legend) at (1.7,-6.75) {Legend:}; 
\node [right=0.5cm of legend,rect](myNode1){};
\node [right=1.5cm of legend,rect](myNode2){};
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

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