Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Alternative basicstyle[s] in listings

462 views
Skip to first unread message

Peter Flynn

unread,
Jan 18, 2015, 4:36:03 PM1/18/15
to
I have some slides for which I am using the listings package (in
Beamer). I have a very long and complex 'basicstyle' (working perfectly).

I need to change the font size slightly in a few slides, in order to
ensure that the text in a lstlistings environment will fit.

Is there any easy way to make a small change to the basicstyle, rather
than have to give the modified version in full (or rather, macroize it)
each time I need smaller type?

///Peter

Jean-Côme Charpentier

unread,
Jan 18, 2015, 5:50:57 PM1/18/15
to
If your normalsize font fits in the default width grid of listings you
can try to decrease colum width. For instance
\lstset{basewidth={0.4em,0.4em}} or less. If it looks ugly, you can
define a macro for your basicstyle default, and add your size. For example
\lstset{basicstyle=\small\mybasicdefault}
You can also define a macro with parameter. For instance:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\newcommand\mybasicdefault[1]{#1\ttfamily\color{red}}
\lstset{language=C, basicstyle=\mybasicdefault{}}

\begin{document}
\begin{lstlisting}
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char** argv){
printf("hello world!\n");
return 0;
}
\end{lstlisting}

\begin{lstlisting}[basicstyle=\mybasicdefault{\tiny}]
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char** argv){
printf("hello world!\n");
return 0;
}
\end{lstlisting}

\end{document}

You can define a general style. For instance:

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}

\newcommand\mybasicdefault{\ttfamily\color{red}}
\lstdefinestyle{small}{basicstyle=\small\mybasicdefault}
\lstdefinestyle{footnotesize}{basicstyle=\footnotesize\mybasicdefault}
\lstdefinestyle{scriptsize}{basicstyle=\scriptsize\mybasicdefault}
\lstdefinestyle{tiny}{basicstyle=\tiny\mybasicdefault}

\lstset{language=C, basicstyle=\mybasicdefault{}}

\begin{document}
\begin{lstlisting}
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char** argv){
printf("hello world!\n");
return 0;
}
\end{lstlisting}

\begin{lstlisting}[style=tiny]
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char** argv){
printf("hello world!\n");
return 0;
}
\end{lstlisting}

\end{document}

I'm sure there are some other possible answers!

Jean-Côme Charpentier

Axel Berger

unread,
Jan 18, 2015, 9:15:10 PM1/18/15
to
Peter Flynn wrote on Sun, 15-01-18 22:35:
>I need to change the font size slightly in a few slides, in order to
>ensure that the text in a lstlistings environment will fit.

Have you tried the shrink option?

\begin{frame}[shrink=<percentage>]

Axel

Peter Flynn

unread,
Jan 19, 2015, 2:53:06 PM1/19/15
to
On 01/18/2015 10:50 PM, Jean-Côme Charpentier wrote:
[...]
> If your normalsize font fits in the default width grid of listings you
> can try to decrease colum width. For instance
> \lstset{basewidth={0.4em,0.4em}} or less. If it looks ugly, you can
> define a macro for your basicstyle default, and add your size. For example
> \lstset{basicstyle=\small\mybasicdefault}
> You can also define a macro with parameter. For instance:
>
> \newcommand\mybasicdefault[1]{#1\ttfamily\color{red}}
> \lstset{language=C, basicstyle=\mybasicdefault{}}

That's much neater than my attempt! Thank you.

On 01/19/2015 01:36 AM, Axel Berger wrote:
> Have you tried the shrink option?
>
> \begin{frame}[shrink=<percentage>]

I had missed that, thanks very much.

///Peter
0 new messages