# How to calculate and use the bounding box of a figure/tikzpicture/table/array

I am writing some text where I usually have some text and then some figure/tikz picture/table (with tabular or array environment) side by side, in this orden. The command that I use is

\newcommand{\mycommand}[4][15pt]{%
\begin{minipage}{\linewidth-#4-#1}%
#2
\end{minipage}%
\hfill
\begin{minipage}{#4}
#3
\end{minipage}
}


My idea is to have both side by side, optimizing the horizontal space.

My question is, how can I extract automatically the bounding box of the second minipage and insert it in the first minipage? in order to use all de horizontal space. Something like this

\newcommand{\mycommand}[3][15pt]{%
\begin{minipage}{\linewidth-<width of bounding box>-#1}%
#2
\end{minipage}%
\hfill
#3 % extract <width of bounding box>
}


where #3 is a figure, tikzpicture, array, tabular environment, or may be only \includegraphics{}.

Is there a general solution?

• – John Kormylo Oct 12 '17 at 3:24
• Thank you John, but your suggest doesn't help me, because de tikzpicture is on the right to the text, and I am looking for a general solution. Thanks again. – Gitano Oct 12 '17 at 15:02
• As I said, these days I just stick it in a savebox, as was done in ignasi's solution. But you did ask. – John Kormylo Oct 12 '17 at 21:17

You can use a sidebyside tcolorbox. In its documentation (page 122) is explained how to build a new mysidebox command which adjusts left hand side to its contents. I've just changed to adjust right hand side. I show two examples, one using colors and amrgins, and the second one uses the blankest skin to avoid all toclorbox lines and colors.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\newsavebox{\mysavebox}

\DeclareTotalTColorBox{\mysidebox}{ O{} +m +m }{
bicolor,colback=white,colbacklower=yellow!10,
fonttitle=\bfseries,center title,
sidebyside,
code={\sbox{\mysavebox}{#3}},
righthand width=\wd\mysavebox,
#1
}
{#2\tcblower\usebox{\mysavebox}}

\begin{document}
\lipsum[2]

\mysidebox{\lipsum[2]}{%
\begin{tikzpicture}
\path[fill=red!20,draw=red!50!black]
(0,0) node[below]{A} -- (3,1) node[right]{B}
-- (1,4) node[above]{C} -- cycle;
\end{tikzpicture}}

\mysidebox[blankest]{\lipsum[2]}
{\begin{tikzpicture}
\path[fill=red!20,draw=red!50!black]
(0,0) node[below]{A} -- (3,1) node[right]{B}
-- (1,4) node[above]{C} -- cycle;
\end{tikzpicture}}
\end{document}


• Thank you very much! this is exactly what I am looking for. – Gitano Oct 12 '17 at 15:53