# Preventing line break at the start of a long name in \lstinline

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}


-
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Apr 14 at 11:22

Circumvent the \discretionary insertion by inserting something else just for the start. Here are some options:

\documentclass{article}
\usepackage[paperwidth=10cm,paperheight=8cm]{geometry}
\pagestyle{empty}
\usepackage{lstdoc}
\lstset{mathescape=true}
\lstMakeShortInline[literate={\_}{\_}{1\discretionary{\_}{}{}} {~}{\_}{1}]|
\newcommand{\preunderscore}{\_}

\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|\par
|_internal_python_name|

\textbf{Bad line break:} the internal variable |~internal_python_name|\par
|~internal_python_name|

\textbf{Bad line break:} the internal variable |$\preunderscore$internal_python_name|\par
|$\preunderscore$internal_python_name|
\end{document}


With mathescape you can insert a macro instead of a literate replacement for _. Alternatively, use a different literate character for the start. There are differences in spacing.

-