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 doing some slides using Beamer.

One of this slides is pure code

\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}

I would like to make this slides more readable and nicer. Is there any easy way to do that?

I have tried

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

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

But I do not have enough space to use this approach.

share|improve this question
3  
That's a lot of numbers for a slide. You probably don't expect your audience to read them all. Consider replacing most of the table with an ellipsis ... - that together with the other answers here should improve readability. –  Ethan Bolker Feb 8 at 14:41
    
For future questions: Please always post a complete minimal working example (MWE). –  Henri Menke Feb 8 at 17:40

3 Answers 3

up vote 11 down vote accepted

You might want to consider using minted. It offers syntax highlighting for GNU R through the external program pygmentize.

Make sure that pygmentize is in your $PATH and typeset the following MWE using 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

share|improve this answer
    
This would be the optimal solution. But can you explain a bit better? Have I to install pygemtize? In their site is seems like a python library. –  Donbeo Feb 8 at 14:25
    
This work. It worked also without pygemtize –  Donbeo Feb 8 at 14:50
    
@Donbeo Then pygmentize is already installed. It doesn't work without it. You're welcome :-). –  Henri Menke Feb 8 at 17:36
\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}

the second slide:

enter image description here

If you want to change the color of single expressions use:

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

then 2194 12276 will be in red:

enter image description here

You can use other characters for the escape sequence. I used backtick and tick (accent grave accent ecute)

share|improve this answer
    
This seems goo. Is there the possibility to have code of one color and result of another? for example dim(X) in blue and 2194 12276 in red? –  Donbeo Feb 8 at 14:13
    
see my edited answer –  Herbert Feb 8 at 14:57

You can use fancyvrb, for general verbatim blocks and adjustbox for the ones that are too wide:

\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

share|improve this answer

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.