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.

4
  • The \verbatimfontcommand sets the font for all subsequent verbratim environments. So simply putting it in the preamble will do what you want.
    – Mike
    May 9, 2018 at 20:51
  • But that also affects the contents of \verb.foo. which is not what I want...
    – fifaltra
    May 9, 2018 at 21:02
  • Oops, sorry. But I found an answer.
    – Mike
    May 9, 2018 at 21:31
  • Not sure the duplicate is really a duplicate, because the answers there change \verb.foo. too, as far as I can see
    – fifaltra
    May 10, 2018 at 10:40

1 Answer 1

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}

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