1

I am trying to place two tikzpictures beside each other but I'm getting a vertical space above the left one! Any idea how to remove the space?

\begin{tikzpicture}
\draw [solid] (0,0) -- (0,6);
\draw [solid] (0,0) -- (6.6,0);
\draw [solid] (0,5.5) -- (0.5,5.5);
\draw [solid] (0,3.5) -- (3,3.5);
\draw [solid] (0.5,2) -- (0.5,5.5);
\draw [solid] (0.5,2) -- (4,2);
\draw [solid] (3,.7) -- (3,3.5);
\draw [solid] (3,.7) -- (6.5,.7);
\draw [solid] (4,0) -- (4,2);
\draw [solid] (6.5,0) -- (6.5,.7);
% draw vertical lines
\foreach \x in {0,0.5,3,4,6.5}
\draw (\x cm,3pt) -- (\x cm,-3pt);
\foreach \x/\descr in {0/0,0.5/C_1,3/C_2,4/C_3, 6.5/C_4}
\node[font=\scriptsize, text height=1.75ex,
text depth=.5ex] at (\x,-.3) {$\descr$};
\node[text width=3cm, anchor=west, right] at (-0.1,3.7){$p_1$};
\node[text width=3cm, anchor=west, right] at (-0.7,4.5){$w_1$};
\node[text width=3cm, anchor=west, right] at (-0.1,2.7){$w_2$};
\node[text width=3cm, anchor=west, right] at (1.2,2.2){$p_2$};
\node[text width=3cm, anchor=west, right] at (2.3,1.3){$w_3$};
\node[text width=3cm, anchor=west, right] at (3.1,0.9){$p_3$};
\node[text width=3cm, anchor=west, right] at (3.35,0.36){$w_4$};
\node[text width=3cm, anchor=west, right] at (4.8,0.18){$p_4$};
\end{tikzpicture}
\begin{tikzpicture}
\draw[color=red!20, line width=13.6pt] 
(1.25,3.52) -- (1.25,5.49);
\draw[color=yellow!40, line width=85pt] 
(2.5,2.01) -- (2.5,3.49);
\draw[color=green!35, line width=113pt] 
(3,0.71) -- (3,1.99);
\draw[color=blue!25, line width=184.5pt] 
(4.247,0) -- (4.247,0.7);
\draw [solid] (1,0) -- (1,6);
\draw [solid] (1,0) -- (8.3,0);
\draw [solid] (1,5.5) -- (1.5,5.5);
\draw [solid] (1,3.5) -- (4,3.5);
\draw [solid] (1.5,2) -- (1.5,5.5);
\draw [solid] (1,2) -- (5,2);
\draw [solid] (4,.7) -- (4,3.5);
\draw [solid] (1,.7) -- (7.5,.7);
\draw [solid] (5,0) -- (5,2);
\draw [solid] (7.5,0) -- (7.5,.7);
% draw vertical lines
\foreach \x in {1,1.5,4,5,7.5}
\draw (\x cm,3pt) -- (\x cm,-3pt);
\foreach \x/\descr in {1/0,1.5/C_1,4/C_2,5/C_3, 7.5/C_4}
\node[font=\scriptsize, text height=1.75ex,
text depth=.5ex] at (\x,-.3) {$\descr$};
\node[text width=3cm, anchor=west, right] at (0.9,3.7){$p_1$};
\node[text width=3cm, anchor=west, right] at (0.3,4.5){$w_1$};
\node[text width=3cm, anchor=west, right] at (0.3,2.7){$w_2$};
\node[text width=3cm, anchor=west, right] at (2.2,2.2){$p_2$};
\node[text width=3cm, anchor=west, right] at (0.3,1.3){$w_3$};
\node[text width=3cm, anchor=west, right] at (4.1,0.9){$p_3$};
\node[text width=3cm, anchor=west, right] at (0.3,0.36){$w_4$};
\node[text width=3cm, anchor=west, right] at (5.8,0.18){$p_4$};
\end{tikzpicture}

enter image description here

Vincent
  • 20,157
wass
  • 11
  • 1
    Add option baseline=0pt to both tikzpicture environments. – muzimuzhi Z Nov 06 '20 at 22:35
  • This is actually an extreme case of https://tex.stackexchange.com/questions/425756/how-to-ensure-two-standalone-documents-have-same-dimensions/425764#425764 I'd probably just use e.g. \path [draw=black, fill=blue!25] (1,0) rectangle +(184.5pt, 0.7);. Edit: so the thing is not that you have space above the left one, but that the bounding box is too large on the right one, so there is a lot of whitespace in the right tikzpicture, below the axis. – Torbjørn T. Nov 06 '20 at 22:59
  • Use a minipage for each of the two picture – Colo Nov 07 '20 at 10:29

1 Answers1

2

As I was saying in a comment, the problem here is that when you draw a line with line width=X, the bounding box gets extended by X/2 beyond the end of each line. So when you do

\draw[color=blue!25, line width=184.5pt]  (4.247,0) -- (4.247,0.7);

the bounding box of that line extends 92.25pt below y=0. Add \draw (current bounding box.south east) rectangle (current bounding box.north west); to draw the outline of the diagrams, you'll see that the right hand diagram has a lot of whitespace.

Other than using a different technique to draw the colored rectangles, such as \fill[blue!25] (1,0) rectangle +(184.5pt, 0.7);, the simplest fix is probably just to add the overlay option to those lines, e.g.

\draw[color=blue!25, line width=184.5pt, overlay] (4.247,0) -- (4.247,0.7);

With overlay added to a path, that path isn't taken into account when calculating the bounding box.

Torbjørn T.
  • 206,688