2

I'm trying to do automatic linebreaking right now with verbatim, and I have this setup.

\usepackage{fancyvrb}
\usepackage{fvextra}


\begin{Verbatim}[breaklines=true,numbers=left,breakafter=\},breakbefore=\\]
text text text text text \macro{text}
\end{Verbatim}

The setup I have right now is a tad cumbersome, and what I'm trying to do right now is code a macro that would let me put in all of that automatically, something like this:

\newcommand{\bigvrb}[1]{
\begin{Verbatim}[breaklines=true,numbers=left,breakafter=\},breakbefore=\\]
#1
\end{Verbatim}}

so that I can do everything a tad easier and, if I want to change the format, I can edit it en masse. Problem is, it just typesets this as #1 because that's how verbatim works. I've tried using \expandafter and things like that but nothing's really working. Do y'all have any ideas? Thanks so much!!!

5
  • 2
    You cannot have a verbatim environment as the argument to another command.
    – egreg
    Jul 19 at 19:39
  • 1
    If you don't need the full verbatim capability (e.g., if you don't need % and if all your braces will be balanced), then \detokenize could work for you, with automatic line breaking. The % limitation can be overcome with a catcode change, but not the balanced brace requirement. Jul 20 at 14:54
  • 1
    @StevenB.Segletes Be aware that \detokenize doubles hashes and inserts spaces behind control-word-tokens. Besides this you may also need a catcode-change of the space-character and the horizontal-tab-character and the ^ as otherwise consecutive spaces collapse into a single space and ^^-notation still works. Try, e.g.: \documentclass{article}\begin{document}\expandafter\texttt\expandafter{\detokenize{\TeX \TeX\TeX# A B^^JC}}\end{document} Jul 20 at 15:04
  • 1
    @UlrichDiez You make excellent points. Perhaps then, my \altdetokenize at tex.stackexchange.com/questions/34580/escape-character-in-latex/… can overcome a number of those issues. Jul 20 at 15:18
  • @StevenB.Segletes I am aware of your tokcycle-package and I like it. :-) In my answer to the question "How to \futurelet the token after a space" a \romannumeral-expansion-based macro \StringifyNAct is provided which applies \string to each token of its argument and hereby takes brace-nesting into account. Perhaps a similar routine can be applied to a macro-argument that got tokenized while space and horizontal tab and carriage return and ^ had catcode 12. Jul 20 at 15:38
9

The fancyvrb package allows you to define your own environments. If you use this you can just change the definition:

\documentclass[]{article}

\usepackage{fancyvrb}
\usepackage{fvextra}

\DefineVerbatimEnvironment{MyVerbatim}{Verbatim}{breaklines=true,numbers=left,breakafter=\},breakbefore=\\}

\begin{document}

\begin{MyVerbatim}
text text text text text \macro{text}
\end{MyVerbatim}

\end{document}
1
3

I cannot recommend using a macro with undelimited-argument-syntax for producing multiline-verbatim-material:

The question of handling indentation of the source code will cause confusion because with verbatim-material indentation-spaces in the source-code are tokenizend and therefore show up as horizontal space/visible space in the output.

In order to exhibit some problems/weirdnesses the following example provides a macro \bigvrb—the option showspaces=true is applied so you can better see the treatment of spaces that are intended to be indentation for just making the source code more readable:

\RequirePackage{xparse}
\documentclass[a4paper]{article}

\usepackage{fancyvrb}
\usepackage{fvextra}

