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 am putting together a beamer presentation with some programming code. However, the verbatim package by default places the code on the left most fringe of the slide. I want the slide to be a little closer to the center of the slide, but still left alligned. How can I do this? can't find anything on google

\begin{frame}[fragile]
\begin{center}
\begin{verbatim}
> library(memisc)
> x <- round(rnorm(3),1)
> y <- round(rnorm(3)*2,1)
> mytable <- genTable(y ~ x)
> toLatex( mytable )
\end{verbatim}
\end{center}
\end{frame}
share|improve this question
    
"I want the slide to be a little closer to the center of the slide, but still left alligned. " what does it mean? –  xport Jul 16 '11 at 18:35
    
@xport There needs to be some tabs from the left, but the code still needs to be aligned. so the letters l,x, y, m, and t need to be in the same position. –  ATMathew Jul 16 '11 at 18:39
add comment

3 Answers

up vote 6 down vote accepted
\documentclass{beamer}

\begin{document}
\begin{frame}[fragile]
\begin{center}
\begin{minipage}{0.5\linewidth}
\begin{verbatim}
> library(memisc)
> x <- round(rnorm(3),1)
> y <- round(rnorm(3)*2,1)
> mytable <- genTable(y ~ x)
> toLatex( mytable )
\end{verbatim}
\end{minipage}
\end{center}
\end{frame}
\end{document}

enter image description here

share|improve this answer
    
+1 wow, good stuff! –  ATMathew Jul 16 '11 at 18:41
add comment

an alternative - especially if you don't want the listing plonked in the middle - might be to use the columns environment available in beamer, e.g.

  \begin{columns}
  \begin{column}{0.1\textwidth}  % this is empty to create the shift right
  \end{column}
  \begin{column}{0.9\textwidth}   % adjust proportions to your liking
 %      insert your code
 %       .....
   \end{column}
  \end{columns}
share|improve this answer
add comment

I like to use the xleftmargin option in listings package to get this sort of formatting in a consistent way across slides:

\documentclass{beamer}
\usepackage{listings}
\lstset{
aboveskip=1ex,
xleftmargin=2em, % change this value for different indents
basicstyle={\ttfamily \small},
breaklines=true,
breakatwhitespace=false,
showspaces=false,
}

\lstMakeShortInline|  % useful for inline code

\begin{document}
\begin{frame}[fragile]


This is text before the code listing...

\begin{lstlisting}
> library(memisc)
> x <- round(rnorm(3),1)
> y <- round(rnorm(3)*2,1)
> mytable <- genTable(y ~ x)
> toLatex( mytable )
\end{lstlisting}

And this is after, including some inline code: |round()| and |length()| are both R functions.

\end{frame}
\end{document}

The advantage of listings package is you can get fancy with the formatting option, as shown below:

\documentclass[svgnames]{beamer}
\usepackage{listings}
\lstMakeShortInline|  % useful for inline code
\lstset{%
aboveskip=1ex,
xleftmargin=2em,
xrightmargin=3pt,
breaklines=true,
basicstyle={\ttfamily \small},
breakatwhitespace=false,
showspaces=false,
showstringspaces=false
showtabs=false,
keywordstyle=\color{DarkRed}\bfseries,
stringstyle=\color{DarkBlue},
commentstyle=\color{Olive}\itshape,
language=R, % sets default language
}

\begin{document}
\begin{frame}[fragile]


This is text before the code listing...

\begin{lstlisting}
> library(memisc)
> x <- round(rnorm(3),1)
> y <- round(rnorm(3)*2,1)
> mytable <- genTable(y ~ x)  # a comment here
> toLatex( mytable )
\end{lstlisting}

And this is after, including some inline code: |round()| and |length()| are both R functions.

\end{frame}
\end{document}
share|improve this answer
add comment

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.