6

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.

Martingal
  • 1,326
  • 3
  • 9
  • 11
  • 1
    That wouldn't be verbatim, which is about representing “exactly” the input. Can you make a more realistic example? – egreg Oct 13 '13 at 10:05
  • You guessed right; that's why I'm asking for a “real world” example, so we can point to a better solution for your needs. – egreg Oct 13 '13 at 10:11
  • Please, take a look at http://tex.stackexchange.com/questions/113906/format-a-verbatim-paragraph?rq=1 –  Oct 13 '13 at 10:21
  • I do not see how the link solve my issue. Could you please show me how it actually does? Thank you. (It looks way to complicated for the issue I am facing?) – Martingal Oct 13 '13 at 10:37
  • One solution I've used is to make the verbatim text in a smaller size, for example \scriptsize, so that the wider lines fit onto the width of the page. Of course, you have to consistently do this for all verbatim text throughout the document. – Steven B. Segletes Oct 13 '13 at 11:03
  • @StevenB.Segletes : even with \scriptsize, I need to have an end of line for some results I got, in my verbatim. – Martingal Oct 13 '13 at 11:21
  • @Martingal You may be wanting the alltt environment available with \usepackage{alltt}. The listing package allows for line breaks (option breaklines to \lstset). – egreg Oct 13 '13 at 12:20
  • I don't understand why you don't want to use the listings package when actually you're using it already. With listings it's easy to have different verbatim styles and it even allows to define separate environments for the different styles. – cgnieder Oct 13 '13 at 12:26
  • @egreg I tried for alltt but there was still no new line for a long string. @ cgnieder : I am looking for a simple solution, I hope there can be a verbatim function that enables the new line when a string is too long. – Martingal Oct 13 '13 at 12:39
  • @Martingal I insist for an example of what you mean. Without that only generic advice can be given. – egreg Oct 13 '13 at 12:45
  • @egreg : I edited my topic. Could you please remove the first comments so that it remains clear ? (Thanks for the help your are providing me) – Martingal Oct 13 '13 at 13:15
  • @Martingal Are you *really* writing a long string like that, with all a's? I can hardly believe it. – egreg Oct 13 '13 at 13:15
  • @egreg Yes, it's a very specific result from a command line. A very long string in that way (even with special chars). – Martingal Oct 13 '13 at 13:17
  • @Martingal Last attempt. Can you show a *real* example of what you have to typeset? Splitting a long string of a's is trivial, but it would be useless to show you how to do it, since it's quite improbable you need to typeset such a string. – egreg Oct 13 '13 at 13:19
  • @egreg I see what you mean now. I edited my topic, so that the long string I want to split is shown. I hope it will be clear enough now, sorry for the multi-edits. – Martingal Oct 13 '13 at 13:33
  • But listings is simple: \begin{lstlisting}[basicstyle=\ttfamily, breaklines=true] and since you say you're already using the package... – cgnieder Oct 13 '13 at 13:44
  • 1
    I really do not understand WHY it is considered as duplicate ! I don't understand what is the link between these 2 topics! I think it is quite unfair. – Martingal Oct 13 '13 at 14:05

1 Answers1

7

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

egreg
  • 1,121,712
  • 1
    THANK YOU VERY MUCH FOR YOUR HELP AND YOUR CONTINUOUS SUPPORT. It works pretty good! Enjoy your sunday, and thank you again ;) – Martingal Oct 13 '13 at 14:40