# Auto adjust the width of tcolorbox right-hand side

\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} 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.

• Could you please explain precisely what you want to achieve. That is, the input is the text and what? The boundary condition is that the right area is a square and that the text fits the rest? And are you aware of the fitting library that ships with tcolorbox? – Schrödinger's cat Oct 16 '19 at 6:05
• I put a square just to have a fixed size content, but usually i have formulas – sound wave Oct 16 '19 at 6:35
• Yes, but please tell us what the input and the expected output are. Do you know the width of the stuff on the right-hand side? Please consider explaining this in the question. – Schrödinger's cat Oct 16 '19 at 6:39
• Oh yes sorry i will do next time, i dont know the width since any tcolorbox has a different equation on the right part. – sound wave Oct 16 '19 at 9:35
• Does nidhin's answer solve your problem, then? If so, please consider accepting it. – Schrödinger's cat Oct 16 '19 at 9:51

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

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

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}
}

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'. \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}

• Thank you, but I have to use tcblisting since I need to obtain this tex.stackexchange.com/a/114582/149290 – sound wave Oct 16 '19 at 9:59
• Thank you for update! Anyway the math environments don't seem to work, for example \mysidebox{$\frac{1}{2}$} or \mysidebox{\begin{align*} x&=2 \\ y&=3 \end{align*}} – sound wave Oct 16 '19 at 19:19
• I added examples and clarification to the question. – sound wave Oct 16 '19 at 19:52

Some naive attempt.

\documentclass{article}
\usepackage[most]{tcolorbox}
\newbox\rightbox

\begin{document}
\tcbset{right
box/.style={/utils/exec={\setbox\rightbox=\hbox{\begin{tabular}{@{}l@{}}
#1
\end{tabular}}},righthand
width=\the\wd\rightbox,comment={\copy\rightbox}}}

\begin{tcblisting}{sidebyside, listing and comment,right box={$E=m\,c^2$}}
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
\end{tcblisting}

\begin{tcblisting}{sidebyside, listing and comment,right box={Errare humanum
est.\\ But I am a cat.}}
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
\end{tcblisting}

\begin{tcblisting}{sidebyside, listing and comment,right box={%
\includegraphics[width=3.14cm]{example-image-duck}}}
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
\end{tcblisting}

\end{document} ADDENDUM: This summarizes what I could offer with regards to your comments below. Unfortunately I do not know how to create a box that may contain an align environment, say, without knowing the width beforehand. (If the width is known, it is trivial, one can use a minipage, say, but this defeats the purpose of this question, or at least my interpretation thereof.) So all I can do is to offer some environment that produces an output that resembles what align gives you.

\documentclass{article}
\usepackage{amsmath}
\usepackage[most]{tcolorbox}
\newbox\rightbox
\newenvironment{myalign}{\displaystyle\begin{aligned}}{\end{aligned}}
\begin{document}
\tcbset{right box/.style={/utils/exec={\setbox\rightbox=\hbox{\begin{tabular}{@{}l@{}}
#1
\end{tabular}}},righthand width=\the\wd\rightbox,comment={\copy\rightbox}}}

\begin{tcblisting}{sidebyside, listing and comment,
right box={\begin{myalign}E&=m\,c^2\\
&=h\,\nu\end{myalign}}}
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
\end{tcblisting}

\begin{tcblisting}{sidebyside, listing and comment,right box={Errare humanum
est.\\ But I am a cat.}}
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
\end{tcblisting}

\begin{tcblisting}{sidebyside, listing and comment,right box={%
\includegraphics[width=3.14cm]{example-image-duck}}}
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
\end{tcblisting}

\end{document} • Thank you very much, I'm testing it in various scenarios. I will let you know and eventually accept the answer. – sound wave Oct 16 '19 at 13:33
• Works good with $...$ environment, but it doesn't work with $...$ or \begin{align} ... \end{align} ones. It says "Bad math environment delimiter." when I use , and multiple errors when I use align. – sound wave Oct 16 '19 at 13:57
• @soundwave Yes, of course. This is why I asked you about the specifications in the comments. I do not have a crystal ball, so I do not know what the requirements are. Please provide a complete list of requirements in the questions such that others can try to address them. – Schrödinger's cat Oct 16 '19 at 15:45
• @soundwave Well, what can I say? You may ask a separate question on whether or not it is possible to define a box that allows you to put align elements in without knowing the width beforehand. I look forward to seeing the answers to this, and would be truly excited to learn a simple way of accomplishing this. (You could define an environment myalign, say, that is nothing but aligned in math mode, perhaps such an environment already exists. By "simple" I mean something simpler than that.) – Schrödinger's cat Oct 17 '19 at 6:35
• @soundwave \displaystyle is just to resemble more the align environment but obviously this won't flush things to the right because the width is not known. (Let me be clear: I do suspect that there is some trick to solve the problem, it is just that I do not know a simple way.) – Schrödinger's cat Oct 17 '19 at 7:00