1

I use both \begin{verbatim} ... \end{verbatim} and \verb.something. in my document. I would like the contents of \begin{verbatim} ... \end{verbatim} to be displayed in a smaller font. I found this, which is kind of what I want. In that answer, the verbatim font can be customized via this command:

\makeatletter
\newcommand{\verbatimfont}[1]{\renewcommand{\verbatim@font}{\ttfamily#1}}
\makeatother

But I would prefer this to be applied to every verbatim environment automatically.

Then I tried to transfer this answer to my problem, but this also makes the contents of \verb.someting. small, which is not what I want.

I also tried defining a custom verbatim environment like this:

\newenvironment{smallverbatim}{
\verbatimfont{\footnotesize}\noindent\ignorespaces\begin{verbatim}}%
{\par\noindent%
\ignorespacesafterend\end{verbatim}}

But this gave me:

! LaTeX Error: \begin{smallverbatim} on input line 526 ended by \end{itemize}.

Is there a way to do what I want to do?

What I need is some kind of macro that replaces every \begin{verbatim} with \verbatimfont{\footnotesize}\begin{verbatim}, so it shouldn't be impossible, but I have no idea how to do it.

fifaltra
  • 1,977

1 Answers1

3

LaTeXs verbatim environment can not be used in \newenvironment, because it looks for \end{verbatim} in the source.

But there are many packages with improved verbatim capabilities. One of them (verbatim) is used here. Among others, it enables you to define your own verbatim environments.

Here is an example:

\documentclass{article}
\usepackage{lipsum} % just for the example
\usepackage{verbatim}

\newenvironment{smallverbatim}%
{\small\verbatim}%
{\endverbatim}

\begin{document}

Test: \verb|something| \lipsum*[3]
\begin{smallverbatim}
1   double x, y;
2   double z, w;
3   main();
4   return 0;
\end{smallverbatim}
Test: \verb|something| \lipsum[3]

\begin{smallverbatim}
1   double x, y;
2   double z, w;
3   main();
4   return 0;
\end{smallverbatim}
Test: \verb|something| \lipsum[3]

\end{document}
TeXnician
  • 33,589
Mike
  • 8,664