0

I am trying to write some pseudocode using verbatim. The problem is, I can't get it write the right symbols. Here's my code:

\begin{verbatim}
    S= $\emptyset$
    while ((not ottimo(S) ) and (C \ne $\emptyset$))
         x = seleziona(C)
         C = C – {x}
         if (ammissibile(S U {x}))
               S = S U {x}
    if (ottimo(S))
         return S
    else
         return $\emptyset$

\end{verbatim}

If I write $\emptyset$ shows it exactly as it is written, but if I use Ø it returns error.

How can I fix it?

ps. it does the same with every other symbol, like not equal, ...

2 Answers 2

2

I don't think this is the best way to present pseudocode. Anyway, you can use alltt, but symbols will look very different.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{alltt}

\begin{document}

\begin{alltt}
    S = \(\emptyset\)
    while ((not ottimo(S) ) and (C \(\ne\) \(\emptyset\)))
         x = seleziona(C)
         C = C – \{x\}
         if (ammissibile(S U \{x\}))
               S = S U \{x\}
    if (ottimo(S))
         return S
    else
         return \(\emptyset\)

\end{alltt}

\end{document}

enter image description here

You may want to use the algpseudocode package, which provides customizable features for the purpose.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{algpseudocode}
\usepackage{amsmath}

\newcommand{\fun}[1]{\mathrm{#1}}% functions
\newcommand{\NOT}{\operatorname{not}}
\newcommand{\AND}{\mathbin{\mathrm{and}}}

\begin{document}

\begin{algorithmic}[1]
\State $S = \emptyset$
\While{$(\NOT \fun{ottimo}(S) ) \AND (C \ne\emptyset)$}
  \State $x = \fun{seleziona}(C)$
  \State $C = C - \{x\}$
  \If{$(\fun{ammissibile}(S\cup \{x\}))$}
    \State $S = S \cup \{x\}$
    \If{$(\fun{ottimo}(S))$}
      \State return $S$
    \Else
      \State return $\emptyset$
    \EndIf
  \EndIf
\EndWhile
\end{algorithmic}

\end{document}

enter image description here

5
  • Thank you very much!!! Commented Jun 14, 2020 at 17:03
  • Can I also add number of lines using this? Commented Jun 14, 2020 at 17:03
  • @hellomynameisA Not easily. That's why I said it's not the best way to display pseudocode. You can have a look at the algpseudocode package.
    – egreg
    Commented Jun 14, 2020 at 19:27
  • 1
    @hellomynameisA I added a possible code.
    – egreg
    Commented Jun 14, 2020 at 19:41
  • Thank you very much for your help!! :) Commented Jun 14, 2020 at 20:07
1

If you load fontspec, use only Unicode symbols and no LaTeX commands, and select a monospaced font that contains all those symbols, you can use verbatim.

\tracinglostchars=2 % Warn if a glyph is missing from the font!
\documentclass{article}
\usepackage{fontspec}

\setmonofont{DejaVu Sans Mono}

\pagestyle{empty} % Suppress page numbering for MWE.

\begin{document}
\begin{verbatim}
    S = Ø
    while ((not ottimo(S)) and (C ≠ Ø))
         x = seleziona(C)
         C = C – {x}
         if (ammissibile(S ∪ {x}))
               S = S ∪ {x}
    if (ottimo(S))
         return S
    else
         return Ø
\end{verbatim}
\end{document}

DejaVu Sans Mono sample

You cannot enter math mode or use commands within verbatim. There are several packages that format pseudocode more attractively than this, but that’s one way to get the retro look.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .