0

I'd like these two rectangles are positioned side by side, aligned with their upper side, without having to "manually" calculate the position, using the anchor points. I've made sure that rectangles height and width fit the size of the rectangle's content (in this case the text: there's probably a better way to fit the size of the rectangle to its content). It's likely that, as I wrote the code, the anchor point is at the center of the rectangle, i.e. at the coordinate point (0,0) Any idea?

\documentclass{article}
\usepackage[margin=1.5cm,top=1cm,headheight=16pt,headsep=0.1in,heightrounded]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes,positioning,intersections,quotes}
\begin{document}
\begin{tikzpicture}
\node[rectangle,
    draw = lightgray,
    fill = yellow,
    minimum width = .1cm, 
    minimum height = .1cm] (rect_1) at (0,0) 
    {
    \Huge{Rectangle}
    };
\node[rectangle,
    draw = lightgray,
    fill = green,
    minimum width = .1cm, 
    minimum height = .1cm] (rect_2) at (rect_1.south east) 
    {
    \Huge{Rectangle}
    };
\end{tikzpicture}
\end{document}

enter image description here

1
  • Add for example to the second rectangle: right=2cm of rect_1 and remove the at (rect_1.south east).
    – Unknown
    May 16 at 17:20

2 Answers 2

2

For nodes positioning I would use positioning library and define anchors for nodes. For nodes I suggest to use common defined style (without definition of minimum node size) with parameter for colors:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning} % <---

\begin{document}
    \begin{tikzpicture}[
node distance = 7mm,         % <---
   box/.style = {draw=lightgray, fill=#1, % <--- 
                 anchor=north west,
                 align=left, font=\Huge}
                       ]
\node (n1)  [box=yellow]    {Rectangle};  % <---
\node (n2)  [right=of n1.north east,      % <--- had to be before node style
             box=green]   {Rectangle\\ Rectangle};
    \end{tikzpicture}
\end{document}

enter image description here

Some remarks (as suggested by @Unknowm):

  • Suggested solution is independent from used document class, so it will give them same result at article document class too.
  • Advantage of use a standalone in this (and similar) case(s) is that proposed solution is focused to image only. It not need any supporting code for showing result, Due to this it is ideal for prototyping of tikzpictures. Picture is shown on screen in full image size.
  • With defining styles for picture's elements as are nodes, arrows, etc, enables (to some degree) decoupling style from picture codes. By this similar elements have uniform stales, styles not repeated at each element. This lead to (far) more clear and concise code, which is simpler to understand and enable simpler changes of styles.
  • By defining anchor parameter in node style is selected, which node anchor is considered. Note: using positioning syntax after defined node's anchor will overwrite it. Therefore the second node first define positioning regarding to the first node, and than by style of node (box) defined which anchor of node is considered.
  • In MWE is used some experiences with drawing of tikzpictures:
    • let be code short (in it les possible to be lost),
    • code should be structured such, that is simple to identify which part of tikzpicture is defined by it,
    • adding comments to code is helpful. IN above MWE are main changes in comparison to your MWE indicated by % <---.
3
  • This is clearly the better solution (even if understanding the box style with an input parameter might be difficult for a beginner). Maybe you can write a sentence why you switched to the standalone documentclass so that it is clear for the questioner.
    – Unknown
    May 16 at 18:41
  • @Unknown, is now better?
    – Zarko
    May 16 at 19:35
  • More than I wanted. Perfect!
    – Unknown
    May 16 at 19:55
1

Not the best solution but at least one possibility:

\documentclass{article}
\usepackage[margin=1.5cm,top=1cm,headheight=16pt,headsep=0.1in,heightrounded]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes,positioning,intersections,quotes}
\begin{document}
\begin{tikzpicture}
\node[rectangle,
    draw = lightgray,
    fill = yellow,
    font=\Huge] (rect_1)
    {
    Rectangle
    };
\node[rectangle,
    draw = lightgray,
    fill = green,
    xshift = 5cm,
    anchor = north,
    align = left,
    font=\Huge] (rect_2) at (rect_1.north)
    {
    Rectangle\\
    Rectangle
    };
\end{tikzpicture}
\end{document}

I also removed minimum width and minimum height because you do not need it.

2
  • Nice! However, they are not yet top-aligned: nopaste.net/m3uMdZwVSl imgur.com/a/f3QK8OU May 16 at 17:35
  • 1
    @user3713179 I updated my answer. You do not need the tabular environment to write more than one line in a node. Just add align=left|center|right and you can use a simple newline.
    – Unknown
    May 16 at 17:50

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.