8

In order to produce an ereader-friendly PDF with small page dimensions, I'd like to scale down some oversize verbatim blocks such as program output with lines too long for the small page. Then readers should be able just to zoom in when needed. This simple test using adjustbox and the BVerbatim environment from fancyvrb works fine:

\documentclass[12pt,a4paper]{article}
\usepackage{adjustbox}
\usepackage{fancyvrb}
\begin{document}
\begin{adjustbox}{max width=\linewidth}
\begin{BVerbatim}
a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line
\end{BVerbatim}
\end{adjustbox}
\end{document}

However, I'm having a bit of trouble with wrapping the adjustbox+verbatim combo in a reusable environment. In particular, this attempt:

\documentclass[12pt,a4paper]{article}
\usepackage{adjustbox}
\usepackage{fancyvrb}
\newenvironment{myverb}{
\begin{adjustbox}{max width=\linewidth}
\begin{BVerbatim}
}{
\end{BVerbatim}
\end{adjustbox}
}
\begin{document}
\begin{myverb}
a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line
\end{myverb}
\end{document}

produces this error:

Runaway argument?
! File ended while scanning use of \FancyVerbGetLine.
<inserted text> 
                \par 

And this attempt:

\documentclass[12pt,a4paper]{article}
\usepackage{adjustbox}
\usepackage{verbatim}
\newenvironment{myverb}{%
\adjustbox{max width=\linewidth}%
\verbatim
}{%
\endverbatim
\endadjustbox
}
\begin{document}
\begin{myverb}
a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line
\end{myverb}
\end{document}

results in this error:

! LaTeX Error: Something's wrong--perhaps a missing \item.

Do you think what I need can be achieved using available LaTeX packages and tools?

Red
  • 10,181
Yar
  • 183

1 Answers1

10

Defining verbatim environments are very special. You can find similar question here at TeX.SX as well as in the FAQ.

However the trick here is to tell LaTeX that the next definition of the environment is a verbatim environment. This can be achieved by the command \VerbatimEnvironment.

\documentclass[12pt,a4paper]{article}
\usepackage{adjustbox}
\usepackage{fancyvrb}
\newenvironment{myverb}{%
 \VerbatimEnvironment
 \begin{adjustbox}{max width=\linewidth}
 \begin{BVerbatim}
  }{
  \end{BVerbatim}
 \end{adjustbox}
}
\begin{document}
\begin{myverb}
a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line a very long line
\end{myverb}
\end{document}
David Carlisle
  • 757,742
Marco Daniel
  • 95,681
  • Marco: Thank you so much for the clue. Before asking my question I did the homework and examined various online sources including TeX.SX but never saw a reference to the magic \VerbatimEnvironment command, which doesn't seem too well known but works like a charm! – Yar Sep 20 '13 at 09:59
  • By the way, the only conventional documentation touching upon \VerbatimEnvironment is that for fancybox. Go figure! – Yar Sep 20 '13 at 10:17
  • What this does, if I understand correctly, is reduce the font until the text fits within the set width. It does not, however, force automatic linebreaks (which is the feature I was looking for). – PatrickT May 02 '16 at 14:36
  • Here are some related questions (about line breaking rather than width): http://tex.stackexchange.com/questions/11973/verbatim-environment-and-line-breaking and http://tex.stackexchange.com/questions/86899/how-can-i-have-a-verbatim-text-in-specific-sized-box – PatrickT May 02 '16 at 14:52
  • how to scale a simple \begin{verbatim} environment. nothing fancy command etc. Just scale the verbatim environemnt? – droid192 Apr 29 '19 at 22:50