Graphics, Figures & TablesPartially colored algorithmic environment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
zaungast
Posts: 2
Joined: Tue Apr 17, 2012 10:26 am

Partially colored algorithmic environment

Post by zaungast »

Dear all,
i've tried to partially color an algorithm defined within the algorithmic-environment. To do so, i've used a combination of \vbox and \colorbox. However, the output doesn't look like desired (see attachment), since lines 2 and 4 are not indented. Here's a short code example, illustrating the problem...

Code: Select all

\documentclass[a4paper]{article} 
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{color}
\begin{document}

\begin{algorithm}[H]
	\caption{test algorithm}
	\begin{algorithmic}[1]
	\IF {A = B}
		\vbox{\colorbox{green}{\vbox{\IF {B = C} \STATE {C = A} \ENDIF}}}
	\ENDIF
	\end{algorithmic}
	\end{algorithm}

\end{document}
Is there any possibility to partially color an algorithm defined within the algorithmic environment?
Attachments
Algorithm.png
Algorithm.png (15.81 KiB) Viewed 19766 times
Last edited by Stefan Kottwitz on Tue Apr 17, 2012 10:55 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10350
Joined: Mon Mar 10, 2008 9:44 pm

Partially colored algorithmic environment

Post by Stefan Kottwitz »

Hi,

welcome to the board!

Do you have the pgf package installed? I would use TikZ for that, which is a front-end for pgf - similar to this matrix example.

Stefan
LaTeX.org admin
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Partially colored algorithmic environment

Post by Juanjo »

Look the following code:

Code: Select all

\documentclass[a4paper]{article} 
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{color}
\begin{document}

\begin{algorithm}[H]
   \caption{test algorithm}
   \begin{algorithmic}[1]
      \IF {A = B}  
      \hspace*{-\fboxsep}\colorbox{green}{\parbox{\linewidth}{%
      \IF {B = C} \STATE {C = A} \ENDIF}}
      \ENDIF
   \end{algorithmic}
\end{algorithm}

\end{document}
I would suggest to use the algorithmicx bundle instead of algorithmic, since it has many features that can be useful. For example,

Code: Select all

\documentclass[a4paper]{article} 
\usepackage[compatible]{algpseudocode}
\usepackage{algorithm}
\usepackage{color}
\begin{document}

\begin{algorithm}[H]
   \caption{test algorithm}
   \begin{algorithmic}[1]
      \IF {A = B}  \newline
      \hspace*{-\fboxsep}\colorbox{green}{\parbox{\linewidth}{%
      \IF {B = C} \STATE {C = A} \ENDIF}}
      \ENDIF
   \end{algorithmic}
\end{algorithm}

\end{document}
The algpseudocode package is part of algorithmicx. Above, it has been called in compatibility mode with algorithmic.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
zaungast
Posts: 2
Joined: Tue Apr 17, 2012 10:26 am

Re: Partially colored algorithmic environment

Post by zaungast »

The above mentioned solution by Juanjo works perfectly. Thank you very much :)
betabandido
Posts: 2
Joined: Wed Jul 25, 2012 6:31 pm

Partially colored algorithmic environment

Post by betabandido »

I am also looking for a solution to highlight some lines in an algorithm. The solution posted by Juanjo works well, but it seems to fail in some cases. For instance, the following works:

Code: Select all

\begin{algorithmic}[1]
    \FORALL{$elem$ in $collection$}
        \STATE $foo(elem)$
        \hspace*{-\fboxsep}\colorbox{green}{\parbox{\linewidth}{\STATE $bar(elem)$}}
    \ENDFOR
\end{algorithmic}
If I swap the order of the statements, however, then it does not compile:

Code: Select all

\begin{algorithmic}[1]
    \FORALL{$elem$ in $collection$}
        \hspace*{-\fboxsep}\colorbox{green}{\parbox{\linewidth}{\STATE $bar(elem)$}}
        \STATE $foo(elem)$
    \ENDFOR
\end{algorithmic}
The error that I get is:

Code: Select all

! Missing \endcsname inserted.
<to be read again> 
                  \ALG@currentblock@2 
l.19 \end{frame}
Do you know what could be the reason? Is there any way to fix this error?

Thank you very much.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Partially colored algorithmic environment

Post by localghost »

Just for information to other users for awareness of possibly already existing solutions. The question by betabandido has also been posted to {TeX} SX.
betabandido
Posts: 2
Joined: Wed Jul 25, 2012 6:31 pm

Partially colored algorithmic environment

Post by betabandido »

Just for reference, a solution has been posted here.
Post Reply