使用 scalebox 减少列表代码的大小

使用 scalebox 减少列表代码的大小

我希望源代码适合一个框架,因为它的部分内容超出了框架。但是当我删除以下源代码中的注释以使用 scalebox 时,我得到了一些错误。这是我的代码和输出。

\documentclass{beamer}
\mode<presentation>
{
  \usetheme{CambridgeUS}
  \usecolortheme{dolphin}
  \usecolortheme{rose}
  \setbeamerfont*{title}{shape=\itshape, family=\rmfamily}
  \setbeamercovered{transparent}
}
\let\Tiny\tiny
\usepackage{listings}
\usepackage{color}
\lstset{
  language=C,
  basicstyle=\footnotesize\ttfamily, 
  numbers=left,
  numberstyle=\footnotesize,
  stepnumber=1,
  tabsize=2,
  numbersep=-2pt,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  frame=single,
  framexleftmargin=-10pt,
  captionpos=b,
  breaklines=true,
  breakatwhitespace=false,
  escapeinside={\%*}{*)}
}
\begin{document}
\begin{frame}[fragile]
    \frametitle{Source code for static cache blocking}
    %\scalebox{0.8}{ % something wrong when I remove the comment to use scalebox
    \begin{lstlisting}
      void block_smvp_sparse (int r, int m,
          int *row_start, int *block_ptr,
          double *value, int *col_idx,
          double *src, double *dest)
      {
        int i, j;
        int b_i, b_j, b_m;
        int end_r;
        b_m = (m+r-1)/r;
        for (b_i=0; b_i<b_m; b_i++){
          end_r = (b_i+1 < b_m) ? r : m - b_i*r ;
          for (b_j=row_start[b_i]; b_j<row_start[b_i+1]; b_j+=end_r){
            for (i=0; i<end_r; i++){
              double t=0;
              for (j=block_ptr[b_j+i]; j<block_ptr[b_j+i+1]; j++)
                t += value[j] * src[col_idx[j]];
              dest[b_i*r+i] += t;
            }
          }
        }
      }
    \end{lstlisting}
      %}
\end{frame}
\end{document}

在此处输入图片描述

答案1

像 这样的逐字类环境lstlisting不能用作 之类的命令的参数\scalebox

您可以使用lrbox创建一个包含 的框lstlisting,然后将其用作 的参数\scalebox

首先,添加

\newsavebox{\mybox}

在序言中。然后嵌入lstlistinglrbox

\begin{lrbox}{\mybox}%
    \begin{lstlisting}%
    .....
    \end{lstlisting}%
\end{lrbox}%

最后打印缩放框

\scalebox{0.8}{\usebox{\mybox}}

梅威瑟:

\documentclass{beamer}
\mode<presentation>
{
  \usetheme{CambridgeUS}
  \usecolortheme{dolphin}
  \usecolortheme{rose}
  \setbeamerfont*{title}{shape=\itshape, family=\rmfamily}
  \setbeamercovered{transparent}
}
\let\Tiny\tiny
\usepackage{listings}
\usepackage{color}
\lstset{
  language=C,
  basicstyle=\footnotesize\ttfamily,
  numbers=left,
  numberstyle=\footnotesize,
  stepnumber=1,
  tabsize=2,
  numbersep=-2pt,
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  frame=single,
  framexleftmargin=-10pt,
  captionpos=b,
  breaklines=true,
  breakatwhitespace=false,
  escapeinside={\%*}{*)}
}

\newsavebox{\mybox}


\begin{document}
\begin{frame}[fragile]
    \frametitle{Source code for static cache blocking}
\begin{lrbox}{\mybox}%
    \begin{lstlisting}%
      void block_smvp_sparse (int r, int m,
          int *row_start, int *block_ptr,
          double *value, int *col_idx,
          double *src, double *dest)
      {
        int i, j;
        int b_i, b_j, b_m;
        int end_r;
        b_m = (m+r-1)/r;
        for (b_i=0; b_i<b_m; b_i++){
          end_r = (b_i+1 < b_m) ? r : m - b_i*r ;
          for (b_j=row_start[b_i]; b_j<row_start[b_i+1]; b_j+=end_r){
            for (i=0; i<end_r; i++){
              double t=0;
              for (j=block_ptr[b_j+i]; j<block_ptr[b_j+i+1]; j++)
                t += value[j] * src[col_idx[j]];
              dest[b_i*r+i] += t;
            }
          }
        }
      }
    \end{lstlisting}%
\end{lrbox}%
\scalebox{0.8}{\usebox{\mybox}}
\end{frame}
\end{document} 

输出

在此处输入图片描述

此外,由于我怀疑您希望它在框架中水平居中,因此您可以使用

\begin{center}
\hspace*{1cm}\scalebox{0.8}{\usebox{\mybox}}
\end{center}

而不是简单地

\scalebox{0.8}{\usebox{\mybox}}

结果是:

在此处输入图片描述

答案2

或者karlkoeller 回答,同时你可以使用minted包(利用强大的程序pygments)它提供了属性fontsize,即通过 可以有效地完成什么scalebox

上述代码简化为:

\documentclass{beamer}
\mode<presentation>
{
  \usetheme{CambridgeUS}
  \usecolortheme{dolphin}
  \usecolortheme{rose}
  \setbeamerfont*{title}{shape=\itshape, family=\rmfamily}
  \setbeamercovered{transparent}
}
\usepackage{minted}

% Adjust margin to display linenumbers
\setbeamersize{text margin left=2em}

\begin{document}

\begin{frame}[fragile]

    \frametitle{Source code for static cache blocking}

    \begin{minted}[numbersep=5pt,
          frame=single,
          framesep=2pt,
          fontsize=\scriptsize,
          linenos,
          breaklines]{C}

      void block_smvp_sparse (int r, int m,
          int *row_start, int *block_ptr,
          double *value, int *col_idx,
          double *src, double *dest)
      {
        int i, j;
        int b_i, b_j, b_m;
        int end_r;
        b_m = (m+r-1)/r;
        for (b_i=0; b_i<b_m; b_i++){
          end_r = (b_i+1 < b_m) ? r : m - b_i*r ;
          for (b_j=row_start[b_i]; b_j<row_start[b_i+1]; b_j+=end_r){
            for (i=0; i<end_r; i++){
              double t=0;
              for (j=block_ptr[b_j+i]; j<block_ptr[b_j+i+1]; j++)
                t += value[j] * src[col_idx[j]];
              dest[b_i*r+i] += t;
            }
          }
        }
      }
    \end{minted}
\end{frame}
\end{document}

输出

附言:由于minted使用外部程序,因此必须使用标志进行编译-shell-escape,例如pdflatex -shell-escape beamercode.tex

相关内容