[Tex/LaTex] Formating verbatim (not using listing package)

formattingverbatim

Considering the following verbatim:

\begin{verbatim}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
\end{verbatim}

How to make sure that the verbatim text is not going out of the page, but going on the following line as soon as the width of the page has been passed?

Please note that I am looking for a solution without the listing package.


EDIT (to describe my problem a bit more)

I am having a long report in which I use a lot verbatim. From time to time, the text of the verbatim is going out of the page. Furthermore, I do use the listing package with box for some special programming code. I would like to keep the same verbatim format, but with auto "end of line" for the verbatim code. Is there a simple way for doing it?


EDIT 2

if you take the following verbatim with a very long string :

\begin{verbatim}
$ mylinuxcommand 
!+.++++.....+++-+++-!^!^+............................................;+++-^^!--.........+++--!^!^+.++++.....+++-+++-^!^+............................................;+++-^^!--.........+++--!^!^+.++++.....+++-+++-!^!^+............................................;+++-^^!--.........+++--!^!^
$ myotherlinuxcommand
true
$ mylastcommand
"Thank you for your help"
\end{verbatim}

On your pdf, only a part of this string is displayed (It stays on the same line, it's "continuing" out of the page). As on below (pdf dislay) :

$ mylinuxcommand

!+.++++…..+++-+++-!^!^+……………………………………..;+++-^^!-

$ myotherlinuxcommand

true

$ mylastcommand

"Thank you for your help"

I wish it could go on the next line automatically, so that all the content of this long string is displayed. The displayed result would give, on the pdf (pdf display):

$ mylinuxcommand

!+.++++…..+++-+++-!^!^+………………………………….

….;+++-^^!–………+++–!^!^+.++++…..+++-+++-^!^+……

……………………………….;+++-^^!–………+++–

!^!^+.++++…..+++-+++-!^!^+……………………………

………..;+++-^^!–………+++–!^!^

$ myotherlinuxcommand

true

$ mylastcommand

"Thank you for your help"

So that the entire long string from the verbatim is displayed on the pdf. A long string should be automatically cut when it reaches the width of the page, and the rest recursivally displayed on the other line. Please note that this long string can change, but globally the structure and chars used remain the same.

Best Answer

You can do with the help of the verbatim command and some low level hackery:

\documentclass{article}
\usepackage{verbatim}
\newenvironment{spverbatim}
 {\verbatim\splitchars}
 {\endverbatim}
\newcommand{\splitchars}{%
  \definesplitchar{\!}%
  \definesplitchar{\.}%
  \definesplitchar{\+}%
  \definesplitchar{\-}%
  \definesplitchar{\^}%
  \definesplitchar{\;}%
}
\newcommand{\definesplitchar}[1]{%
  \begingroup\lccode`~=`#1\relax
  \lowercase{\endgroup\def~}{\char`#1\hspace{0pt plus 0.1pt minus 0.1pt}}%
  \catcode`#1=\active
}

\begin{document}
\begin{spverbatim}
$ mylinuxcommand 
!+.++++.....+++-+++-!^!^+............................................;+++-^^!--.........+++--!^!^+.++++.....+++-+++-^!^+............................................;+++-^^!--.........+++--!^!^+.++++.....+++-+++-!^!^+............................................;+++-^^!--.........+++--!^!^
$ myotherlinuxcommand
true
$ mylastcommand
"Thank you for your help"
\end{spverbatim}
\end{document}

enter image description here