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

5
  • 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?
    – user194703
    Oct 16, 2019 at 6:05
  • I put a square just to have a fixed size content, but usually i have formulas
    – sound wave
    Oct 16, 2019 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.
    – user194703
    Oct 16, 2019 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, 2019 at 9:35
  • Does nidhin's answer solve your problem, then? If so, please consider accepting it.
    – user194703
    Oct 16, 2019 at 9:51

3 Answers 3

12

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}
3
  • Thank you, but I have to use tcblisting since I need to obtain this tex.stackexchange.com/a/114582/149290
    – sound wave
    Oct 16, 2019 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, 2019 at 19:19
  • I added examples and clarification to the question.
    – sound wave
    Oct 16, 2019 at 19:52
5

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}

enter image description here

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}

enter image description here

13
  • 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, 2019 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, 2019 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.
    – user194703
    Oct 16, 2019 at 15:45
  • 1
    @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.)
    – user194703
    Oct 17, 2019 at 6:35
  • 1
    @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.)
    – user194703
    Oct 17, 2019 at 7:00
0

Here is what I did to make my color box auto adjust to the size of math equations.

\documentclass[10pt,letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox} % for colorboxes
\tcbuselibrary{listings,xparse,skins} % for \tcbsidebyside
\usepackage{lipsum} % dummy text

\begin{document}

%----- flexible split
% cannot adapt to equation environments unless inside $...$
\tcbsidebyside[
    title=Title Goes Here,
    sidebyside adapt=left, % we adapt to the size of the left content
    halign=flush center,
    halign lower=left,
    colback=white,
    colframe=black,
]{
    %----- left content
    %--- works
    $
        \begin{gathered} 
            \begin{aligned} 
                &x^2 - 2 \\ 
                &x^3 + x - \frac{1}{2}
            \end{aligned} \\
            x^2 + 3 \\ 
            x^3 - \frac{4}{2} + 2
        \end{gathered}
    $
    
    %--- doesn't work
    % \begin{gather*}
    %     x^2 - 3 \\ x^3 - \frac{4}{2}
    % \end{gather*}
}{
    %----- right content
    \lipsum[1]
}

\end{document}

Results

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .