TransWikia.com

How to reproduce some attractive pseudo-code

TeX - LaTeX Asked on September 5, 2020

I am trying to reproduce:

enter image description here

I have made some progress with the following code:

documentclass{beamer}
usetheme{default}
usepackage{enumitem}
usepackage{framed} 
usepackage[framemethod=TikZ]{mdframed}
title{Beamer Template}
begin{document}
begin{frame}

end{frame}
begin{mdframed}
begin{itemize}[label={}]
    item textsc{Traverse}($s$)
     vspace{0.5cm}
    item put $color{blue}s$ into the textcolor{green}{bag} 
    item while the textcolor{green}{bag} is not empty
    begin{itemize}
        item take $color{blue}u$ from the bag
        item if $color{blue}u$ unmarked
        item textcolor{red}{mark} $color{blue}u$
        item for every edge $color{blue}(u,v)$
        begin{itemize}
            item put $color{blue}v$ into the bag
        end{itemize}
    end{itemize}
    
end{itemize}
end{mdframed}
end{document}

enter image description here

To complete this I need:

  • Fit frame to text
  • Make the frame thicker
  • Find the right font
  • Find the right colors

I can do the final one. How can I do the other three?

3 Answers

You could try something entirely different.

article (report, or book) document classes

documentclass{article}

usepackage{minted}
usepackage[many]{tcolorbox}
tcbuselibrary{skins}
tcbset{
    arc=0pt,
    outer arc=0pt,
    colback=white,
}

definecolor{code-yellow}{RGB}{232, 168, 24}
definecolor{code-blue}{RGB}{0, 114, 178}
definecolor{code-green}{RGB}{0, 158, 115}

begin{document}

begin{tcolorbox}[text width=5cm]
begin{minted}[escapeinside=||,mathescape=true]{text}
|textsc{Traverse}|(|color{code-blue}{$s$}|)

put |color{code-blue}{$s$}| into the |textcolor{code-yellow}{bag}|
while the |textcolor{code-yellow}{bag}| is not empty
  take |$color{code-blue}u$| from the |textcolor{code-yellow}{bag}|
  if |color{code-blue}{$u$}| unmarked
    |textcolor{code-green}{mark}| |$color{code-blue}u$|
    for every edge |$color{code-blue}(u,v)$|
        put |$color{code-blue}v$| into the |textcolor{code-yellow}{bag}|
end{minted}
end{tcolorbox}

end{document}

beamer document class

documentclass{beamer}

usepackage{minted}
usepackage[many]{tcolorbox}
tcbuselibrary{skins}
tcbset{
    arc=0pt,
    outer arc=0pt,
    colback=white,
}

definecolor{code-yellow}{RGB}{232, 168, 24}
definecolor{code-blue}{RGB}{0, 114, 178}
definecolor{code-green}{RGB}{0, 158, 115}

begin{document}

begin{frame}[fragile]

begin{tcolorbox}[text width=5.5cm]
begin{minted}[escapeinside=||,mathescape=true]{text}
|textsc{Traverse}|(|color{code-blue}{$s$}|)

put |color{code-blue}{$s$}| into the |textcolor{code-yellow}{bag}|
while the |textcolor{code-yellow}{bag}| is not empty
  take |$color{code-blue}u$| from the |textcolor{code-yellow}{bag}|
  if |color{code-blue}{$u$}| unmarked
    |textcolor{code-green}{mark}| |$color{code-blue}u$|
    for every edge |$color{code-blue}(u,v)$|
        put |$color{code-blue}v$| into the |textcolor{code-yellow}{bag}|
end{minted}
end{tcolorbox}

end{frame}

end{document}

code with minted

Correct answer by phil-elkabat on September 5, 2020

Take a look at the packages for pseudocode (and/or listings). At least my personal favorite algorithm2e (seems to) have the required flexibility to create a format like you want with little pain. If there is extensive use, that is; if it is a few algorithms only doing the variable and so on coloring "by hand" is simpler.

Answered by vonbrand on September 5, 2020

You can use semiverbatim, that's built in beamer.

documentclass{beamer}
usetheme{default}
usepackage[framemethod=TikZ]{mdframed}

title{Beamer Template}

newcommand{Proc}[1]{textsc{#1}}
newcommand{Var}[1]{ensuremath{textcolor{varcolor}{#1}}}
newcommand{Tag}[1]{textcolor{tagcolor}{#1}}
newcommand{Com}[1]{textcolor{comcolor}{#1}}

definecolor{varcolor}{RGB}{15,122,183}
definecolor{tagcolor}{RGB}{230,159,3}
definecolor{comcolor}{RGB}{10,161,119}

begin{document}

begin{frame}[fragile]

begin{mdframed}
begin{semiverbatim}
Proc{Traverse}(Var{s})

put Var{s} into the Tag{bag} 
while the Tag{bag} is not empty
  take Var{u} from the bag
  if Var{u} unmarked
    Com{mark} Var{u}
    for every edge Var{(u,v)}
      put Var{v} into the Tag{bag}    
end{semiverbatim}
end{mdframed}

end{frame}

end{document}

Don't forget the fragile option.

enter image description here

An alternative is to use BVerbatim from fancyvrb.

documentclass{beamer}
usetheme{default}
usepackage{fancyvrb}

title{Beamer Template}

newcommand{Proc}[1]{textsc{#1}}
newcommand{Var}[1]{ensuremath{textcolor{varcolor}{#1}}}
newcommand{Tag}[1]{textcolor{tagcolor}{#1}}
newcommand{Com}[1]{textcolor{comcolor}{#1}}

definecolor{varcolor}{RGB}{15,122,183}
definecolor{tagcolor}{RGB}{230,159,3}
definecolor{comcolor}{RGB}{10,161,119}

newenvironment{pseudocode}
 {%
  VerbatimEnvironment
  begin{lrbox}{pseudocodebox}
  fvset{commandchars={}}
  begin{BVerbatim}
 }
 {end{BVerbatim}end{lrbox}fbox{usebox{pseudocodebox}}}
newsavebox{pseudocodebox}

begin{document}

begin{frame}[fragile]

begin{pseudocode}
Proc{Traverse}(Var{s})

put Var{s} into the Tag{bag} 
while the Tag{bag} is not empty
  take Var{u} from the bag
  if Var{u} unmarked
    Com{mark} Var{u}
    for every edge Var{(u,v)}
      put Var{v} into the Tag{bag}    
end{pseudocode}

end{frame}

end{document}

enter image description here

As far as I know, mdframed always uses the whole text width. Check with tcolorbox for more powerful methods.

Answered by egreg on September 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP