\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{algorithm2e}
\usepackage[most]{tcolorbox}
\definecolor{bg}{RGB}{255,249,227}
\begin{document}
\section{Introduction}
\begin{algorithm}[H]
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
\begin{tcolorbox}[enhanced jigsaw,colback=bg,boxrule=0pt,arc=0pt]
Then compute
\begin{equation}
a= b + c
\end{equation} \end{tcolorbox}
}{
Compute
\begin{equation} d = e + f \end{equation}
}
}
\caption{My Code}
\end{algorithm}
\end{document}
这里我有一段执行文本突出显示的简单代码。
然而,一个不幸的副作用是它将方程编号延伸到了算法框之外(进入边缘)。
我怎样才能防止这种情况发生?
还请注意文本的无意缩进。
答案1
您需要调整框的宽度,幸运的是 algorithm2e 将当前缩进存储在 中\skiptotal
。tcolorbox
还有一些内部边距,在当前设置中为4mm
。使用这些值,我们得到
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[]{algorithm2e}
\usepackage[most]{tcolorbox}
\definecolor{bg}{RGB}{255,249,227}
\begin{document}
\section{Introduction}
\tracingoutput=1
\begin{algorithm}[H]
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
\begin{tcolorbox}[enhanced
jigsaw,colback=bg,boxrule=0pt,arc=0pt,width=\textwidth-\skiptotal-4mm]
Then compute
\begin{equation}
a= b + c
\end{equation} \end{tcolorbox}
}{
Compute
\begin{equation} d = e + f \end{equation}
}
\begin{tcolorbox}[enhanced
jigsaw,colback=bg,boxrule=0pt,arc=0pt,width=\textwidth-\skiptotal-4mm]
Then compute
\begin{equation}
a= b + c
\end{equation} \end{tcolorbox}
}
\begin{tcolorbox}[enhanced
jigsaw,colback=bg,boxrule=0pt,arc=0pt,width=\textwidth-\skiptotal-4mm]
Then compute
\begin{equation}
a= b + c
\end{equation} \end{tcolorbox}
\caption{My Code}
\end{algorithm}
\end{document}