How can I modify the following command to make it work?

\newcommand{\code}[1]{\begingroup \fontsize{10pt}{10pt}\selectfont \begin{verbatim} #1 \end{verbatim} \endgroup}

I currently get the following error on the close of the following verbatim block:

! Undefined control sequence.
<argument> ...endgroup ^^M^^M$\eta_{xi} = r_{xi} -
                                                   q_i \cdot p_x$ \\^^M$q_i ...
l.227     \end{verbatim}

migrated from stackoverflow.com Feb 21 '14 at 16:49

This question came from our site for professional and enthusiast programmers.


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.


This is text to show the font size
This is smaller
Some other text

Explanation: \verbatim@font selects the typewriter type font; by adding \small we force it to another size.

enter image description here

Just to make clearer that it works, I repeat the output with \tiny instead of \small:

enter image description here


You can also place the verbatim environment inside a small environment:


Source: https://www.namsu.de/latex/kapitel5.html

  • 2
    Not really. Without a blank line before \begin{small} you get wrong line spacing in the text above the verbatim material. – egreg Nov 2 '18 at 11:52
  • 1
    Also small is not an environment... – Andrew Swann Nov 2 '18 at 13:04

The verbatimbox package can take optional arguments, such as \fontsize.

Thi$ i$ my \/erbatim
Thi$ i$ my \/erbatim

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.