How can I hyphenate words with dots in a \texttt environment in LaTeX?

There is a bunch of discussions online how to enable (or disable) hyphenation in \texttt environments. I found both the options

\usepackage[htt]{hyphenat}


and

\DeclareTextFontCommand{\mytexttt}{\ttfamily\hyphenchar\font=45\relax}


to work. However, what still does not work is to put a line break into a term such as \texttt{ee.Algorithms.Landsat.simpleCloudScore()}. I don't care that there might be rules that suggest not to line break code and want LaTeX to be able to output, for example,

... ee.Algorithms
.Landsat.simpleCloudScore() ...


The solutions discussed for words with dashes won't work, and there is surprisingly little to be found for "my" problem.

You can split at periods and then reinserting them with a penalty in front and the possibility to hyphenate the following part.

\documentclass{article}
\usepackage{xparse}
\usepackage[htt]{hyphenat}

\newcommand{\breakingperiod}{%
\penalty0 % allow a break before the period
.\nobreak\hspace{0pt}%
}

\ExplSyntaxOn

\NewDocumentCommand{\longword}{m}
{
\texttt
{
\seq_set_split:Nnn \l_michael_lw_seq { . } { #1 }
\seq_use:Nn \l_michael_lw_seq { \breakingperiod }
}
}

\ExplSyntaxOff

\begin{document}

\parbox{0pt}{\hspace{0pt}%
\longword{ee.Algorithms.Landsat.simpleCloudScore()}
}

\end{document}


The \parbox trick allows to see all break points.

• This solution worked like a charm! – Michael Mar 26 '19 at 19:38

various possibilities, depending on whether you want a break at a . to be hyphenated

\documentclass{article}

\usepackage{url}
\begin{document}

\newcommand\zz[1]{\par\bigskip\fbox{\parbox{#1}{\ttfamily\hyphenchar\font=\-\lccode\.=\.
a Something.Landsat.simpleCloudScore()}}}

\zz{20em}

\zz{15em}

\zz{10em}

\zz{5em}

\catcode.=\active
\edef\.{\string.}
\renewcommand\zz[1]{\par\bigskip\fbox{\parbox{#1}{\ttfamily\hyphenchar\font=\-\def.{\string.\hspace{0pt}}%
a Something.Landsat.simpleCloudScore()}}}

\zz{20em}

\zz{15em}

\zz{10em}

\zz{5em}
\catcode.=12

\end{document}

• The problem with this solution seems that the \zz command takes a width parameter - and I don't want to specify any width and would much rather have fully automatic hyphenation. Or am I misunderstanding the solution? – Michael Mar 26 '19 at 18:48
• @Michael sorry you misundersttod (I should have been clearer) the \zz is just a test command that sets the text at the specified width in a \fbox so that you can see different linebreaking, you would not use that at all in a real document. – David Carlisle Mar 26 '19 at 19:28