\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}
由于我必须编写大量的 tcolorboxes(特别是 tcblisting,以便获得并排源和输出效果如图这里),如果有办法可以自动调整右侧部分的宽度就好了,这样我就不用每次都手动调整了。
我尝试过这个选项hbox
我尝试了建议的这里,但它同时调整了左右部分的宽度。
我也尝试过,但是由于页面的总宽度,righthand width=\linewidth
它不起作用。\linewidth
因此要求是:
有一个环境(tcblisting 或类似的),让我在一侧显示源代码+其他文本,在另一侧仅显示源代码的输出,即而不是“其他文本”;
输出是数学和 tikz,因此必须支持
equation
、、、、等类似align
的环境;\[ \]
array
$ ... $
输出侧(通常是右侧)的宽度必须自动调整,这样就不需要
righthand ratio=...
每次都放置。
答案1
您可以tcbsidebyside
从xparse
库中使用sidebyside adapt
选项。
% \tcbuselibrary{xparse} % in preamble
\tcbsidebyside[sidebyside adapt=right]{<left-hand content>}{<right-hand content>}
\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}
获得“并排源和输出效果”。
\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}
答案2
一些天真的尝试。
\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}
附录:这总结了我可以针对您下面的评论提供的内容。不幸的是,我不知道如何创建一个可能包含align
环境的框,例如,事先不知道宽度。(如果知道宽度,这很简单,可以使用迷你页面,但这违背了这个问题的目的,或者至少违背了我的解释。)所以我所能做的就是提供一些环境,产生类似于align
您给出的输出。
\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}
答案3
以下是我所做的,使我的颜色框自动调整到数学方程式的大小。
\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}