# Decrease font size in a listing

Hi I am trying to include a short code snippet into one of my beamer slides

However one of the lines is not fitting into the slides. Please let me know how to decrease the font size to make the source code fit properly.

Here is my code,

\begin{frame}[fragile]{CUDA C Concepts}
\textbf{\color{orange}Kernel} \textit{Def} C functions which are executed N times in parallel by
\lstset{language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
}
\begin{lstlisting}
// Kernel Definition
__global__ void VecAdd(float* A, float* B, float* C)
{
C[i] = A[i] + B[i];
}
int main(void)
{
...
// Kernel Invocation with N threads
return 0;
}
\end{lstlisting}
\end{frame}


Here is the output of the current latex code.

-

In this case you can use breaklines=true to activate automatic line breaking of long lines:

\documentclass{beamer}
\usepackage{listings}

\begin{document}

\begin{frame}[fragile]{CUDA C Concepts}
\textbf{\color{orange}Kernel} \textit{Def} C functions which are executed N times in parallel by
\lstset{language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
breaklines=true
}
\begin{lstlisting}
// Kernel Definition
__global__ void VecAdd(float* A, float* B, float* C)
{
C[i] = A[i] + B[i];
}
int main(void)
{
...
// Kernel Invocation with N threads
return 0;
}
\end{lstlisting}
\end{frame}

\end{document}


If you still want to reduce the font size, you can use one of the standard font size switches (or \fontsize{<size>}{<baseline>}\selectfont) in basicstyle, as in:

basicstyle=\ttfamily\footnotesize


A complete example in which I used \scriptsize:

\documentclass{beamer}
\usepackage{listings}

\begin{document}

\begin{frame}[fragile]{CUDA C Concepts}
\textbf{\color{orange}Kernel} \textit{Def} C functions which are executed N times in parallel by
\lstset{language=C++,
basicstyle=\ttfamily\scriptsize,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
breaklines=true
}
\begin{lstlisting}
// Kernel Definition
__global__ void VecAdd(float* A, float* B, float* C)
{
C[i] = A[i] + B[i];
}
int main(void)
{
...
// Kernel Invocation with N threads
return 0;
}
\end{lstlisting}
\end{frame}

\end{document}


As a side note, I would move this section

\lstset{language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
breaklines=true
}


to the preamble of the document.

-

Option breaklines and smaller font sizes are already explained in detail by Gonzalo.

## Flexible column alignment

The default settings of package listings uses a fixed column alignment, where all letters use the same space. It destroys the normal distances between letters in the word, that makes the look ugly. Also the letter boxes are quite broad, because wide letters have to fit in the boxes, too. Flexible column alignments get better results, especially if the fixed layout is not needed, see documentation of listings, "2.10 Fixed and flexible columns" and "4.13 Column alignment". The example below uses flexible.

## Font with variable widths

Also the fonts can be improved. The example uses cmtt, the typewriter font of Computer Modern. A further development of these fonts, the Latin Modern fonts, provide a typewriter font with variable widths of the letters:

\usepackage[T1]{fontenc}
\usepackage[variablett]{lmodern}


Option variablett selects font family lmvtt. There is a drawback, the underscore is too wide with too less space inbetween:

That can be fixed using the literate feature of package listings, see example below.

## Indenting

The source code gets indented by accident, because the spaces at the start of the line in the source file. These spaces can be removed by option gobble to avoid nasty looking source files. In the output the source can be indented like other environments (quote, …) using option xleftmargin that can be set to \leftmargini, the indentation of top level lists (the next level is \leftmarginii, …). Also the right margin could be set to \leftmargini (LaTeX uses the same indentation for left and right), but I think, it is better to ignore the right indentation to avoid source lines that are broken across lines.

## Color green

The green is very bright and difficult to read on a white background. A little more darkness improves the readability. For instance, the listings package uses darkgreen for keys and commands in the reference guide:

\definecolor{darkgreen}{rgb}{0,0.5,0}


## Full example

documentclass{beamer}
\usepackage{listings}

\usepackage[T1]{fontenc}
\usepackage[variablett]{lmodern}

\newcommand*{\vttfamily}{%
\fontencoding{T1}\fontfamily{lmvtt}\selectfont
}

\newcommand*{\textsmallunderscore}{%
\begingroup
\fontencoding{T1}\fontfamily{lmtt}\selectfont
\textunderscore
\endgroup
}

\definecolor{darkgreen}{rgb}{0,.5,0}

\begin{document}

\begin{frame}[fragile]{CUDA C Concepts}
\textbf{\color{orange}Kernel} \textit{Def} C functions
which are executed N times in parallel by
\lstset{
language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
breaklines=true,
columns=flexible,
literate={_}{\textsmallunderscore}1,
gobble=4,
xleftmargin=\leftmargini,
}
\begin{lstlisting}
// Kernel Definition
__global__ void VecAdd(float* A, float* B, float* C)
{
C[i]  = A[i] + B[i];
}
int main(void)
{
...
// Kernel Invocation with N threads
return 0;
}
\end{lstlisting}
\end{frame}

\end{document}


-