4

I want to align the node GNU (with the text "GNU C Library") right with the node Userspace.

I tried this with with the options [anchor=north west, below=1cm of UserSpace] but this doesn't work out.

Here the code

\begin{tikzpicture}
    % Outer Frame
    \draw (0, -4) rectangle (14, 8);

    % Nodes
    \node (UserSpace) at(0.5,6) [rectangle, draw, anchor=south west, minimum width=13cm, minimum height=1cm, align=left,text width=13cm] {\textbf{User Space (Anwendung)}\\z.\,B. Office, Internet, Grafik, Bild-/Audio-/Video-Bearbeitung, etc.};

    \node (GNU)[rectangle, draw, anchor=north west, minimum width=6.5cm, minimum height=1cm, align=left,text width=6.5cm, below=1cm of UserSpace] {\textbf{GNU C Library}\\z.\,B. open, sbrk, exec, fopen, \dots};

    \node (GNU) [rectangle, draw, anchor=north west, minimum width=13cm, minimum height=1cm, align=left,text width=13cm, below=1cm of GNU] {\textbf{Kernel Space (Betriebssystem)}\\u.a. Dateisystem, Prozess- und Speichermanagement,\\ Ein- und  Ausgabe-Management};

\end{tikzpicture}

There is another little problem, I want a space between my nodes and the outer frame. Through the option [text width = ...] I can align my text left, but the length of the rectangles is not anymore the length I intended to have.

enter image description here

Torbjørn T.
  • 206,688
B. Cak
  • 73
  • 1
    Hi, welcome. You need the anchor setting after below=.., as below will also set the anchor, thus overwriting your setting. – Torbjørn T. Mar 10 '18 at 09:56
  • 1
    Regarding the second problem, see options 1 or 2 in https://tex.stackexchange.com/a/124114/ (set align=left instead of text width=..). – Torbjørn T. Mar 10 '18 at 09:59
  • Hey :) It worked! Thanks :) Is there a possibility to align now the Kernel Space 1cm below GNU but centered as UserSpace? – B. Cak Mar 10 '18 at 10:00
  • Regarding to your answer for the align=left - if I do so the linebreak is left aligned but not the whole text to the left of the rectangle :/ – B. Cak Mar 10 '18 at 10:03
  • Remove minimum width. – Torbjørn T. Mar 10 '18 at 10:06
  • Without minimum width the whole text is left aligned but the rectangle doesn't have anymore the length like User Space Is there a way to set the length of the rectangle without affecting the alignment of the text? – B. Cak Mar 10 '18 at 10:12

1 Answers1

3

It's not 100% clear to me what you want, is this the way you like it to be?

output of code

The key point for the text alignment, is to only set the text width. If you want the nodes to have at least a given width, use minimum width. If you want the node width to adapt to the content, while allowing line breaks with \\, use only align=left. But for this case where you want a specific width, setting the text width makes the most sense I think, you don't need align in addition here, as the text is left aligned by default.

I made some other changes as well:

  • I defined a new style box that contains common settings for the nodes. Defining styles is a good way of making code more concise, and reduces the need for repetition, making it easier to change things. For example if you decide you want the node borders to be red with thicker lines, you just edit the definition of box to have draw=red,thick, instead of editing the options of every node separately.

  • You wanted the middle node aligned with the right side of the upper node, so I set below=UserSpace.south east, i.e. the bottom right corner of UserSpace, followed by anchor=north east. Hence, the upper right corner of GNU is directly below the lower right corner of UserSpace.

  • For the last node I set below=of GNU.south -| UserSpace.south. The -| notation is explained in TikZ: What EXACTLY does the the |- notation for arrows do? In short, GNU.south -| UserSpace means the coordinate that has the y-component of GNU.south, and the x-component of UserSpace.

  • Finally, I use the fit library to draw the surrounding frame at the end. Instead of drawing it first, and moving the nodes around, the nodes are placed first, and the frame drawn afterward. fit=(UserSpace)(KernelSpace) means that the node encompasses the UserSpace and KernelSpace nodes.


\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}
\begin{document}

\begin{tikzpicture}[
  box/.style={
   draw,
   minimum height=1cm,
   text width=13cm
  }
]
% Nodes
\node [box] (UserSpace) {%
  \textbf{User Space (Anwendung)}\\
   z.\,B. Office, Internet, Grafik, Bild-/Audio-/Video-Bearbeitung, etc.};

\node [box,
       text width=6.5cm, % this overrwrites the text width of the style
       below=of UserSpace.south east,
       anchor=north east] (GNU)  {%
    \textbf{GNU C Library}\\
    z.\,B. open, sbrk, exec, fopen, \dots};

\node [box,
       below=of GNU.south -| UserSpace] (KernelSpace)  {%
   \textbf{Kernel Space (Betriebssystem)}\\
    u.a. Dateisystem, Prozess- und Speichermanagement,\\
    Ein- und  Ausgabe-Management};

\node [draw,fit=(UserSpace)(KernelSpace),inner sep=5mm] {};
\end{tikzpicture}

\end{document}
Torbjørn T.
  • 206,688