\NewDocumentCommand\bigvrb{}{%
  \begingroup
  \catcode`\^^I=12 %
  %\endlinechar=`\^^M
  \newlinechar=\endlinechar
  \innerbigvrb
}%
\begingroup
\catcode`\^^A=14\relax
\catcode`\%=12\relax
\catcode`\{=12\relax
\catcode`\}=12\relax
\catcode`\(=1\relax
\catcode`\)=2\relax
\catcode`\/=0\relax
\catcode`\\=12\relax
/catcode`/^^M=12/relax^^A
/csname @firstofone/endcsname(^^A
  /endgroup^^A
  /NewDocumentCommand/innerbigvrb(+v)(^^A
    /scantokens(^^A
      \endgroup^^A
      \begin{Verbatim}[showspaces=true, breaklines=true,numbers=left,breakafter=\},breakbefore=\\]^^M^^A
      #1^^M^^A
      \end{Verbatim}^^M%^^A
    )^^A
  )^^A
)%

\pagestyle{empty}
\parindent=0ex
\parskip=.25\baselineskip

\begin{document}
\enlargethispage{9cm}\vspace*{-4.5cm}%

\noindent\hrulefill

This looks okay:

\bigvrb{ABC
DEF}

\noindent\hrulefill

This has a visible comment-char and the spaces for indenting the code are visible also:

\bigvrb{%
  ABC%
  DEF%
}
\noindent\hrulefill

The spaces for indenting the code are visible. Besides this you have four
linebreaks \emph{inside} the verbatim-argument---the linebreak before the
first line (\verb*|  ABC|) and the linebreak after the last line
(\verb*|  DEF|) each yields an empty line:

\bigvrb{
  ABC
  DEF
}

\noindent\hrulefill

Even more spaces are used for indenting the code. They all are visible:

\begin{itemize}
\item Some text of an item of an \verb|itemize|-environment.
      That item also contains a \verb|\bigvrb|-command whose
      result is here:
      \bigvrb{ABC
              DEF}
\end{itemize}

\noindent\hrulefill

Now let's have fun creating two \verb|Verbatim|-environments in a weird way, keeping in mind that
curly braces must be balanced with \verb|\bigvrb|'s argument if not using delimiter-syntax:

\let\MyClosebrace=\}
\bigvrb{First environment
\end{Verbatim}

\begin{Verbatim}[showspaces=true, breaklines=true, numbers=left, breakafter=\MyClosebrace, breakbefore=\\]
Second environment}

\noindent\hrulefill

Now let's have fun creating two \verb|Verbatim|-environments in a weird way,
using delimiter-syntax---as verbatim-arg-delimiter \verb|>| is chosen:

\bigvrb>First environment
\end{Verbatim}

\begin{Verbatim}[showspaces=true, breaklines=true, numbers=left, breakafter=\}, breakbefore=\\]
Second environment>


\noindent\hrulefill

\textbf{Summa summarum:}

Undelimited-argument-syntax with multiline-verbatim-material is
confusing as 
\begin{itemize}
\item you cannot have the usual code-indentation and linebreaking.
\item you cannot easily use the command \verb|\bigverb|
for verbatimized typesetting of the line \verb|\end{Verbatim}|.
\item you may need to take care of braces being balanced in
\verb|\bigvrb|'s argument if you don't use delimiter-syntax.
\end{itemize}

\end{document}

enter image description here

Of course \bigvrb of the example above is intended to obtain its argument by reading and tokenizing .tex-input after (temporarily) switching to verbatim-catcode-régime.

Therefore \bigvrb does not work in situations where it obtains its argument

  • by having it passed as part of a macro-definition which got tokenized under non-verbatim-catcode-régime
  • by having it passed from another macro during whose carrying-out the argument got tokenized under non-verbatim-catcode-régime
  • by having it passed from a token-register via \the-expansion when the content of that register was tokenized under non-verbatim-catcode-régime
  • etc.

Using it as component of moving arguments might yield unexpected effects.

As a rule of thumb you can use \bigvrb within the text of the document but you cannot use it within macro-definitions/within the argument of other macros. Thus it is of very limited use only.

2

This tokcycle based approach is set up to handle tokens (and groups) in the input stream in a successive manor. It can thus be programmed to emulate verbatim in most ways (the exception being that the grouping tokens {} must be balanced)

But as we see in the MWE, special characters like {#$%^&\_} are all handled. I have further set up the cycle to interpret paragraphs as \par but to interpret \\ linebreaks as actual linebreaks. These behaviors of course can be reprogrammed as desired. In the MWE, the space token defaults to a space, but the optional argument can be used to set it to something else, for example, a visible space.

Importantly, this \altdetokenize eliminates the ugly space following macro names, except, of course, when the macro is followed by an alphabetic character, where the space must be retained.

This approach does not support line numbering.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tokcycle}
\newif\ifmacro
\newcommand\altdetokenize{\begingroup\catcode`\%=12\altdetokenizeAUX}
\newcommand\altdetokenizeAUX[2][ ]{\stripgroupingtrue\macrofalse
  \stripimplicitgroupingcase{-1}%
  \tokcycle
    {\ifmacro\def\tmp{##1}\ifcat\tmp A\else\unskip\allowbreak\fi\macrofalse\fi
     \detokenize{##1}\ifx##1\bgroup\unskip\fi\ifx##1\egroup\unskip\fi}
    {\ifmacro\unskip\macrofalse\fi\{\processtoks{##1}\ifmacro\unskip\fi\}\allowbreak}
    {\tctestifx{\\##1}{\\}{\ifmacro\unskip\allowbreak\fi
     \allowbreak\detokenize{##1}\macrotrue}}
    {#1\hspace{0pt plus .3em minus .3ex}}
    {#2}%
  \unskip
\endgroup}

\begin{document}
\altdetokenize{This is a \relax test to check
  out line breaking and verbatim {#$%^&\_} as well as that ugly
  space after \macros. Like \a\b\c

And what about paragraphs?\\
And linebreaks?
}

\altdetokenize[\textvisiblespace]{This is a \relax test to check
  out line breaking and verbatim {#$%^&\_} as well as that ugly
  space after \macros. Like \a\b\c

And what about paragraphs?\\
And linebreaks?
}
\end{document}

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.