13

According to this Question:

How to deal with very long lines that do not contain spaces?

My problem is that my \lstinline-phrases does not contain any special character like "-". In my lstlinlines there are very long class name like ThisIsAVeryLongExampleClassNameAndItHasNoLineBreak. The problem that results from those class names are overfull boxes.

Does anyone knows an approach how to set conditional line breaking marks. (Like \- in normal text).

  • 1
    I don't think that from programmation point of view it is a good idea to use too long name. – projetmbc Aug 29 '12 at 19:28
  • 3
    I know, but the models are given, and so I have no other choice – martin Aug 29 '12 at 20:09
9

Or, to make things fully automatic (in your case):

\documentclass{minimal}
\usepackage{listings}
\begin{document}
\lstinline[
  literate={A}{A}{1\discretionary{}{}{}}
           {B}{B}{1\discretionary{}{}{}}
           {C}{C}{1\discretionary{}{}{}}
           {D}{D}{1\discretionary{}{}{}}
           {E}{E}{1\discretionary{}{}{}}
           {F}{F}{1\discretionary{}{}{}}
           {G}{G}{1\discretionary{}{}{}}
           {H}{H}{1\discretionary{}{}{}}
           {I}{I}{1\discretionary{}{}{}}
           {J}{J}{1\discretionary{}{}{}}
           {K}{K}{1\discretionary{}{}{}}
           {L}{L}{1\discretionary{}{}{}}
           {M}{M}{1\discretionary{}{}{}}
           {N}{N}{1\discretionary{}{}{}}
           {O}{O}{1\discretionary{}{}{}}
           {P}{P}{1\discretionary{}{}{}}
           {Q}{Q}{1\discretionary{}{}{}}
           {R}{R}{1\discretionary{}{}{}}
           {S}{S}{1\discretionary{}{}{}}
           {T}{T}{1\discretionary{}{}{}}
           {U}{U}{1\discretionary{}{}{}}
           {V}{V}{1\discretionary{}{}{}}
           {W}{W}{1\discretionary{}{}{}}
           {X}{X}{1\discretionary{}{}{}}
           {Y}{Y}{1\discretionary{}{}{}}
           {Z}{Z}{1\discretionary{}{}{}}
!ThisIsAVeryLongExampleClassNameAndItHasNoLineBreakThisIsAVeryLongExampleClassNameAndItHasNoLineBreakThisIsAVeryLongExampleClassNameAndItHasNoLineBreakThisIsAVeryLongExampleClassNameAndItHasNoLineBreakThisIsAVeryLongExampleClassNameAndItHasNoLineBreak!

\end{document}

Obviously, this should be put in a style...

(To get - at the end of the line, use \discretionary{-}{}{}. 26 times :-)

  • The drawback with this method is that keywords can't use capital letters. I think that the better solution is the one using \-. – projetmbc Aug 30 '12 at 9:22
  • I guess nothing is wrong with using both methods. For the user, it is trivial to decide which is better, at least in the context of inline-listings. – Sašo Živanović Aug 30 '12 at 9:41
  • And, being (also) a pythonist, I'd make myself another method for breaking at _. – Sašo Živanović Aug 30 '12 at 9:42
  • How do you do ? Python does the job for you ? I like to use Python but I also do my best to automatically produce not too ugly LaTeX code (so as to obtain LaTeX codes that can be read by one human as far as it is possible). – projetmbc Aug 30 '12 at 9:50
  • 1
    I expressed myself badly... Python and TeX are (mostly) separate worlds in my life. But both are beautiful ;-) – Sašo Živanović Aug 30 '12 at 10:00
9

The cool trick from the question you have cited can be used to define a conditional line break:

\documentclass{minimal}
\usepackage{listings}
\begin{document}
Bla bla blabla blabla bla bla blabla bla blabla bla blabla
bla blabla bla blablablabla bla
\lstinline[literate={\\\-}{}{0\discretionary{-}{}{}}]!ThisIsAVeryLong\-Example\-ClassNameAndItHasNoLineBreak!
bla blabla blabla blabla bla
\end{document}

P.S. I guess the \\ as the second arg of \discretionary in the cited question was as mistake? Anyway, the above works for me.

  • this also works fine in a style – flamingpenguin Aug 19 '14 at 20:45
0

I had some luck tonight following these two steps:

  1. Break the name explicitly: \lstinline{ThisIsAVeryLongExampleClassName}\lstinline{AndItHasNoLineBreak}
  2. Ensure your preceding lstset has breaklines=true (or just breaklines actually).

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.