1

I wanted to know how to go about changing the algorithm caption label to blue and bold italics. I have so far been able to change the caption name to blue, but I have not been able to change the algorithm caption label to bold and italicized. Here is the code that I have so far:

\documentclass{article}
\usepackage{amsmath, amsfonts, amssymb, amsthm, bm}
\usepackage{commath}
\usepackage{xcolor}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\SetAlgoCaptionSeparator{.}
\renewcommand\AlCapFnt{\textit\textbf\color{blue}}
\usepackage[font={color=mybluei,bf,it},figurename=Fig.,labelfont={it}]{caption}
\begin{document}
\begin{center}
\colorbox[gray]{0.95}{
\begin{minipage}{0.65\textwidth}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
\begin{algorithm}[H]
\DontPrintSemicolon
This is line one\\
This is line two numbered\\
This should be numbered\\
This should also be numbered
\caption{Unnumbered lines}
\end{algorithm} 
\end{minipage}} 
\end{center}

\end{document} 
  • 1
    Maybe \renewcommand\AlCapFnt{\itshape\bfseries\color{blue}}? – Benjamin Apr 24 '16 at 1:35
  • @Benjamin, Thanks for your input. The \AlCapFnt, appears to change the algorithm name, "Algorithm 1". I wanted to be able to change the caption name, so in this example, it will be to change "Unnumbered lines" to \itshape\bfseries\color{blue}. I will change my original question to be clearer. Thanks again for your help so far. – Joe Apr 24 '16 at 3:02
3

algorithm2e's \caption has the following format:

\AlCapSty{\AlCapFnt Algorithm 1:}\AlCapNameSty{\AlCapNameFnt my algorithm}

Taken from the "change log":

By default, \AlCapSty is \textbf and \AlCapFnt is nothing. \AlCapNameSty keep text as it is, and \AlCapNameFnt do nothing also. You can redefine \AlCapFnt and \AlCapNameFnt by giving macro to \Set... commands. For example, you can do \SetAlCapFnt{\large} to see Algorithm printed in \large font.

You can redefine \AlCapSty, \AlCapFnt, \AlCapNameSty and \AlCapNameFnt with the corresponding \Set... command. For the \...Sty commands, you have to give in parameter name of a macro (without \) which takes one argument. For example, \SetAlCapFnt{textbf} defines the default behaviour. If you want to do more complicated thing, you should define your own macro and give it to \SetAlCapFnt or \SetAlCapNameFnt. Here are two examples:

\newcommand{\mycapsty}[1]{\tiny #1}\SetAlCapNameSty{mycapsty}
\newcommand{\mycapsty}[1]{\textsl{\small #1}}\SetAlCapNameSty{mycapsty}

Or you can combine the two, for the last example you can also do:

\SetAlCapNameSty{textsl}\SetAlCapNameFnt{\small}

With the above information, the following provides what you're after:

\renewcommand\AlCapFnt{\itshape\bfseries\color{blue}}
\renewcommand\AlCapNameFnt{\AlCapFnt}

enter image description here

\documentclass{article}

\usepackage{xcolor}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

% \AlCapSty{\AlCapFnt Algorithm 1:}\AlCapNameSty{\AlCapNameFnt my algorithm}
\renewcommand\AlCapFnt{\itshape\bfseries\color{blue}}
\renewcommand\AlCapNameFnt{\AlCapFnt}

\begin{document}

\begin{algorithm}
  \DontPrintSemicolon
  This is line one \;
  This is line two numbered \;
  This should be numbered \;
  This should also be numbered
  \caption{Unnumbered lines}
\end{algorithm} 

\end{document}
  • I am sorry for asking this, but I couldn't really comprehend the difference between AlCapFnt and AlCapSty. AlCapFnt can change the font name and size in addition to the ability of centering the caption. So, what is the need for AlCapSty or when do I need to redefine AlCapSty? – Diaa Feb 4 '17 at 9:51
  • @DiaaAbidou: The package author tried to keep a font-specific switch for the caption in \AlCapFnt, while \AlCapSty is meant to represent the entire caption style. This could include font switches as well as other things (like fixed text). You don't have to use \AlCapFnt, as you can insert that manually as part of \AlCapSty. – Werner Feb 4 '17 at 16:18
  • When trying to change AlCapSty, I have to do it in this 2-command step: \newcommand{\mycapsty}[1]{\centering\normalfont#1} \SetAlCapNameSty{mycapsty}. Is there a single command that can do the same job? – Diaa Feb 4 '17 at 16:34
  • @DiaaAbidou: Yes, try \renewcommand{\AlCapFnt}{\centering\normalfont}. – Werner Feb 4 '17 at 16:43
1

Change the \caption command line to \caption{\color{blue} \textbf{\textit{Unnumbered lines}}} and compile you will get:

captionblue

  • Thanks for your input. I was looking for a way to automatically do it, just like the \renewcommand\AlCapFnt{\itshape\bfseries\color{blue}}. You set it once in the preamble and then its set. Do you know of a solution to set the caption label in the same manner so you would not have to always type \caption{\color{blue} \textbf{\textit{ ? Thanks. – Joe Apr 24 '16 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.