42

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?

2
  • 2
    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
    Commented Apr 16, 2014 at 11:10
  • This is not a duplicate of tex.stackexchange.com/questions/120633/… because changing size has different problems than changing typeface.
    – egreg
    Commented Apr 16, 2014 at 16:41

4 Answers 4

50

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}

enter image description here

1
  • 11
    For those using \begin{verbatim} ... \end{verbatim}, notice that here Verbatim is written with a capital V.
    – Qaswed
    Commented Nov 9, 2018 at 19:54
18

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:

enter image description here

\documentclass{article}
\usepackage{lipsum} % just for the example
\makeatletter
\newcommand{\verbatimfont}[1]{\renewcommand{\verbatim@font}{\ttfamily#1}}
\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}
11

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}

enter image description here

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}

enter image description here

10

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:

Output contents

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .