7

After some reading I thought the listings package would be able to handle this, but I'm not having any success. I have some longish code strings that I need to include in the paragraphs of the document, and I want these to break over lines, or to simply leave white space and put on a new line, as needed. Currently, I can only convince the listings package to line break when there is a space in the code string.

\documentclass{article}
\usepackage{listings}
\lstset{breaklines=true}
\begin{document}
\verb|lllllllllllllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|

\lstinline|lllllllllllllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|

\lstinline|lllllllllllllllllllllloooooooooooooooooonnnn nnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnn nnnnnnnngggggggg$$$$$$$|
\end{document}
  • If your code is not from a file you might give \texttt{} a shot (though it doesn't handle stuff like $ and the like). – Skillmon likes topanswers.xyz Mar 8 '17 at 11:42
7

enter image description here

You can iterate through the verb, adding a breakpoint after each character.

\documentclass{article}

\makeatletter
\let\old@sverb\@sverb
\def\@sverb#1{\old@sverb{#1}\zz}
\def\zz#1{#1\ifx\@undefined#1\else\penalty\z@\expandafter\zz\fi}
\makeatother

\begin{document}


\raggedright

\verb|lllllllllllllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|

\verb|`?<<---|

\verb|`?<<---ooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|

\end{document}
| improve this answer | |
  • Great. This also works fine once I include all my other formatting packages, verbatim in footnotes etc. – Charlie Mar 8 '17 at 14:22
4

A solution with xparse:

\documentclass[draft]{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\splitverb}{v}
 {
  \group_begin:
  \use:c { verbatim@font }
  \seq_set_split:Nnn \l_tmpa_seq {} {#1}
  \seq_use:Nn \l_tmpa_seq { \hspace{0pt plus .2em}\penalty\c_zero\hspace{0pt plus -.2em} }
  \group_end:
 }
\ExplSyntaxOff

\begin{document}

Some text \splitverb|lllllllllllllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|

Let's see that ligatures are not formed: \splitverb|`?<<---|

\end{document}

The combination of \hspace and \penalty ensures you don't get overfull boxes.

enter image description here

You may want to add some glue with small flexibility instead.

\documentclass[draft]{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\splitverb}{v}
 {
  \group_begin:
  \use:c { verbatim@font }
  \seq_set_split:Nnn \l_tmpa_seq {} {#1}
  \seq_use:Nn \l_tmpa_seq { \hspace{0pt plus 0.1pt} }
  \group_end:
 }
\ExplSyntaxOff

\begin{document}

Some text \splitverb|lllllllllllllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|

Let's see that ligatures are not formed: \splitverb|`?<<---|

\end{document}

enter image description here

| improve this answer | |
  • Problems arose with this once I included it in my full document, so I accepted the seemingly more robust (in terms of package / formatting interactions) approach from David Carlisle. – Charlie Mar 8 '17 at 14:25
1

You could create a macro that typesets each character on its own using \string:

\documentclass{article}

\makeatletter
\def\mycode|#1#2|{%
    \texttt{\string#1}\penalty\z@%
    \ifx&#2&%
    \else%
        \mycode|#2|%
    \fi%
}
\makeatother

\begin{document}
\mycode|lllllllllllllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggg$$$$$$$$$$$$$$$$llllllllllllloooooooooooooooonnnnnnnnnnnnnnnngggggggg$$$$$$$|
\end{document}

But David Carlisle's answer might be better.

| improve this answer | |
  • if I add that definition to the test file it runs without error but it adds a space between each character and typesets | (as an em-dash in OT1 encoding) at the end) – David Carlisle Mar 8 '17 at 12:20
  • @DavidCarlisle I saw that, too (just after posting the answer). Do you know where this behavior originates from? – Skillmon likes topanswers.xyz Mar 8 '17 at 12:24
  • yes the space is the missing % after the { (although you do not add any break point apart from that) and the final | is because you typeset #1 before testing if you are at the end. (also why do you not put the definition in the test file so people can run your answer?) – David Carlisle Mar 8 '17 at 12:52
  • @DavidCarlisle I found the issues myself but thanks for the explanations and effort. I'll edit the answer again to include the \def in the document. – Skillmon likes topanswers.xyz Mar 8 '17 at 13:12

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.