# How to reproduce some attractive pseudo-code

I am trying to reproduce:

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}

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?

• You also need to indent after the if statement. Is it bold Courier? Aug 9, 2020 at 20:20
• 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. Aug 10, 2020 at 0:58
• The font is Computer Modern. Aug 10, 2020 at 9:32

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}

• Can |\textcolor{code-green}{mark}| be abbreviated to |\green{mark}| by defining new commands? Aug 10, 2020 at 5:13
• @Anush, sure! Just define what you need: \newcommand{\green}[1]{\textcolor{code-green}{#1}} Aug 10, 2020 at 7:55
• Picture perfect. It's a shame about requiring shell escape though. Aug 10, 2020 at 9:23
• @fomin, don't you have access to your shell? invoking pdflatex -shell-escape <tex file> doesn't seem so bad anymore. Even most editors have options to set the flag. If you need help there, just let me know… Aug 10, 2020 at 9:30
• You are right. I have set a menu option in TeXStudio now. Thank you again. Aug 10, 2020 at 9:33

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.

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}

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

• This is really nice, I had no idea about semiverbatim. Is there a way to make the box fit the text more tightly? Aug 10, 2020 at 9:28
• semiverbatim looks exactly what I need! I will combine the tcolorbox answer from phil-elkabat with this answer. Thank you. Aug 10, 2020 at 9:38
• @Anush Not with mdframed, I believe. Anyway, there it is. Aug 10, 2020 at 9:45
• Thank you! I don't fully understand all of your code but I will try to study it. Aug 10, 2020 at 11:51