如何让这个逐字代码看起来美观?

如何让这个逐字代码看起来美观?

我正在使用 Beamer 制作一些幻灯片。

其中一张幻灯片是纯代码

\begin{frame}[fragile]

\begin{verbatim}
> dim(X)   #dimension of our matrix X
[1]  2194 12276
> length(Y) # length of out output y
[1] 2194
> cv=cv.glmnet(X,Y,nfolds=10) # run 10 folds CV 
> cv$lambda.min  # The optimal value of lambda 
[1] 0.02664502
# Beta are the coefficient estimated by lasso
> beta=coef(cv,"lambda.min") 
> which(beta!=0) # which coefficient are estimated non 0
 [1]     1    23    90   393   726   824  1343  1432  1451  1526  1527  1569  1574
[14]  1797  1952  2048  2106  2110  2118  2121  2122  4094  4101  4103  5944  6519
[27]  6524  6525  6542  6544  6545  6546  6839  7026  8283  8318  8321  8322  8323
[40]  8328  8364  8637  8638  8643  8644  8699  8701  9025  9028  9031  9249  9254
[53]  9255  9754  9755  9906  9921 10102 10103 10105 10175 10177 10193 10204 10243
[66] 10260 10261 10300 10306 10636 10934 11077 11078 11294 11295 11297 11299 11300
[79] 11423 11436 12018 12082 12221 12222 12223 12248
> length(which(beta!=0))# how many are estimated non 0
[1] 86
\end{verbatim}

\end{frame}

我想让这个幻灯片更易读、更美观。有什么简单的方法可以做到这一点吗?

我努力了

{\color{blue}\begin{verbatim}
 ....part 1...
\end{verbatim}
}

{\color{red}\begin{verbatim}
...part 2 ....
\end{verbatim}
}

但我没有足够的空间来使用这种方法。

答案1

您可能需要考虑使用minted。它通过外部程序为 GNU R 提供语法高亮显示pygmentize

确保pygmentize在您的中$PATH并使用排版以下 MWE pdflatex -shell-escape

\documentclass{beamer}
\usepackage{minted}
\begin{document}
\begin{frame}[fragile]
\tiny
\begin{minted}{rconsole}
> dim(X)   #dimension of our matrix X
[1]  2194 12276
> length(Y) # length of out output y
[1] 2194
> cv=cv.glmnet(X,Y,nfolds=10) # run 10 folds CV 
> cv$lambda.min  # The optimal value of lambda 
[1] 0.02664502
# Beta are the coefficient estimated by lasso
> beta=coef(cv,"lambda.min") 
> which(beta!=0) # which coefficient are estimated non 0
[1]     1    23    90   393   726   824  1343  1432  1451  1526  1527  1569  1574
[14]  1797  1952  2048  2106  2110  2118  2121  2122  4094  4101  4103  5944  6519
[27]  6524  6525  6542  6544  6545  6546  6839  7026  8283  8318  8321  8322  8323
[40]  8328  8364  8637  8638  8643  8644  8699  8701  9025  9028  9031  9249  9254
[53]  9255  9754  9755  9906  9921 10102 10103 10105 10175 10177 10193 10204 10243
[66] 10260 10261 10300 10306 10636 10934 11077 11078 11294 11295 11297 11299 11300
[79] 11423 11436 12018 12082 12221 12222 12223 12248
> length(which(beta!=0))# how many are estimated non 0
[1] 86
\end{minted}
\end{frame}
\end{document}

enter image description here

答案2

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{listings}

\begin{document}
\begin{frame}[fragile]
\begin{lstlisting}[basicstyle=\color{blue}\tiny\ttfamily,language=R]
> dim(X)   #dimension of our matrix X
[1]  2194 12276
> length(Y) # length of out output y
[1] 2194
> cv=cv.glmnet(X,Y,nfolds=10) # run 10 folds CV 
> cv$lambda.min  # The optimal value of lambda 
[1] 0.02664502
# Beta are the coefficient estimated by lasso
> beta=coef(cv,"lambda.min") 
\end{lstlisting}
\pause
\begin{lstlisting}[basicstyle=\color{red}\tiny\ttfamily,language=R,breaklines]
> which(beta!=0) # which coefficient are estimated non 0
 [1]     1    23    90   393   726   824  1343  1432  1451  1526  1527  1569  1574
[14]  1797  1952  2048  2106  2110  2118  2121  2122  4094  4101  4103  5944  6519
[27]  6524  6525  6542  6544  6545  6546  6839  7026  8283  8318  8321  8322  8323
[40]  8328  8364  8637  8638  8643  8644  8699  8701  9025  9028  9031  9249  9254
[53]  9255  9754  9755  9906  9921 10102 10103 10105 10175 10177 10193 10204 10243
[66] 10260 10261 10300 10306 10636 10934 11077 11078 11294 11295 11297 11299 11300
[79] 11423 11436 12018 12082 12221 12222 12223 12248
> length(which(beta!=0))# how many are estimated non 0
[1] 86
\end{lstlisting}  
\end{frame}

\end{document}

第二张幻灯片:

enter image description here

如果您想改变单个表达式的颜色,请使用:

[....]
\begin{lstlisting}[basicstyle=\color{blue}\tiny\ttfamily,language=R,escapeinside=`']
> dim(X)   #dimension of our matrix X
[1]  `\textcolor{red}{2194 12276}'
[...]

则为2194 12276红色:

enter image description here

您可以使用其他字符作为转义序列。我使用了反引号和勾号(重音符号 ecute)

答案3

您可以使用fancyvrb, 来表示一般的逐字块以及adjustbox太宽的逐字块:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb,adjustbox}

\begin{document}
\begin{frame}[fragile]
\begin{Verbatim}[fontsize=\scriptsize,formatcom=\color{blue}]
> dim(X)   #dimension of our matrix X
[1]  2194 12276
> length(Y) # length of out output y
[1] 2194
> cv=cv.glmnet(X,Y,nfolds=10) # run 10 folds CV 
> cv$lambda.min  # The optimal value of lambda 
[1] 0.02664502
# Beta are the coefficient estimated by lasso
> beta=coef(cv,"lambda.min") 
\end{Verbatim}
\pause

\begin{adjustbox}{max width=\textwidth}
\begin{BVerbatim}[fontsize=\scriptsize,formatcom=\color{red}]
> which(beta!=0) # which coefficient are estimated non 0
 [1]     1    23    90   393   726   824  1343  1432  1451  1526  1527  1569  1574
[14]  1797  1952  2048  2106  2110  2118  2121  2122  4094  4101  4103  5944  6519
[27]  6524  6525  6542  6544  6545  6546  6839  7026  8283  8318  8321  8322  8323
[40]  8328  8364  8637  8638  8643  8644  8699  8701  9025  9028  9031  9249  9254
[53]  9255  9754  9755  9906  9921 10102 10103 10105 10175 10177 10193 10204 10243
[66] 10260 10261 10300 10306 10636 10934 11077 11078 11294 11295 11297 11299 11300
[79] 11423 11436 12018 12082 12221 12222 12223 12248
> length(which(beta!=0))# how many are estimated non 0
[1] 86
\end{BVerbatim}
\end{adjustbox}
\end{frame}

\end{document}

enter image description here

相关内容