4

I'd like to specify the dimensions of a boxed verbatim environment by providing the width in characters (e.g. to fit terminal dimensions).

Is that possible? A solution using fancyvrb would be preferred.

1
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. A suggestion: Do us a favour and change your username to something more telling than "user1234". Commented Mar 10, 2017 at 8:28

2 Answers 2

4

Since the normal verbatim environment doesn't do linebreaks at to much width the easiest solution would be to insert manual linebreaks in a verbatim environment:

\documentclass[]{scrartcl}

\begin{document}
\begin{verbatim}
This is an
awful lot of verbatim text, just to check whether the verbatim
environment linebreaks or not if the total width of the text is to big (which it doesn't at all)
\end{verbatim}
\end{document}

first code snippets results

Another approach would be to use a modified verb-command and putting it into a minipage (modification taken from here)

\documentclass[]{scrartcl}

\makeatletter
\let\old@sverb\@sverb
\def\@sverb#1{\old@sverb{#1}\zz}
\def\zz#1{#1\ifx\@undefined#1\else\penalty\z@\expandafter\zz\fi}
\makeatother

\setbox1\hbox{\verb|l|}
\newlength{\vcharwidth}
\setlength{\vcharwidth}{\wd1}

\begin{document}

\raggedright
\begin{minipage}[t]{38\vcharwidth}
    \verb|lllllllllllllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|
\end{minipage}

\verb|`?<<---|

\verb|`?<<---ooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|

\end{document}

results of the second code

I don't know fancyvrb so I can't help you with that.

EDIT: If a frame is desired around the verbatim text one could use the framed-package:

\documentclass[]{scrartcl}

\usepackage{framed}

\makeatletter
\let\old@sverb\@sverb
\def\@sverb#1{\old@sverb{#1}\zz}
\def\zz#1{#1\ifx\@undefined#1\else\penalty\z@\expandafter\zz\fi}
\makeatother

\setbox1\hbox{\verb|l|}
\newlength{\vcharwidth}
\setlength{\vcharwidth}{\wd1}

\begin{document}

\raggedright
\begin{minipage}[t]{\dimexpr38\vcharwidth+2\FrameSep+1pt}%the width has to be adapted to include the framebox around the desired character count
\begin{framed}
    \verb|lllllllllllllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|
\end{framed}
\end{minipage}
\end{document}
3

You can base the new environment on BVerbatim:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{lipsum}

\newenvironment{FVerbatim}
 {\VerbatimEnvironment
  \begin{lrbox}{\FVerbatimbox}%
  \begin{BVerbatim}}
 {\end{BVerbatim}\end{lrbox}%
  \framebox[\FVerbatimwidth][l]{\usebox{\FVerbatimbox}}}

\newsavebox{\FVerbatimbox}
\newlength{\FVerbatimwidth}
\AtBeginDocument{%
  \sbox0{\ttfamily X}%
  \setlength{\FVerbatimwidth}{60\wd0}%
  \addtolength{\FVerbatimwidth}{2\fboxsep}% for framebox
}

\begin{document}

\lipsum[2]
\begin{center}
\begin{FVerbatim}
123456789012345678901234567890123456789012345678901234567890
\newenvironment{FVerbatim}
 {\VerbatimEnvironment
  \begin{lrbox}{\FVerbatimbox}%
  \begin{BVerbatim}}
 {\end{BVerbatim}\end{lrbox}%
  \makebox[\FVerbatimwidth][l]{\usebox{\FVerbatimbox}}}

\newsavebox{\FVerbatimbox}
\newlength{\FVerbatimwidth}
\AtBeginDocument{%
  \sbox0{\ttfamily X}%
  \setlength{\FVerbatimwidth}{60\wd0}%
  \addtolength{\FVerbatimwidth}{2\fboxsep}% for framebox
}
\end{FVerbatim}
\end{center}
\lipsum[3]

\end{document}

enter image description here

4
  • Your code doesn't linebreak the entered stuff if the width would be wider than the maximum width (resulting in ugly overflow outside of the framed box). I don't know whether this was desired or not by OP.
    – Skillmon
    Commented Mar 10, 2017 at 15:19
  • @Skillmon That's expected. One needs a different approach; I'll add something later, but the specifications are a bit scanty. Is a frame, centering, page breaking needed?
    – egreg
    Commented Mar 10, 2017 at 15:21
  • My specifications? If you mean my answer could you please elaborate? I am not the questioner.
    – Skillmon
    Commented Mar 10, 2017 at 15:23
  • @Skillmon Oh, sorry!
    – egreg
    Commented Mar 10, 2017 at 15:25

You must log in to answer this question.

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