[Tex/LaTex] Redefine verbatim environment

environmentsmacrospandocverbatim

I have a text with source code, which I would like to display. I am using pandoc, so I have to use either verbatim or listings environment. Unfortunately, I can't use listings package (due to Cyrillic symbols issue). So, I stuck with verbatim only. But verbatim is unable to make word wrap. I found spverbatim package which is suited to my needs, and now I would like to redefine verbatim enviroment.

1) At first, I've tried to redefine it with let command:

\let\verbatim\spverbatim

But it just doesn't compile, with no errors. Looked like the whole compiling process was hanged up.

2) Then, I've tried to use renewenvironment command:

\renewenvironment{verbatim}{\begin{spverbatim}}{%
   \end{spverbatim}\ignorespacesafteren
}

and got an error:
"! TeX capacity exceeded, sorry [save size=80000].
\reserved@a …tim}\edef \@currenvline {\on@line }
\csuse {@begin@spverbatim@…
l.372 \begin{verbatim}"

Is there any way to redefine build in environments like verbatim or I am doing something totally wrong?

Best Answer

You may use the same idea as spverbatim; I also added \raggedright or several overfull boxes would be expected.

\documentclass{article}

\makeatletter
\def\@xobeysp{\mbox{}\space}
\def\verbatim@font{\normalfont\ttfamily\raggedright}
\makeatother

\begin{document}
\begin{verbatim}
Something which will break but is otherwise typeset @#verbatim#} with special characters accessible
\end{verbatim}
\end{document}

enter image description here

If you plan to use verbatim in lists, a small change is needed (thanks to Christof R for noting):

\documentclass{article}

\makeatletter
\def\@xobeysp{\mbox{}\space}
\def\verbatim@font{\normalfont\ttfamily\raggedright\leftskip\@totalleftmargin}
\makeatother

\begin{document}
\begin{verbatim}
Something which will break but is otherwise typeset @#verbatim#} with special characters accessible
\end{verbatim}

\begin{itemize}
\item Text
\begin{verbatim}
Something which will break but is otherwise typeset @#verbatim#} with special characters accessible
\end{verbatim}

\begin{itemize}
\item Text
\begin{verbatim}
Something which will break but is otherwise typeset @#verbatim#} with special characters accessible
\end{verbatim}
\end{itemize}

\item Text
\end{itemize}
\end{document}

enter image description here