I have the following command:
\newcommand*{\code}{\lstinline[basicstyle=\relscale{0.9}\ttfamily\color{inline code},keywordstyle=\color{inline code},stringstyle=\color{inline code},keepspaces=true]}
I am using this extensively throughout my book, and sometimes the inline code is fairly long (such as a fully-qualified class name). This is causing hbox overflow problems, which is understandable because TeX is unable to hyphenate the code.
One approach that works is to break long code up, so instead of this:
\code{AReallyLongClassName}
I can do this:
\code{A}\-\code{Really}\-\code{Long}\-\code{Class}\-\code{Name}
Whilst this works, it would be really laborious going through all my \code usages and converting them. Also, it makes the source harder to read.
I've been reading through a few existing questions on this matter and found some advice to do this:
\usepackage[htt]{hyphenat} % enable line breaks
However, that seems to only be supported with texttt, which I'm not using (and am not familiar enough with it to know whether I should be using it, but what I've read suggests not).
I've also tried this:
\hyphenation{A-Really-Long-Class-Name}
But that also seems to be ignored by lstinline.
Here is a MWE:
% !TEX program = xelatex
\documentclass[fontsize=11pt,pagesize=auto,hidelinks,cleardoublepage=empty,parskip]{book}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{relsize}
\usepackage[paperheight=10in,paperwidth=6in,margin=2cm,heightrounded,bindingoffset=5mm,showframe=false]{geometry}
\sloppy
\newcommand*{\code}{\lstinline[basicstyle=\relscale{0.9}\ttfamily\color{red},keywordstyle=\color{red},stringstyle=\color{red},keepspaces=true]}
\begin{document}
Here is a paragraph with some inline code. \code{ReallyLongName.First.Second.Third.Fourth.Fifth}.
Here is a paragraph with some inline code. \code{Really}\-\code{Long}\-\code{Name}\-\code{.First}\-\code{.Second}\-\code{.Third}\-\code{.Fourth}\-\code{.Fifth}.
\end{document}
Output:

