[Tex/LaTex] Auto adjust the width of tcolorbox right-hand side

spacingtcolorbox

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{mathtools}
\usepackage[lmargin=2cm,rmargin=2cm]{geometry}

\begin{document}

\begin{tcblisting}{title=Column Factorization,sidebyside, listing and comment,righthand ratio=0.35,
comment={
$\begin{array}{r|r}
1.500 & 2 \\
750 & 2 \\
375 & 3 \\
125 & 5 \\
25 & 5 \\
5 & 5 \\
1
\end{array} \quad \boxed{1.500 = 2^2 \cdot 3 \cdot 5^3}$
}}
To solve the exercise we use the method of decomposition into column.

$\begin{array}{r|r}
1.500 & 2 \\
750 & 2 \\
375 & 3 \\
125 & 5 \\
25 & 5 \\
5 & 5 \\
1
\end{array} \quad \boxed{1.500 = 2^2 \cdot 3 \cdot 5^3}$
\end{tcblisting}

\begin{tcblisting}{title=Find GCD and LCD,sidebyside, listing and comment,righthand ratio=0.45,
comment={
\begin{align*}
12 = 4 \cdot 3 &= 2^2 \cdot 3 \\
8 &= 2^3
\end{align*}
\[
   \text{GCD}(12, 8) = \boxed4 \qquad \text{LCD}(12, 8) = 8 \cdot 3 = \boxed{24}
\]
}}
The numbers are small so to calculate GCD and LCM it is better to use the mental decomposition.

\begin{align*}
12 = 4 \cdot 3 &= 2^2 \cdot 3 \\
8 &= 2^3
\end{align*}
\[
   \text{GCD}(12, 8) = \boxed4 \qquad \text{LCD}(12, 8) = 8 \cdot 3 = \boxed{24}
\]
\end{tcblisting}

\begin{tcblisting}{title=Tree diagram,sidebyside, listing and comment,righthand ratio=0.25,
comment={
\begin{tikzpicture}
\node[circle,draw,fill=orange!40!white,minimum size=0.7cm] (1) at  (10,10) {+};
\node[circle,draw,fill=orange!40!white,minimum size=0.7cm] (2) at  (9,9) {-};
\node[circle,draw,minimum size=0.7cm] (3) at  (11,9) {2};
\node[circle,draw,minimum size=0.7cm] (4) at  (8,8) {7};
\node[circle,draw,minimum size=0.7cm] (5) at  (10,8) {3};
\draw (1)--(2);
\draw (1)--(3);
\draw (2)--(4);
\draw (2)--(5);
\end{tikzpicture}
}}
The tree diagram shows the order of the operations and the numbers involved.

\begin{tikzpicture}
\node[circle,draw,fill=orange!40!white,minimum size=0.7cm] (1) at  (10,10) {+};
\node[circle,draw,fill=orange!40!white,minimum size=0.7cm] (2) at  (9,9) {-};
\node[circle,draw,minimum size=0.7cm] (3) at  (11,9) {2};
\node[circle,draw,minimum size=0.7cm] (4) at  (8,8) {7};
\node[circle,draw,minimum size=0.7cm] (5) at  (10,8) {3};
\draw (1)--(2);
\draw (1)--(3);
\draw (2)--(4);
\draw (2)--(5);
\end{tikzpicture}
\end{tcblisting}

\end{document}

enter image description here

Since I have to write a lot of tcolorboxes (in particular, tcblisting in order to obtain the Side-by-side source and output effect shown here), it would be nice if there would be a way to automatically adjust the width of the right-hand part, so that I won't have to manually adjust it every time.

I tried with the option hbox as suggested here, but it adjusts the width of both the left and right parts.

I also tried with righthand width=\linewidth but it doesn't work since \linewidth is the totat width of the page.


So the requirements are:

  • Have an environment (tcblisting or similar) which let me display on one side the source code+other text and on the other side the output of the source code only, ie not of the "other text";

  • Output is math and tikz, so environments like equation, align, \[ \], array, $ ... $ and similar have to be supported;

  • The width of the output side (which usually is the right-hand side) have to be automatically adjusted, so that it is not necessary to place righthand ratio=... every time.

Best Answer

You can use tcbsidebyside from the xparse library with sidebyside adapt option.

% \tcbuselibrary{xparse} % in preamble
\tcbsidebyside[sidebyside adapt=right]{<left-hand content>}{<right-hand content>}

enter image description here

\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{xparse}
\begin{document}

\tcbsidebyside[sidebyside adapt=right,]{
This is not a test This is not a test This is not a test This is not a test This is not a test This is not a test This is not a test
}{
\rule{1cm}{1cm}
}

\tcbsidebyside[sidebyside adapt=right,]{
This is not a test This is not a test This is not a test This is not a test This is not a test This is not a test This is not a test
}{
\rule{4cm}{4cm}
}

\end{document}

To get 'side-by-side source and output effect'.

enter image description here

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,xparse,skins}

\newsavebox\mysavebox
\DeclareTotalTColorBox{\mysidebox}{ O{} +m}{
sidebyside,
code={\sbox{\mysavebox}{#2}},righthand width=\wd\mysavebox,#1}{\scantokens{\begin{tcblisting}{listing only,blankest}
#2
\end{tcblisting}}\tcblower\usebox{\mysavebox}}


\begin{document}
\mysidebox[]{%
This is \LaTeX%
}

\mysidebox{%
$$a=b$$
}

\mysidebox[title=The Triangle]{%
\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}