# TikZ rectangle split horizontal extra space

The following code gives extra space both on the left under the graphic and on the right under the text. Is there a way to make the split rectangles tight against the larger text or graphic?

% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = XeLaTeX

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{multicol}

\usetikzlibrary{shapes.multipart,positioning}

\newsavebox{\testbox}

\begin{document}

\begin{lrbox}{\testbox}
\begin{minipage}{3in}
\begin{multicols}{2}
This is a lot of bla bla bla to see what is happening. And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more.
\end{multicols}
And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
\end{minipage}
\end{lrbox}

\tikz{%
\node[%
rectangle split,
rectangle split parts=2,
rectangle split horizontal,
rectangle split part align={top},
draw,
line width=3pt
](test){%
\includegraphics[width=2in]{example-image-a}
\nodepart{two} \usebox{\testbox}.
};
\node[above=12pt of test.south,fill=white]{%
\bfseries\LARGE\color{red} $\Longleftarrow$ EXTRA SPACE? $\Longrightarrow$};
}%

\end{document}

This has to do with the way you include the graphics. Here is one out of many ways to fix it: put it in $\vcenter{\hbox{...}}$, which is a standard trick to vertically center objects (in equations). My choice is not special, nor sophisticated, but seems to work. As for the question: why is this necessary here? I do not have a compelling answer except maybe that I am not surprised to have to care about the vertical alignment.

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{multicol}

\usetikzlibrary{shapes.multipart,positioning}

\newsavebox{\testbox}

\begin{document}

\begin{lrbox}{\testbox}
\begin{minipage}{3in}
\begin{multicols}{2}
This is a lot of bla bla bla to see what is happening. And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more.
\end{multicols}
And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
\end{minipage}
\end{lrbox}

\tikz{%
\node[%
rectangle split,
rectangle split parts=2,
rectangle split horizontal,
rectangle split part align={top},
draw,
line width=3pt
](test){%
$\vcenter{\hbox{\includegraphics[width=2in]{example-image-a}}}$
\nodepart{two} \usebox{\testbox}.
};
}%

\end{document}

• Hmmm. Thanks, but this begs the question: Why are two levels of extra boxing necessary to make this happen? I tried \hbox and \mbox but would wouldn't have thought of \vcenter in a gajillion years. Some interaction between \includegraphics and rectangle split horizontal? I don't think I've ever seen \includegraphics behave this way on its own. Thanks for the help! – sgmoye Sep 25 '18 at 18:37
• @sgmoye Sorry, I am in meeting after meeting. \vcenter was what I immediately thought of using here, just compare $x\includegraphics[width=2in]{example-image-a}$ to $x\vcenter{\hbox{\includegraphics[width=2in]{example-image-a}}}$. The latter is the standard trick of vertically centering things in an equation. So my thought was to try it and it worked. Need to rush to the next meeting, will explain more later. – marmot Sep 25 '18 at 19:32
• I would really appreciate knowing why a math mode TeX primitive is necessary to make this work. To all appearances, this indicates some non-obvious (to me, anyway) manipulation of boxes by TikZ. – sgmoye Sep 26 '18 at 11:27
• @sgmoye I do not know the details what TikZ dos precisely in these multipart nodes. I guess both of us looked at the code but at least I gave up due to the complexity. Nonetheless, the \vcenter{\hbox{...}} thingy is something I do rather often, e.g. to center a Feynman diagram or some other graphics in an equation. Out of the top of my head, I wouldn't know an alternative (other than using some baseline option if the diagram is drawn with TikZ), so for me using the threshold using this primitive is very low since it was always fine. – marmot Sep 26 '18 at 11:56
• Interesting. I just discovered that this works: \parbox{2in}{\includegraphics[width=2in]{example-image-a}}.. – sgmoye Sep 26 '18 at 13:29

an alternative solution is use of the adjustbox package:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,
shapes.multipart}
\usepackage{multicol}
\usepackage{ragged2e}

\newsavebox{\testbox}

\begin{document}

\begin{lrbox}{\testbox}
\begin{minipage}{3in}\RaggedRight
\begin{multicols}{2}
This is a lot of bla bla bla to see what is happening. And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more.
\end{multicols}
And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
\end{minipage}
\end{lrbox}

\tikz{%
\node[%
rectangle split,
rectangle split parts=2,
rectangle split horizontal,
rectangle split part align={top},
draw,
line width=3pt
] (test)
{\includegraphics[width=2in,valign=c]{example-image-a} % with "valign=c" is image baseline moved to its vertical center
\nodepart{two}  \usebox{\testbox}
};
}

\end{document}

• Thank you -- that also didn't occur to me at all. But why is all this necessary? What is the underlying mechanism that causes the extra space? I'd like to be aware of the mechanics so that I can avoid a similar conundrum in the future. Thanks again. I've learned something and that's always nice... – sgmoye Sep 25 '18 at 19:04
• all is about boxes. and how they are aligned to each other. but i might be wrong. you should read TeXbook ... (instead thanks, you can up-vote both answers) – Zarko Sep 25 '18 at 19:14
• Yes, I suppose that we could all, no doubt, benefit from a re-(re-re-re-re-)reading of the TeXbook, but the problem, it seems to me, lies with TikZ and what it is doing with boxes -- I looked at the code but came away none the wiser. Marmot's interesting solution requires the use of a math mode TeX primitive. Altogether non-obvious, at least to me. Some explanation of that would also be useful. – sgmoye Sep 26 '18 at 13:06
• @sgmoye, both solution do the same: move image baseline to the center. for this they are only used different tools. for better (or more correct) explanation you should ask some latex guru (what i'm not). – Zarko Sep 26 '18 at 13:14

In case you could accept text box and not a TikZ node, tcolorbox offers a \tcbsidebyside box where you can decide which part will adapt to its size while the other uses the available space (\textwidth or box width).

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{multicol}
\usepackage{lmodern}

\begin{document}
\tcbsidebyside[notitle,
colback=white,
enhanced,
segmentation style=solid,
sharp corners]
{%
\includegraphics[width=3cm]{example-image-a}
}{%
\begin{multicols}{2}
This is a lot of bla bla bla to see what is happening. And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more.
\end{multicols}
And this is more text. and a lot more. And this is more text. and a lot more. And this is more text. and a lot more
}

\tcbsidebyside[notitle,