2

I would like to know if it is possible to align the bottom part of a node with a specific point. I already have a code, but it has two problems: first the alignement is not perfect, I don't know why, and also the code is not very natural to write (maybe a library already do the math for me?).

Goal: align the C box like that, without the error (and possibly with a nice code):

enter image description here

MWE:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}[
  box/.style={
    rounded corners,
    minimum width=2cm,
    align=center,
    fill=red!50!white,
  },
  ]%
  \node[box] (A) {$A$};
  \node[box, below=of A] (B) {$B$\\multiline};
  \path let \p1=($(A.north)-(B.south)$) in node[box, right=of A.north east,anchor=north west, minimum height={\y1}, inner sep=0pt] (C) {$C$};
\end{tikzpicture}%
\end{document}
1
  • I got an error because you have a non-ASCII character in your code, but otherwise the approach is fine I think. – user194703 Apr 14 '20 at 19:54
4

Edit 1:

If you are really annoyed with the 0.01411cm of extra space that is generated, you can use the following to draw the box C without borders:

\node[box,fit=(A)(B), right=of A.north east,anchor=north west,inner sep=-\pgflinewidth/2,outer sep=\pgflinewidth/2, draw=none] (C) {$C$};

Or the following with the default border (line width=0.4pt) thickness:

\node[box,fit=(A)(B), right=of A.north east,anchor=north west,inner sep=-\pgflinewidth,outer sep=\pgflinewidth, draw=blue] (C) {$C$};

Note: rounding will probably occur as we are talking about very tiny numbers and pdf viewers uses whole pixels so you might feel it is not perfect to the precision of the pixel.

Output updated

Here is a MWE:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}[
  box/.style={
    rounded corners,
    minimum width=2cm,
    align=center,
    fill=red!50!white,
  },
  ]%
  \node[box] (A) {$A$};
  \node[box, below=of A] (B) {$B$\\multiline};
  \node[box,fit=(A)(B), right=of A.north east,anchor=north west,inner sep=-\pgflinewidth/2,outer sep=\pgflinewidth/2, draw=none] (C) {$C$};
  \node[box,fit=(A)(B), right=of C.north east,anchor=north west,inner sep=-\pgflinewidth,outer sep=\pgflinewidth, draw=blue] (D) {With border};
\end{tikzpicture}%
\end{document}

Original:

Use the fit option as you have imported it.

Output

\documentclass[tikz, border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}[
  box/.style={
    rounded corners,
    minimum width=2cm,
    align=center,
    fill=red!50!white,
  },
  ]%
  \node[box] (A) {$A$};
  \node[box, below=of A] (B) {$B$\\multiline};
  \node[box,fit=(A)(B), right=of A.north east,anchor=north west, inner sep=0pt] (C) {$C$};
\end{tikzpicture}%
\end{document}
3
  • Oh, it's possible to fit that way?! Amazing, thanks! But there is still this strange thin space below, that appears if you remove the border of the image, any idea why? – tobiasBora Apr 14 '20 at 19:57
  • @tobiasBora, I have updated the answer, please, have a look at it. – M. Al Jumaily Apr 14 '20 at 22:31
  • Great thanks a lot! – tobiasBora Apr 15 '20 at 6:38

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.