16

I am trying to typset the following

\resizebox{0.95\textwidth}{!}{%
 \begin{verbatim}
  require( "lattice" )
  require( "grid" )
 \end{verbatim}
}

I have a lot more to add to this box, and it needs to be typset verbatim, but it gives me an error. Am i doing something wrong.

15

You can not use verbatim in the argument to another command.

\documentclass{article}
 \usepackage{graphicx}
\begin{document}
\newsavebox\myv

\begin{lrbox}{\myv}\begin{minipage}{\textwidth}
\begin{verbatim}
...
\end{verbatim}
\end{minipage}\end{lrbox}

\resizebox{0.95\textwidth}{!}{\usebox\myv}
\end{document}
  • this still gives a error, albeit a new one :). So I should put in everything i want into the lrbox and then typeset the document? – Sam Dec 13 '12 at 15:50
  • 6
    You should always include complete documents in the question. I just made a complete document in my answer, that should run without error, if yours does not there is a difference somewhere – David Carlisle Dec 13 '12 at 16:00
  • i accept my mistake. But thank you for answering the question. I think this solves the problem. – Sam Dec 13 '12 at 16:01
  • The reason verbatim cannot be used in the argument to another command is specific to this package, or is there a more general rule about LaTeX that I'm missing here? – astrojuanlu Mar 8 '16 at 10:21
  • 1
    @astrojuanlu verbatim (and all similar commands (verb, listings, minted, etc) can not be used in an argument as they affect the way characters are tokenized, but arguments are tokenized before the macro is expanded so changing the rules after that has no effect. How could it work otherwise. verbatim makes {} not special but in \foo{\begin{verbatim}....\end{verbatim}} you are relying on {} being parsed and matched with normal special meaning in order to find the closing }. – David Carlisle Mar 8 '16 at 10:28
7

If what you want is to reduce the size of the font used in verbatim, you can use the fancyvrb package:

\documentclass{article}
\usepackage{fancyvrb}

\begin{document}

\begin{Verbatim}[fontsize=\small]
This will be verbatim at \small size
\end{Verbatim}

\end{document}

If you also want to shift a bit the verbatim text, use also the key xleftmargin, for instance

\begin{Verbatim}[fontsize=\small,xleftmargin=\parindent]

If you say

\fvset{fontsize=\small,xleftmargin=\parindent}

all you need in the document is

\begin{Verbatim}

(but you can countermand the global settings by specifying other values in the optional argument).

5
\documentclass{article}
\usepackage{fancyvrb,graphicx}
\begin{document}

\begin{SaveVerbatim}{myVerb}
  require( "lattice" )
  require( "grid" )
\end{SaveVerbatim}
\resizebox{0.95\textwidth}{!}{\BUseVerbatim{myVerb}}%

\end{document} 
  • Your answer is much simpler, but i like the previous answer by David Carlisle, because it does not require me to use additional packages. I also think this solution is more generic. – Sam Dec 21 '12 at 15:48
  • This answer does exactly what I want and is clear and easy to understand. – Cameron Lowell Palmer May 9 '17 at 9:24
2

There is the verbatimbox package that puts verbatim in a box

\documentclass{article}
\usepackage{graphicx}
\usepackage{verbatimbox}
\begin{document}
 \begin{verbbox}
  require( "lattice" )
  require( "grid" )
 \end{verbbox}
\resizebox{0.95\textwidth}{!}{\theverbbox}
\end{document}

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.