我正在用一些编程代码制作一个 Beamer 演示文稿。但是,verbatim 包默认将代码放在幻灯片的最左边。我希望幻灯片更靠近幻灯片的中心,但仍保持左侧对齐。我该怎么做?在 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}
答案1
\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}
答案2
另一种选择 - 特别是如果你不想让列表出现在中间 - 可能是使用columns
beamer 中可用的环境,例如
\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}
答案3
我喜欢使用包xleftmargin
中的选项listings
在幻灯片中以一致的方式获取这种格式:
\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}
包的优点listings
是您可以使用格式化选项,如下所示:
\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}