19

I want my Java code to be typeset in a monospaced font, so I am using the verbatim environment. LaTeX commands are highlighted in Eclipse, but as soon as I use the verbatim environment everything inside and the color of the environment itself becomes gray.

\begin{verbatim}

 public class main {
 public static void main (string[] args){
     for(int i = 0; i < arg.length; i ++){
         System.out.println(args[i]);
     }
   }
 };

\end{verbatim}

I'm using the verbatim and alltt packages.

Update 1. MWE.

\documentclass[xcolor=dvipsnames]{beamer}

\mode<presentation>
{
\usetheme{CambridgeUS}
\setbeamercovered{transparent}
}

% Поддержка русского языка
\RequirePackage[utf8]{inputenc}
\RequirePackage[T2A]{fontenc}
\RequirePackage[russian]{babel}

\RequirePackage{amsfonts}

\usepackage{algorithmic}
\usepackage{mathptmx}
\usepackage[scaled=.90]{helvet}
\usepackage{courier}
\usepackage{pst-node}
\usepackage{pst-tree}
\usepackage{auto-pst-pdf}
\usepackage{color}
\usepackage{colortbl}
\usepackage{pstricks}
\usepackage{verbatim}
\usepackage{alltt}
\usepackage{listings}
\usepackage{animate}
\usepackage{multimedia}

\title{Title}

\author{Name}

\institute{}

\date{\today}

\begin{document}

\begin{frame}
\titlepage
\end{frame}

\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}

\section{Introduction}

\begin{frame}{Классификация по поколениям}

\begin{itemize}

\item Третьего поколения (высокоуровневые языки) - последовательность
высокоуровневых конструкций (циклы, условные выражения, классы, интерфейсы,
методы, процедуры, \ldots) \pause

\begin{exampleblock}{Пример}

\begin{centering}

Fortran, C, C++, C\#, Java, Lisp.

\begin{verbatim}

public class main {
public statiс void main (string[] args){
for(int i = 0; i < arg.length; i ++){
System.out.println(args[i]);
}
}
};

\end{verbatim}

\end{centering}

\end{exampleblock}\pause

\item Четвертого поколения (для конкретного применения) -
последовательность инструкций конкретной предметной области \pause

\begin{exampleblock}{Пример}
SQL, Postscript, XML, Mathlab.
\end{exampleblock} \pause

\end{itemize}

\end{frame}

\end{document}

marked as duplicate by Mensch, Masroor, Stefan Pinnow, CarLaTeX, TeXnician Aug 8 '17 at 6:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Welcome to TeX.SX! Personally, I would expect this behavior. There is nothing telling eclipse that what's inside is java code—frankly I would be surprised if it handled that case even with a package such as listings. I'm not sure there is a way to get the behavior you want. – Sean Allred Feb 19 '14 at 6:12
  • As a hint, in a beamer presentation you can use semiverbatim, so you can use overlays and the like. – Johannes_B Feb 19 '14 at 7:34
  • 3
    If you want to use verbatim in a frame, you have to use \begin{frame}[fragile] – egreg Feb 19 '14 at 9:28
  • 1
    That's not a minimal example - you forgot the everything-but-the-kitchen-sink package. – Martin Schröder Feb 19 '14 at 16:06
  • 1
    Please read the fine beamer manual; it has a section on verbatim. – Martin Schröder Feb 19 '14 at 16:08
29

You cannot use any of the verbatim environments, including verbatim itself, just as any of the listing environment without writing,

\begin{frame}[fragile]\frametitle{....}
...
\end{frame}

or any other form of passing the fragile flag to the \frame command. If the frame does not include any overlays, i.e., it has only a single slide, it is recommended to write:

\begin{frame}[fragile=singleslide]\frametitle{....}
...
\end{frame}

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