I would like to enable automatic line breaking in Verbatim environment; besides I do not want to use lstlisting as it will not have exactly the same font shapes as Verbatim and some other special characters used in Logic Programming like \+ does not show the same. I should only use Verbatim environment but it seems that the option of line breaking is not available. fancyvrb also doesn't work as we need to know when we want the line to break. I need this to be automatic.


Here a small hack. It saves the contents of a verbatim line inside a vertical box which allows line breaks:


\box_new:N \l_fvrb_box
\tl_new:N \l_fvrb_tl

\RenewDocumentCommand \FancyVerbFormatLine { m }
   \hbox_set:Nn \l_fvrb_box { #1 }
    \dim_compare:nNnTF { \box_wd:N \l_fvrb_box }>{ \linewidth }
      {%box to big 
       \fvrb_use_linecontents:n { #1 }
      {%box fits
       \box_use:N \l_fvrb_box

\cs_new:Npn \fvrb_use_linecontents:n  #1
   \vbox_set:Nn \l_fvrb_box
       \advance\hsize by -2em
    \hspace*{2em}\box_use:N \l_fvrb_box


asdsadsadsa asd  a

asdsadsadsa asd  asd  asd sa sd sd as  sdadas sa+ dsa +sa++94+4+ +4sd+as 

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.