# Change font size of the verbatim environment

I want to apply small fonts inside the Verbatim environment e.g.

{\small
\begin{Verbatim}

1   double x, y;
2   double z, w;
3   main();
4   return 0;

\end{Verbatim}}


Before the command \small and after the closing braket } text has normal size, which is obvious but the font size should be small inside the brakets.

Why is this command not applied to the environment's contents?

-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. The environment Verbatim resets the font size to \normalsize. –  strpeter Apr 16 '14 at 11:10
This is not a duplicate of tex.stackexchange.com/questions/120633/… because changing size has different problems than changing typeface. –  egreg Apr 16 '14 at 16:41

You appear to be using fancyvrb, which already has the feature you want:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{lipsum} % just for the example
\begin{document}

\lipsum*[3]
\begin{Verbatim}[fontsize=\small]
1   double x, y;
2   double z, w;
3   main();
4   return 0;
\end{Verbatim}
\lipsum[3]

\end{document}


-

The macros of the verbatimbox package can take things like \small as an optional argument. In this case, I chose the verbnobox environment, but there are others in that package than may better suit your need.

\documentclass{article}
\usepackage{verbatimbox}
\begin{document}
\noindent Normal Size
\begin{verbnobox}[\small]

1   double x, y;
2   double z, w;
3   main();
4   return 0;

\end{verbnobox}
Normal Size
\end{document}


Furthermore, you could ask the package to number the lines for you, instead of you doing it manually:

\documentclass{article}
\usepackage{verbatimbox}
\begin{document}
\noindent Normal Size
\begin{verbnobox}[\tiny\arabic{VerbboxLineNo}\small\hspace{3ex}]
double x, y;
double z, w;
main();
return 0;
\end{verbnobox}
Normal Size
\end{document}


-

As mentioned in Change typeface of verbatim environment, the default font used in verbatim is \ttfamily, as set in \verbatim@font. You can create a user interface to adjust this font in the following way:

\documentclass{article}
\usepackage{lipsum} % just for the example
\makeatletter
\makeatother
\begin{document}

\lipsum*[3]
\verbatimfont{\small}%
\begin{verbatim}
1   double x, y;
2   double z, w;
3   main();
4   return 0;
\end{verbatim}
\lipsum[3]

\end{document}

-

I would recommend the package listings, which is ment for this purpose. See the page on Wikibooks for simple explanations about settings and my MWE below:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\lstset{ %
language=C++,           % choose the language of the code
numbers=left,           % where to put the line-numbers
numberstyle=\tiny,      % the size of the fonts that are used for the line-numbers
basicstyle=\footnotesize    % the size of the fonts that are used for the line-numbers
}

\begin{document}
Lorem ipsum
{\footnotesize Lorem ipsum
\begin{lstlisting}
double x, y;
double z, w;
main();
return 0;
\end{lstlisting}
Lorem ipsum}
Lorem ipsum
\end{document}


I replaced \small by \footnotesize in order to emphasize the different sizes:

-