Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to output Prolog style inside algorithm2e and It seems that I cannot use \verb inside algorithm2e.

How can I use \verb or verbatim for only one line inside algorithm2e in Latex?

share|improve this question

migrated from stackoverflow.com Mar 11 '13 at 11:18

This question came from our site for professional and enthusiast programmers.

    
Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Mar 11 '13 at 14:29

1 Answer 1

Since algorithm2e uses a macro-style interface for its programming structures, using verbatim directly is a problem. If its use is really necessary, boxing the content before using it allows you to pass it as an argument to the programming structure commands:

enter image description here

\documentclass{article}
\usepackage{algorithm2e}% http://ctan.org/pkg/algorithm2e
\newsavebox{\mycode}
\begin{document}

\begin{algorithm}[H]
  \begin{lrbox}{\mycode}
  \verb!%&$*@#*%!
  \end{lrbox}
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e}
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section and \usebox{\mycode}\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}
\end{document}

The verbatim content is stored in \mycode first using the lrbox environment. Subsequent usage of this box is via \usebox{\mycode}.

If it's not really necessary, using \texttt{..}, say, would (should) suffice. The decide here would be based on whether you have funny characters included in the part you designate as requiring verbatim.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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