6

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}

A good and a bad line-break in <code>\listinline</code>

1
1

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

enter image description here

\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.

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.