4

I am trying to type computer code in my document using the alltt environment. Some symbols, such as ' and {, do not appear in the document the way they do in the code. I solved the problem with ' by using the upquote package. But I still have a problem with { and }; these symbols are taller than the surrounding text in the alltt environment. For example,

\documentclass{article}
\usepackage[margin=2cm]{geometry} %set margins
\usepackage{color}
\usepackage{alltt}
\usepackage{upquote}

\begin{document}

    \begin{alltt}
    for (i in 1:3){
        print(i^2)
        }
    \end{alltt}

\end{document}

doesn't do what I want (since the braces do not appear), and neither does using \{ and \} in place of { and } (the braces are too tall). I think this question is something similar, but I do not understand the answers given there.

  • Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – jub0bs May 26 '13 at 23:15
  • 2
    Why don't you use the verbatim environment? – egreg May 26 '13 at 23:19
  • 1
    Thank you. I edited the question to include compilable code. – Flounderer May 26 '13 at 23:20
  • 2
    Or use listings package. – cacamailg May 26 '13 at 23:21
  • 2
    From the alltt abstract: This package defines the alltt environment, which is like the verbatim environment except that \, {, and } have their usual meanings. Thus, other commands and environments can appear within an alltt environment. – cacamailg May 26 '13 at 23:24
  • @egreg that looks good! Do I not need to use alltt at all then? I thought for some reason that I needed it if I wanted to have my code in the typewriter font, but verbatim seems to use that font as well. – Flounderer May 26 '13 at 23:25
  • Thanks, that solves my problem; I'll just use verbatim instead. I have upvoted your comments! – Flounderer May 26 '13 at 23:26
  • 2
    @Flounderer in alltt you can use \textbackslash for \ and \} and \{ for } and {, respectively. – cacamailg May 26 '13 at 23:30
  • If you want to get the correct braces in an alltt environment you should use \{ \} and \usepackage[T1]{fontenc} (which is a good idea anyway - a lot problems with wrong symbols are due to the restrictions of the default OT1-encoding). – Ulrike Fischer May 27 '13 at 08:37

1 Answers1

3

The alltt environment is a kind of "semiverbatim": it uses the typewriter font, but still \, { and } maintain their meaning, for being able to use commands (say for changing color, fonts or whatever).

For real verbatim, use the verbatim environment:

\documentclass{article}
\usepackage{upquote}

\begin{document}

\begin{verbatim}
for (i in 1:3){
     print(i^2)
    }
\end{verbatim}

\end{document}

enter image description here

Limitation: you shouldn't "globally" indent it; all spaces at the beginning of lines are honored, so begin at the left margin and indent as much as you want inside the environment.

More features are provided by the fancyvrb package. Still many more are available with the very powerful listings package.

egreg
  • 1,121,712