algorithm2e und Farbe

algorithm2e und Farbe

Ich versuche, den Hintergrund der Beschriftung im Algorithmus einzufärben (mithilfe von algorithm2e).

Ich habe den ursprünglichen Befehl wie folgt direkt in der .sty-Datei geändert.

Wie Sie jedoch auf dem Bild sehen können, ist das Ergebnis nicht angenehm. Ich möchte, dass die ganze „Zeile“ grau ist (ich habe die Zeile in Klammern gesetzt, da es sich nicht um eine Tabelle handelt).

\newcommand{\algocf@makecaption@ruled}[2]{%
  \global\sbox\algocf@capbox{\hskip\AlCapHSkip![enter image description here][2]
    \parbox[t]{\hsize}{\colorbox{gray!50}{\algocf@captiontext{#1}{#2}}}}
}%

Das Original :

\newcommand{\algocf@makecaption@ruled}[2]{%
  \global\sbox\algocf@capbox{\hskip\AlCapHSkip% .5\algomargin%
    \parbox[t]{\hsize}{\algocf@captiontext{#1}{#2}}}
}%

Bildbeschreibung hier eingeben

Der vollständige Code:

\documentclass{article} 
\usepackage{etex} 
\usepackage{easybmat} 
\usepackage[T1]{fontenc} 
\usepackage{lmodern} 
\usepackage{xcolor}
\usepackage{algorithm2e}
\RestyleAlgo{algoruled}

\begin{document} 

  \SetKw{KwFrom}{from} 


\newenvironment{algo}{
\begin{algorithm}[H]
\DontPrintSemicolon \SetAlgoVlined}
{\end{algorithm}}



\begin{algo}\label{algotrigsup}
\caption{Caption of the algo}
\For{$k$ \KwFrom $n-1$ \KwTo $1$}{
$x_k=b_k$\;
\For{$j$ \KwFrom $k+1$ \KwTo $n$}{
$x_k=x_k -a_{kj} x_j$\;
}
$x_k=\displaystyle \frac{x_k}{a_{kk}}$
}
\end{algo}


\end{document}

Antwort1

Hier ist eine Möglichkeit (das \parboxmuss innerhalb von liegen \colorboxund einige vertikale und horizontale Abstandsanpassungen sind erforderlich):

\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{xcolor}

\colorlet{AlgCaptionColor}{gray!30}

\makeatletter
\renewcommand{\algocf@makecaption@ruled}[2]{%
  \global\sbox\algocf@capbox{\colorbox{AlgCaptionColor}{\hskip\AlCapHSkip% .5\algomargin%
    \parbox[t]{\hsize}{\algocf@captiontext{\strut#1}{\strut#2\strut}}\hskip.6\algomargin}}% then caption is not centered
}%
\setlength{\interspacetitleruled}{0pt}%
\makeatother

\begin{document}

\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms and some additional text just for the exmaple}
\end{algorithm}

\end{document}

Bildbeschreibung hier eingeben

verwandte Informationen