# Algorithm inside a tcolorbox: how to put a label to the algorithm but the caption as title of tcolorbox?

This question comes after a question about algorithms inside tcolorboxes.

Before the question, is better to explain what I have now. I have a file with the algorithm that is q-learning.tex.

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[longend]{algorithm2e}
\usepackage{textgreek}
\usepackage{amssymb}

\begin{document}
\begin{algorithm}[H]
Algorithm parameters: step size $\alpha \in (0, 1]$\;
Initialize $Q(s, a)$, for all $s \in \mathcal{S}^+, a \in \mathcal{A}(s)$, arbitrarily except that $Q(\mathrm{terminal}, \cdot) = 0$\;

\ForEach{episode}{
Initialize S\;
\ForEach{step of episode}{
Choose $A$ from $S$ using policy derived from $Q$ (e.g. \textepsilon-greedy)\;
Take action $A$, observe $R$, $S'$\;
$Q(S, A) \leftarrow Q(S, A) + \alpha [R + \gamma \max_a Q(S', a) - Q(S, A)]$\;
$S \leftarrow S'$\;
}
}
\end{algorithm}
\end{document}


And I include it in my main file with standalone package.

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[subpreambles,sort]{standalone}
\usepackage{tcolorbox}
\usepackage{algorithm2e}

\begin{document}
\begin{tcolorbox}[fonttitle=\bfseries, title=Q-learning for estimating $\pi \approx \pi^*$]
\input{q-learning}
\end{tcolorbox}
\end{document}


And the cropped result is like this. The reason why I need to load algorithm2e again is obscure to me but it's not what bothers me now. I wish to number the algorithms and then listing them with \listofalgorithms and I want also the possibility to refer to this kind of figures. However, in this implementation, I have no caption and I don't know where to put it. The caption should be the title field of the tcolorbox, i.e. the title should be displayed like "Algorithm 3.2 Q-learning for..." but the label should refer to the algorithm that is inside.

So, to summarize:

• The label should refer to the algorithm and I should have the possibility to make a list of algorithms with \listofalgorithms.
• The caption of the algorithm should be displayed as the title of the tcolorbox

The best change I've made so far is to insert a blank caption and a label inside the algorithm and referring it in the title. It also shows correctly in the list of algorithms. However, I'm not able to remove the annoying "Algorithm 1:" at the bottom.

This is the visual effect:  And this is the code:

q-learning.tex

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[longend]{algorithm2e}
\usepackage{textgreek}
\usepackage{amssymb}

\begin{document}
\begin{algorithm}[H]
Algorithm parameters: step size $\alpha \in (0, 1]$\;
Initialize $Q(s, a)$, for all $s \in \mathcal{S}^+, a \in \mathcal{A}(s)$, arbitrarily except that $Q(\mathrm{terminal}, \cdot) = 0$\;

\ForEach{episode}{
Initialize S\;
\ForEach{step of episode}{
Choose $A$ from $S$ using policy derived from $Q$ (e.g. \textepsilon-greedy)\;
Take action $A$, observe $R$, $S'$\;
$Q(S, A) \leftarrow Q(S, A) + \alpha [R + \gamma \max_a Q(S', a) - Q(S, A)]$\;
$S \leftarrow S'$\;
}
}
\caption[Q-learning for estimating $\pi \approx \pi^*$]{}
\label{alg:q-learning}
\end{algorithm}
\end{document}


main.tex

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[subpreambles,sort]{standalone}
\usepackage{tcolorbox}
\usepackage{algorithm2e}

\begin{document}
\frontmatter
\listofalgorithms
\mainmatter
\chapter{First chapter}
\begin{tcolorbox}[fonttitle=\bfseries, title=Algorithm \ref{alg:q-learning}: Q-learning for estimating $\pi \approx \pi^*$]
\input{q-learning}
\end{tcolorbox}
\end{document}


Of course, the result unitl now is not suitable for my needs.

Found a solution. It's not perfect but do the job.

q-learning.tex

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[longend]{algorithm2e}
\usepackage{textgreek}
\usepackage{amssymb}

\begin{document}
\begin{algorithm}[H]
Algorithm parameters: step size $\alpha \in (0, 1]$\;
Initialize $Q(s, a)$, for all $s \in \mathcal{S}^+, a \in \mathcal{A}(s)$, arbitrarily except that $Q(\mathrm{terminal}, \cdot) = 0$\;

\ForEach{episode}{
Initialize S\;
\ForEach{step of episode}{
Choose $A$ from $S$ using policy derived from $Q$ (e.g. \textepsilon-greedy)\;
Take action $A$, observe $R$, $S'$\;
$Q(S, A) \leftarrow Q(S, A) + \alpha [R + \gamma \max_a Q(S', a) - Q(S, A)]$\;
$S \leftarrow S'$\;
}
}
\caption[Q-learning for estimating $\pi \approx \pi^*$]{}
\label{alg:q-learning}
\end{algorithm}
\end{document}


main.tex

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[subpreambles,sort]{standalone}
\usepackage{tcolorbox}
\usepackage{algorithm2e}
\NoCaptionOfAlgo
\usepackage{hyperref}

\begin{document}
\frontmatter
\listofalgorithms
\mainmatter
\chapter{First chapter}
\begin{tcolorbox}[fonttitle=\bfseries, title=Algorithm~\ref*{alg:q-learning}: Q-learning for estimating $\pi \approx \pi^*$]
\input{q-learning}
\end{tcolorbox}
\end{document}


Basically, it works in this way:

• \NoCaptionOfAlgo doesn’t print Algorithm and its number in the caption. This macro is ONLY active for "algoruled" or "ruled" algorithms (see documentation).
• In the algorithm, I set only the label and the optional parameter of \caption. The first for reference, the second to be displayed in the list of algorithms printed with \listofalgorithms.
• I manually type the caption prefix in the title of tcolorbox together with the number of the algorithm. If the package hyperref is loaded, I suggest using the starred version of \ref to avoid hyperlink.

The solution works fine. The only thing that doesn't satisfy me is the last point: the title, the reference and the caption prefix in the title are all set manually. This means that is prone to errors. If anyone comes with a better, automated solution I will appreciate it.