Decreasing font size in verbatim
listings
is the de-facto standard for typesetting of code these days and offers syntax highlighting and font selection, amongst other things; fancyvrb
might be easier to handle for your use case.
To answer your more immediate question: you can't put the argument of a command into a verbatim
environment, verbatim
needs to change the way text is read and it's too late for that then.
You can use the verbatim
environment; just patch it so that it selects a smaller font. In the example I use \small
which is 9pt, 10pt or 11pt when the main font size is 10pt, 11pt or 12pt respectively.
Instead of \small
you can substitute any other \fontsize{X}{Y}\selectfont
instruction, but keep in mind that Y
stands for the baseline skip, which should be larger than the font size X
.
\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@verbatim}
{\verbatim@font}
{\verbatim@font\small}
{}{}
\makeatother
\begin{document}
This is text to show the font size
\begin{verbatim}
This is smaller
\end{verbatim}
Some other text
\end{document}
Explanation: \verbatim@font
selects the typewriter type font; by adding \small
we force it to another size.
Just to make clearer that it works, I repeat the output with \tiny
instead of \small
:
The verbatimbox
package can take optional arguments, such as \fontsize
.
\documentclass{article}
\usepackage{verbatimbox}
\begin{document}
\begin{verbnobox}[\fontsize{8pt}{8pt}\selectfont]
Thi$ i$ my \/erbatim
text
\end{verbnobox}
\begin{verbnobox}[\fontsize{12pt}{12pt}\selectfont]
Thi$ i$ my \/erbatim
text
\end{verbnobox}
\end{document}