This question has developed from How to deal with very long lstinline-phrases like long class names?.
That question is about line breaking inside a \lstinline
phrase. I have provided an answer which automatically hyphenates on capital letters, and then, in a comment, I realized it could sometimes be useful to have automatic hyphenation on underscores _
as well.
I managed to implement this, but one glitch remains: I would like to prevent automatic _
-hyphenation at the start of the name (in Python, for example, this is common situation, as by convention, internal names start with an underscore (or two)).
\documentclass{article}
\usepackage[paperwidth=10cm,paperheight=5cm]{geometry}
\pagestyle{empty}
\usepackage{lstdoc}
\lstMakeShortInline[literate={\_}{\_}{1\discretionary{\_}{}{}}]|
\begin{document}
\textbf{Good line break:} in this paragraph the line break in the
python's variable |a_python_style_name| is good!
\textbf{Bad line break:} the internal variable |_internal_python_name|
is not hyphenated very nicely in this paragraph.
\end{document}